note 99118 added to function.eval
| From: | contacto at hardcode dot com dot ar | Date: | Wed, 28 Jul 2010 18:47:09 +0000 |
| Subject: | note 99118 added to function.eval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171192@lists.php.net to get a copy of this message | ||
Here i can see a lot of people does the same than me, use the eval() to manage templates.
I've found a nice way to put this inside a function, so the code gets cleaner
function process_template($tpl) {
$html = file_get_contents("tpl/$tpl");
preg_match_all("|(\$\w+)\W|Us", $html, $vars);
if(is_array($vars[1])) {
eval("global ".implode(", ", $vars[1]).";");
}
eval('$html = strislashes("'.addslashes($html).'");');
return $html;
}
echo process_template("some.html");
So first you need to look for variables inside the template, then use eval() to get their values
from outside the function. Then just evaluate the html in the usual form
NOTE: this only works when calling from outside a function, because variables inside a function do
not share memory within another function. A trick is to pass another param, using the result of a
call to "get_defined_vars()" and istead of globalize the template's variables,
grabing the values from the defined vars array (get_defined_vars() should be run when you call this
func, ej: echo process_template("some.html", get_defined_vars()); )
----
Server IP: 201.216.232.3
Probable Submitter: 190.55.170.233
----
Manual Page -- http://www.php.net/manual/en/function.eval.php
Edit -- https://master.php.net/note/edit/99118
Del: integrated -- https://master.php.net/note/delete/99118/integrated
Del: useless -- https://master.php.net/note/delete/99118/useless
Del: bad code -- https://master.php.net/note/delete/99118/bad+code
Del: spam -- https://master.php.net/note/delete/99118/spam
Del: non-english -- https://master.php.net/note/delete/99118/non-english
Del: in docs -- https://master.php.net/note/delete/99118/in+docs
Del: other reasons-- https://master.php.net/note/delete/99118
Reject -- https://master.php.net/note/reject/99118
Search -- https://master.php.net/manage/user-notes.php