note 99118 deleted from function.eval by danbrown

From: Date: Fri, 30 Jul 2010 16:37:28 +0000
Subject: note 99118 deleted from function.eval by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-171249@lists.php.net to get a copy of this message
Note Submitter: contacto at hardcode dot com dot ar ---- 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()); )

« previous php.notes (#171249) next »