I have a template .php file with a bunch of functions in it to render out html. Here is a snippet:
$j ='
table("tb_02", "600", "", "ebebeb", "right", "top", "30 0 30 0","Table content...");
//whole bunch of similar functions here...
';
The table function looks like this:
function table($tbID, $tbWidth, $tbHeight, $tbBgColor, $tbHzAlign, $tbVtAlign, $tbPad, $tbContent) {
// code omitted here
$a = "
<table id=\"$tbID\" width=\"$tbWidth\" $tbHeight $tbBgColor border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td valign=\"$tbVtAlign\" align=\"$tbHzAlign\" width=\"$tbWidth\" style=\"$pad\">
$tbContent
</td>
</tr>
</table>
";
echo $a;
};
What I want to do is turn the result of $j in the template .php file (which will be html only – no php) into a string. The issue I’m having is that I first need it eval’d (the result of the functions, and then convert that). I’m after something like this, but can’t find a way to get the eval’d code back to a string:
$j = eval($j);
$j = str_replace("<", "<", $j);
$j = str_replace(">", ">", $j);
echo $j;
Thanks for the help!
evalis not a templating system. It’s slow, unsafe, and shouldn’t be used. There is always a better way to do things.I suggest looking around for a real templating system, what you are doing is very bad practice.
First off, your functions should be
returning data, notechoing it. That way, you can manipulate it, andechoit out all at once.Second, have a look at
HEREDOCs, they’ll make your life easier.Lastly, you shouldn’t be trying to
evalstrings, it’s pad practice. My suggestion may not be any better practice, but it’s better thaneval.Instead of
$jbeing a string, try making it an array.Now what you can do, is use
call_user_func_arrayto “eval” the code.