View previous topic :: View next topic |
Author |
Message |
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Jun 25, 2003 11:42 pm Post subject: |
|
|
Panama Jack wrote: | Thanks, that works like a charm but it should still be added as a togglable option in the configuration. There shouldn't be a need to add the above for every iunstance of {php}{/php}. I would still like to see this as an option in future releases.
|
The only problem is that it creates an overhead. For one, a new property has to be added to the base Smarty class, something like $extract_php_vars. Another is the if() test for it.
A new property, a new if() test. Although this may seem insignificant, it does add to the core. Since there has been only one request for this (yours), I can't justify putting it in this extra feature when it will only be used by you.
But, like boots said, you can use a prefilter to transparently extract everthing for you automatically. It does not bloat the core Smarty code and it does not affect your runtime speed. It only does a single step at compile time and meets your BC requirement. Just have the prefilter replace {php} with {php}extract($this->_tpl_vars);.
Monte |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Jun 25, 2003 11:47 pm Post subject: |
|
|
You know what, even a better idea...
Write a prefilter that replaces all the vars within {php}{/php} tags.
Example:
{php} echo $foo; {/php}
Will get translated to:
{php} echo $this->get_template_vars('foo'); {/php}
Now you keep your old 2.3.1 behavior, and avoid the extract() overhead.
Monte |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Jun 25, 2003 11:50 pm Post subject: |
|
|
oh yeah, you can probably lesson the number of manual changes you will have to make by greping your templates-- replace "[php]" with "[php]extract(....)". Many text editors and IDE's allow you to do replaces over entire directories.
OR, you can use a prefilter as I suggested to do the same thing on the fly. The latter is probably the best option because you can include it in your custom Smarty class wrapper so that all of your templates get that treatment silently. In that way, you can mimick the old behaviour and still move to the new version. That's the beauty of Smarty--it is very flexible and can often be extended to meet the needs of your design patterns if you are willing to customize it.
Good luck!
Last edited by boots on Thu Jun 26, 2003 4:00 am; edited 2 times in total |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Jun 25, 2003 11:50 pm Post subject: |
|
|
Somehow, Monte is always a step ahead of me |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Jun 26, 2003 4:03 am Post subject: |
|
|
mohrt wrote: | {php} with {php}extract($this->_tpl_vars); |
old dog |
|
Back to top |
|
Panama Jack Smarty Rookie
Joined: 23 Jun 2003 Posts: 16
|
Posted: Fri Jun 27, 2003 4:18 am Post subject: |
|
|
Well it would be nice if everything worked. Sigh...
If I add this line right after the {php} in the template...
Code: |
extract($this->get_template_vars());
|
The template works just fine. All of the template variables are extracted.
Now if I add a prefilter like this...
Code: |
function extract_variables($tpl_source, &$smarty)
{
return preg_replace("/{php}/","{php}extract($this->get_template_vars());",$tpl_source);
}
$smarty->register_prefilter("extract_variables");
|
I end up getting this error...
Code: |
Parse error: parse error, unexpected ')' in /home/www/templates_c/%%-10/%%-1039886737/main2.tpl.php on line 80
|
If I change the prefilter to something simple like this...
Code: |
function extract_variables($tpl_source, &$smarty)
{
return preg_replace("/{php}/","{php}echo\"I work!\";",$tpl_source);
}
$smarty->register_prefilter("extract_variables");
|
Then everything works like a charm as "I work!" is displayed at every place there is a {php} tag.
But for some reason adding that extract command as a prefilter completely kills the compilation of the template. So what other ideas do you guys have to extracting all of the template variables for use in the PHP sections. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Jun 27, 2003 4:29 am Post subject: |
|
|
Just use str_replace() for a literal replacement, it's way faster than preg_replace() and you don't have to deal with escaping preg_replace()'s special characters.
Monte |
|
Back to top |
|
Panama Jack Smarty Rookie
Joined: 23 Jun 2003 Posts: 16
|
Posted: Fri Jun 27, 2003 4:34 am Post subject: |
|
|
Yep definately would be way faster.
I found the problem. Kind of stupid of me really...
Code: |
function extract_variables($tpl_source, &$smarty)
{
return preg_replace("/{php}/","{php}extract(\$this->get_template_vars());",$tpl_source);
}
|
I forgot that the $ would be taken as a beginning of a variable. So the object was trying to execute and returning a null. By adding the backslash that cured the problem and now it works like a charm. |
|
Back to top |
|
|