Re: [SMARTY] Question: Injecting template code within a currently processed template
| From: | Andr� Rabold | Date: | Thu, 14 Nov 2002 13:31:20 +0000 |
| Subject: | Re: [SMARTY] Question: Injecting template code within a currently processed template | ||
| References: | 1 2 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-6435@lists.php.net to get a copy of this message | ||
With the code below you can inject any template code within the currently
processed template:
function smarty_function_myFunction ($params, &$this) {
// ...
$template_path = "path/to/my/template.tpl"; // path to my template
$template_code = implode("\n", file($template_path)); // load file from
disk
// ...
$this->assign("specialVars", $mySpecialVars); // assign some vars to the
template
$this->_compile_template("evaluated template", $template_code,
$compiled); // now compile the template into php code
eval('?>' . $compiled);
}
I am developing a HTML Form Plugin with all HTML elements beeing Smarty
templates.
{form}
{form_field name="myCombo" type="combobox" options=$myOptions
value=$myOptionValue}
{/form}
Now my "form_field" plugin loads another template called
"form_field_combobox" and inserts it's output into the above
template:
<select name=$field.name ... >
{section loop=$field.options ... } ... {/section}
</select>
This way my plugin is more flexible than everything I have seen before. And
it's easy to add new form elements not
existing in HTML.
BUT: The above solution (my function that evals another template) is too
slow for me. The following solution is not
nice but much (!) faster:
function smarty_function_myFunction ($params, &$this) {
// ...
$template_path = "path/to/my/template.tpl"; // path to my template
// ...
$subtemplate = new $Smarty();
$subtemplate->assign("specialVars", $mySpecialVars); // assign some vars
to the template
$subtemplate->display($template_path);
}
"Paolo Nesti Poggi" <pnp@italyflash.com> schrieb im Newsbeitrag
news:NFBBIMEDOLFHIIPILFPIIEEHCNAA.pnp@italyflash.com...
> Hi André,
>
> would you share the code?
> Both because I don't quite got the whole picture of what you wanted to
> achieve, and because it at the same time sounds useful.
>
> Bye
> Paolo
>
>
> > -----Oprindelig meddelelse-----
> > Fra: André Rabold [mailto:arabold@nero.com]
> > Sendt: 14. november 2002 13:18
> > Til: smarty-general@lists.php.net
> > Emne: Re: [SMARTY] Question: Injecting template code within a currently
> > processed template
> >
> >
> >
> > I have found what I have searched. The plugin I needed does
> > already exists:
> > function.eval.php. I've created my own function which works
> > excelent now :-D
> >
> > bye,
> > André
> >
> >
> > "André Rabold" <arabold@nero.com> schrieb im Newsbeitrag
> > news:20021114074236.28941.qmail@pb1.pair.com...
> > > Right, this sounds good. It is not the best solution but it's
> > ok. I still
> > > think a custom
> > > include plugin would be a nicer cleaner way to implement this. It's
not
> > just
> > > a simple
> > > {include} I need but some PHP code must be executed before loading the
> > > template.
> > > Sure I could include the code via {php}{/php} but that's not
> > really what I
> > > want.
> > >
> > > Thanks for help anyway
> > > André
> > >
> > >
> > >
> > > "Gary Loescher" <gary@loescher.org> schrieb im Newsbeitrag
> > > news:000701c28b25$3c08b690$0aebf80c@cb767813a...
> > > Why not just use {include file=$template myVars=$vars}
> > > (or something similar)? For transformations before compilation, try
> > > creating a prefilter:
> > >
> > > http://smarty.php.net/manual/en/plugins.prefilters.postfilters.html
> > >
> > > Gary
> > >
> > > >-----Original Message-----
> > > >From: André Rabold [mailto:arabold@nero.com]
> > > >Sent: Wednesday, November 13, 2002 8:02 AM
> > > >To: smarty-general@lists.php.net
> > > >Subject: [SMARTY] Question: Injecting template code within a
> > > >currently processed template
> > > >
> > > >
> > > >I want to create a plugin function which is able to dynamically
include
> > > >other templates. Such a function must be executed before the script
> > > >is compiled and injects template code into the currently processed
> > > >template.
> > > >The only plugin type which could be able to solve my problem is the
> > > >"compiler" plugin but it doesn't fit my needs. I could only do
> > > >some
> > > >regex replacements or something similar which isn't what i want.
> > > >
> > > >For now I solved the problem writing a normal function plugin:
> > > >
> > > ><?php
> > > >function smarty_function_injectTemplate ($params, $smarty) {
> > > > // ... do some stuff here
> > > > $myTemplate = $params["template"];
> > > > $smarty = new Smarty();
> > > > $smarty->assign( $myVars );
> > > > $smarty->display( $myTemplate );
> > > >}
> > > >?>
> > > >
> > > >This isn't nice :-( Does anybody has a better solution?
> > > >
> > > >Thanks in advance,
> > > >André
> > > >
> > > >
> > > >
> > > >--
> > > >Smarty General Mailing List (http://smarty.php.net/)
> > > >To unsubscribe, visit:
> > > >http://www.php.net/unsub.php
> > > >
> > >
> > >
> >
> >
> >
> > --
> > Smarty General Mailing List (http://smarty.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>