Jump to content

Self made quick template engine.... problems (eval() help!)?


StrangeWill

Recommended Posts

I read using eval() is a lot more efficient way than say... downloading smarty templates (just adding the templates, not even fully implementing them, triples my page load time), but I'm having a major problem:

[code=php:0]
$RegEx = "/{(.*)\.(.*)}/";
$RegExReplace = '$this->Site->Config[\'\\1\'][\'\\2\']';
$Data = preg_replace($RegEx, $RegExReplace, $Data, PREG_SET_ORDER);
eval("\$Data = \"$Data\";");
echo($Data);
[/code]

$Data:
[code]
<p>{site.title}</p>
<p>{site.path}</p>
[/code]

$Data before eval()
[code]
<p>$this->Site->Config['site']['title']</p>
<p>$this->Site->Config['site']['path']</p>
[/code]
Good :D

$Data after eval()
[code]
<p>Object id #1->Config['site']['title']</p>
<p>Object id #1->Config['site']['path']</p>
[/code]

Object id? WTF?! No bad! BAD code! Run the WHOLE code, ugh!

If I do a echo($this->Site->Config['site']['path']); it prints the code fine... some kind of issue with eval()
New code:
[code]
?><p>$this->Site->Config['site']['title']?></p>
<p>$this->Site->Config['site']['path']?></p><?
[/code]

Comes out:
[code]
?><p><?=Object id #1->Config['site']['title']?></p>
<p><?=Object id #1->Config['site']['path']?></p><?
[/code]
It doesn't process it even though that runs through the eval()
[quote author=thorpe link=topic=117155.msg478295#msg478295 date=1165217476]
Can we see the code you've tried? You are also aware that the keyword $this will not work outside of an objects context? This is more than likely your issue buit I would need to see code first.
[/quote]
Yes, I'm using objects, using: echo($this->Site->Config['site']['title']) will print the correct data.

Basically I'm taking this:
[code]
<p>{site.title}</p>
<p>{site.path}</p>
[/code]

I'm translating it into PHP via RegEx:
[code]
<p>$this->Site->Config['site']['title']</p>
<p>$this->Site->Config['site']['path']</p>
[/code]
(or if this is better I can do this)
[code]
<p><?=$this->Site->Config['site']['title']?></p>
<p><?=$this->Site->Config['site']['path']?></p>
[/code]

I COULD cache this to a file, then include() it, but that becomes rather messy, I'd much rather process it as IF it was a php page into the $Data string, I read you do this with eval(), and then insert it into the page.
Sorry didn't have access till this morning:

[code=php:0]
class Wolfram_Studios_Template extends Wolfram_Studios_Base
{
//Template data in array
var $TmpVar;
var $TmpDir;
var $TmpMain;

function Wolfram_Studios_Template($inSite)
{
$this->Name = "Wolfram Studios Template";
$this->Version = "1.0.0";

$this->Site = $inSite;
$this->Site->AddComponent($this);

$this->TmpDir = $this->Site->Config['site']['path'];
$this->TmpMain = $this->Site->Config['site']['path'] .
$this->Site->Config['site']['template_path'] .
'main.tpl';
}
function DisplayTemplate($TemplateName)
{
//Open the template and get all the variables ({var})
$Filename = $this->TmpDir . $TemplateName;
$File = fopen($Filename, 'r');
$FileSize = filesize($Filename);
$Data = fread($File, $FileSize);
fclose($File);

$RegEx = "/{(.*)\.(.*)}/";
$RegExReplace = '$this->Site->Config[\'\\1\'][\'\\2\']';
$Data = preg_replace($RegEx, $RegExReplace, $Data, PREG_SET_ORDER);
eval("\$Data = \"$Data\";");
echo($Data);
}
}

class Wolfram_Studios_Base
{
//Parent Site
var $Site;

//This component's data
var $Version;
var $Name;
var $Requirements;

function AddRequirement($InComponenetName, $InComponentVersion)
{
$this->Requirements[sizeof($this->Requirements)] =
array('Name' => $InComponenetName, 'Version' => $InComponentVersion);
}

}
[/code]

I call DisplayTemplate($TemplateName) under the Wolfram_Studios_Template object I created.
[quote author=thorpe link=topic=117155.msg478989#msg478989 date=1165312641]
Yeah Im sorry, but I really cant see what it is your trying to achieve.
[/quote]

Okay....

I got these values:
$this->Site->Config['site']['title'] = "test 1";
$this->Site->Config['site']['path'] = "test 2";

I got a string:
[code]
<p><?=$this->Site->Config['site']['title']?></p>
<p><?=$this->Site->Config['site']['path']?></p>
[/code]

I want it to output:
[code]
<p>test 1</p>
<p>test 2</p>
[/code]

As if it was rendering a PHP page from the string. Run it like code.
[quote author=thorpe link=topic=117155.msg479348#msg479348 date=1165358475]
Can we see what $Data looks like before you try and use eval() on it? What is the ouput of...?

[code=php:0]
$Data = preg_replace($RegEx, $RegExReplace, $Data, PREG_SET_ORDER);
die($Data);
[/code]
[/quote]

[code]<p>$this->Site->Config['site']['title']</p>
<p>$this->Site->Config['site']['path']</p>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.