Re: [SMARTY] template from var

From: Date: Mon, 30 Aug 2004 21:16:36 +0000
Subject: Re: [SMARTY] template from var
References: 1 2  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-11741@lists.php.net to get a copy of this message
On Mon, Aug 30, 2004 at 08:31:48AM -0700, boots wrote: > --- Paul <pardel@gmail.com> wrote: > > Still, I would like to know it ;) > > Cheers, > > Paul > > I agree with bok, don't use this, but here is a dirty one -- it assumes > the variable is a global but uses Smarty's standard way of redirecting > input, namely a resource: maybe bok meant this one i once posted: (OTOH) function eval_source($source, &$smarty) { // load the plugin implementation require_once $smarty->_get_plugin_filepath('function', 'eval'); // run the plugin return smarty_function_eval(array('var' => $source), $smarty); } using _get_plugin_filepath() is not that ugly, it's the proposed way to load a plugin within a plugin (according to the manual) i also don't think calling plugins that way from php is entirely bad: it's a documented API you have to follow to implement plugins, why not use it in reverse to call plugins? anyway: i don't want to encourage anybody to do the above for plugins in general if he/she doesn't feel 100% confident. in the first place: the use of {eval} is questionable in each and every case. you can consider {eval} to be 10 times slower than display(). you are throwing away smarty's main strength with eval: the ability to reuse compiled template source without the need to recompile. never consider {eval} for daily use (only if you employ caching, and even then only as a very very last resort). maybe for certain cases the above may suit, though. if you don't want do evaluate a template from a php variable in general but from maybe a database-record, see http://smarty.php.net/manual/en/template.resources.php greetings messju > function smarty_resource_var_source($tpl_name, &$tpl_source, &$smarty) > { > global $$tpl_name; > $tpl_source = $$tpl_name; > return true; > } > function smarty_resource_var_timestamp($tpl_name, &$tpl_timestamp, > &$smarty) > { > $tpl_timestamp = ''; > return true; > } > function smarty_resource_var_secure($tpl_name, &$smarty) { return true; > } > function smarty_resource_var_trusted($tpl_name, &$smarty) { return; } > > eg: > > $bar = 'eek!'; > $foo = '{$bar}'; > > $smarty->assign('bar', $bar); > $smarty->display('var:foo'); > > NOTE: examine your compiled/cache dirs after this one runs -- you may > not like it. > > xo boots > > > On Mon, 30 Aug 2004 22:50:44 +1000, Robert Amos <shiznatz@gmail.com> > > wrote: > > > Theres an ugly hack you can use from within a php file, but you're > > > better off avoiding it as best you can. > > > > > > -bok > > > > > > On Mon, 30 Aug 2004 13:59:05 +0200, Stefan <e.tuli@gmx.de> wrote: > > > > Paul wrote: > > > > > > > > >Hi all, > > > > >Is there any way to use the content of a variable as a template? > > > > >Thank you! > > > > >Paul > > > > > > > > > inside a template use {eval}. Inside a php file, assign it to an > > > > existing template. Afaik there's no other possibilty of > > evaluation > > > > template code withing php code without loading it directly from a > > file. > > -- > Smarty General Mailing List (http://smarty.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.smarty.general (#11741) next »