Jump to content

[SOLVED] Template system help


Nooty

Recommended Posts

Hello all hope you are well..?

I have written a simple template engine, which pulls the template out of a database, evals it and then outputs it.

this works fine.

I then went to add conditionals into the template so i could add more diversity to the templates, using <if condition=""><endif> tags.

I managed to get this code working in a simple situation, using a preg_replace, and and alternative if statment. i then updated my code with the conditional code, and it worked almost fine.

in my template code, i need to use an addslashes function to make sure the eval function executes correctly (for html that has quotes in it)

when i remove the addslashes, and the template doesnt have quotes, it works perfect, evaluating hte template and parsing all the conditionals, however when i have a template with quotes in it and thus need to use addslshes my code refuses to work. here is my code, im sure the problem is my preg_match search string..

here is my code if anyone can help..

[code=php:0]<?
//For The Record, the data contained in the requested database feild is (without quotes) "Hello.. do i seee foo?<br/><if condition="$foo == "1"">Foo!<endif>"


function fetch($template){
$result = mysql_query("SELECT template FROM " . TABLE_PREFIX . "templates WHERE templatename = '$template' LIMIT 1");

$row = mysql_fetch_array($result);
$template = $row[template];
$template = addslashes($template);
$template = preg_replace("/<if condition=\\\"(.*)\\\">(.*)<endif>/","\" . ($1 ? \"$2\" : \"\") . \"", $template);

return($template);
}

function display($data){
$data = stripslashes($data);
print($data);
}

$foo = "1";

eval('display("' . fetch('index') . '");');
?>[/code]


Thx ;)
Link to comment
https://forums.phpfreaks.com/topic/30917-solved-template-system-help/
Share on other sites

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.