Jump to content

[SOLVED] Creating a template engine: Using Echo and eval() side-by-side


Recommended Posts

Hi all, I've been trying in vain to make the following work correctly and can't for the life of me find anything in  :rtfm: .

 

I'm making a template engine (can't dump the project and use Smarty) from the ground up and it's working by replacing keywords in a html file like

{phpfreaks::isgreat}

with either HTML output or PHP using echo or eval respectively. The database table rows are marked accordingly so no problems there until I examine the output, where I find that eval has worked correctly but instead of putting it where the old keyword was it places it at the very beginning, what am I doing wrong and how can I work around it to get the evaluated PHP code where it should be.

 

Thanks

Heres the code, the first loop searches and replaces HTML and the second PHP.

 


	//Search and replace template terms that are not PHP
	$this->res = mysql_query("select * from " . PRE . "confTemplateEngine where isPHP='0'");
	while($this->row = mysql_fetch_array($this->res))
	{
		$this->fileContents = str_replace($this->row['searchValue'], $this->row['replaceValue'], $this->fileContents);
	}

	//Search for markup used to generate PHP, then eval them and return to fileContents
	$this->res = mysql_query("select * from " . PRE . "confTemplateEngine where isPHP='1'");
	while($this->row = mysql_fetch_array($this->res))
	{
		$this->fileContents = str_replace($this->row['searchValue'], eval($this->row['replaceValue']), $this->fileContents);
	}

 

The output source is this...


It Works!<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<style type="text/css"> 
<!--
body {
background-color: #F00;
}
#apDiv1 {
position:absolute;
width:200px;
height:115px;
z-index:1;
left: 224px;
top: 56px;
background-color: #00CCFF;
}
.sfg {
font-weight: bold;
font-family: "Lucida Console", Monaco, monospace;
font-size: 24px;
}
#apDiv2 {
position:absolute;
width:435px;
height:115px;
z-index:2;
left: 582px;
top: 183px;
}
-->
</style></head> 

<body> 
<div id="apDiv1">qwerty qwerty qwerty success!</div> 
<p class="sfg"> </p> 
<p class="sfg"> </p> 
<p class="sfg">qwerty qwerty qwerty success! 
</p> 
<p class="sfg"> </p> 
<p class="sfg">qwerty qwerty qwerty success!</p> 
<div id="apDiv2"></div> 
</body> 
</html> 

 

The "qwerty qwerty qwerty ect" is a successful HTML replace (for example in its place was {qwerty} and it successfully changed it and placed it in the correct location. However the "It Works!" at the top of the page is evaluated code that should be inside "<div id="apDiv2"></div>" where the original "{php::itworks}" was.

this does not do, what you think it does:

 

$this->fileContents = str_replace($this->row['searchValue'], eval($this->row['replaceValue']), $this->fileContents);

 

this is supposed to simply fill the property "fileContents", right? It doesn't. It immediately executes the php code. That's why it turns up at the beginning of the file. You need to get rid of the eval at this place.

 

I bet you have a different place where you echo "fileContents"? This is where you need to make the distinction between eval and echo. Perhaps make "fileContents" an array and add one entry each time your loop runs. you can loop through it at a later point and decide to either echo or eval the entry.

 

Bjom

i have an idea...just use Smarty lol I wrote an MVC framework for my own projects ( quite alot in it really! ) with smarty fully integrated for the engine...fantastic

this does not do, what you think it does:

 

$this->fileContents = str_replace($this->row['searchValue'], eval($this->row['replaceValue']), $this->fileContents);

 

this is supposed to simply fill the property "fileContents", right? It doesn't. It immediately executes the php code. That's why it turns up at the beginning of the file. You need to get rid of the eval at this place.

 

I bet you have a different place where you echo "fileContents"? This is where you need to make the distinction between eval and echo. Perhaps make "fileContents" an array and add one entry each time your loop runs. you can loop through it at a later point and decide to either echo or eval the entry.

 

Bjom

 

Thanks, that's what I wanted to know and you are quite correct about the echo being later on.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.