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

Variable assignment during template rendering

 
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 -> Smarty Development
View previous topic :: View next topic  
Author Message
arrix
Smarty n00b


Joined: 13 Oct 2011
Posts: 2

PostPosted: Wed Jan 04, 2012 10:53 am    Post subject: Variable assignment during template rendering Reply with quote

Hi,

Is there a way to reliably assign variables during template rendering (fetch or display)? Observation is that once fetch() begins, $smarty->assign() is no longer effective, only $_smarty_tpl->assign() works.

It may sound weird at first. Why would one want to do that? Here is my scenario.

I've implemented a fragment caching framework with smarty integration.
Code:

{{mycache_begin id="cache_id" callback="provide_data_function"}}
{{$expensive_data}}
{{mycache_end}}


mycache_begin and mycache_end are implemented as compiler functions. The idea is that when the cache isn't available, the callback is invoked to assign needed template variables.

It worked well in Smarty 3.0 RC2. But in 3.1.7, it turns out that inside the callback $this->smarty->assign(...) isn't effective. The variables assigned in the callback can't be used for rendering though they confusingly show up in the debug console.

A work around is to assign using $_smarty_tpl, which can be exposed to the callback by the compiler function.

In 3.1.7, $_smarty_tpl is an instance of Smarty_Internal_Template.
Is it reliable to assign using $_smarty_tpl?

Thanks!

Arrix
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Jan 04, 2012 5:56 pm    Post subject: Reply with quote

Yes, in compiler functions you must use $_smarty_tpl->assign().

For speed optimizations Smarty 3.1 does copy the variables assigned to the Smarty object into the scope of the template object when the template is invoked. If you add variables to the Smarty object from within the template these are no longer seen by the template.
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 -> Smarty Development All times are GMT
Page 1 of 1

 
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