Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

nocache blocks and effects on variables

 
Post new topic   Reply to topic    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
ndorphin
Smarty Rookie


Joined: 24 Dec 2009
Posts: 15
Location: Berlin, Germany

PostPosted: Mon Jan 25, 2016 9:34 am    Post subject: nocache blocks and effects on variables Reply with quote

Hi,

here in this sample i would expect the output of 'abc' but the variable is always empty. I thought the unused {nocache} block in {if} condition couldn't affect the variable in {else} but it does. Question is bug or feature?

thanks for info.
ndorphin

{$test='abc'}
{if $doNot}
{nocache}
{$test='xyz'}
{/nocache}
{else}
{$test}
{/if}

Tested with 3.1.29
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Jan 27, 2016 8:59 pm    Post subject: Reply with quote

This is an example of inconsistent use of using the same variable in cached and nocache mode.

The compiler has to produce different code for both cases.
If the variable was assigned by $smarty->assign() the variable and its nocache mode is known when it compiles for example {$test}.

In your example the compiler does first see {$test='abc'} and it will generate code which will be only be executed when the compiled template is rendered, but the variable will not be assigned when the cached template is called.

Later the compiler sees {nocache}
{$test='xyz'}
{/nocache} This will generate an assignment of $test as nocache code which might never be executed because of the value of $doNot.
However the compiler will now set a flag that all future references of $test shall be compiled in nocache mode which happens with {$test} in the else section. In your case it will result in an unassigned value.

The compiler is to be as smart as possible but it can not make assumptions about the run time template code flow.

To say it in one sentence it's an ambiguous use of using same variable in both caching modes.

Gruß aus Hamburg
Back to top
View user's profile Send private message
ndorphin
Smarty Rookie


Joined: 24 Dec 2009
Posts: 15
Location: Berlin, Germany

PostPosted: Sat Jan 30, 2016 1:15 pm    Post subject: Reply with quote

Ok, i understand. Thanks for clearing this, Uwe.

Schönen Tag dann noch!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Bugs 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