Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

PHP in Templates with 2.5.0...
Goto page Previous  1, 2
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Jun 25, 2003 11:42 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Jun 25, 2003 11:47 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Jun 25, 2003 11:50 pm    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Jun 25, 2003 11:50 pm    Post subject: Reply with quote

Somehow, Monte is always a step ahead of me Smile
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Jun 26, 2003 4:03 am    Post subject: Reply with quote

mohrt wrote:
{php} with {php}extract($this->_tpl_vars);


old dog Laughing
Back to top
View user's profile Send private message
Panama Jack
Smarty Rookie


Joined: 23 Jun 2003
Posts: 16

PostPosted: Fri Jun 27, 2003 4:18 am    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Jun 27, 2003 4:29 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Panama Jack
Smarty Rookie


Joined: 23 Jun 2003
Posts: 16

PostPosted: Fri Jun 27, 2003 4:34 am    Post subject: Reply with quote

Yep definately would be way faster. Smile

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP