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

include and dynamic attribute value

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


Joined: 01 Feb 2016
Posts: 5

PostPosted: Thu Mar 01, 2018 1:39 pm    Post subject: include and dynamic attribute value Reply with quote

Hello,
I am using an include in a foreach loop:

Code:
{foreach $home_sections as $section}
    {include file="sections/{$section.template}"}
{/foreach}


Now, I am trying to set the nocache attribute of {include} dynamically, based on a property of $section.
I have checked the potential values of the nocache attribute (true and false, default to true) and the different ways of setting attribute values
https://www.smarty.net/docs/en/language.syntax.attributes.tpl

I keep getting a "illegal value of option flag nocache", I have tried:

nocache=$section.nocache
nocache="$section.nocache"
nocache={$section.nocache}
nocache="{$section.nocache}"
also all those using a bespoke modifier "bool":
function smarty_modifier_bool($string){return $string === 'true' ? 'true' : 'false';}

You can see the despair in the pattern...

of course I can do
Code:
{if $section.nocache}
  {include nocache}
{else}
  {include}
{/if}

But I do not see any reason my attempts are failing...

Thank you for any help!
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1268

PostPosted: Thu Mar 01, 2018 1:44 pm    Post subject: Reply with quote

Your attempts are failing because "nocache" is a flag, not option. It has no parameters.
Back to top
View user's profile Send private message
jetter
Smarty Rookie


Joined: 01 Feb 2016
Posts: 5

PostPosted: Thu Mar 01, 2018 2:16 pm    Post subject: Reply with quote

AnrDaemon wrote:
Your attempts are failing because "nocache" is a flag, not option. It has no parameters.


Even if {include nocache=true} and {include nocache=false} are working as expected?

From
https://www.smarty.net/docs/en/language.syntax.attributes.tpl:
{include file="header.tpl" nocache} // is equivalent to nocache=true
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1268

PostPosted: Thu Mar 01, 2018 5:17 pm    Post subject: Reply with quote

If you want an honest answer, I think you're doing something wrong on a fundamental level.
Rethink your approach to templating.
Back to top
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 175
Location: West Coast

PostPosted: Thu Mar 01, 2018 10:47 pm    Post subject: Reply with quote

Looking at the {include} function:
https://www.smarty.net/docs/en/language.function.include.tpl
we see that nocache is not in the table of attributes, rather it is in the table of flags. So, while the name is the same, I will conclude its semantic meaning is different. (I also conclude the page you mention has an erroneous example.)

I would also try wrapping all three lines inside a nocache block:
Code:
{nocache}{foreach $home_sections as $section}
    {include file="sections/{$section.template}"}
{/foreach}{/nocache}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1268

PostPosted: Thu Mar 01, 2018 11:35 pm    Post subject: Reply with quote

The problem is, he for some reason wants selective caching/nocaching behavior on a consistent dataset.
Or his dataset is NOT consistent and should have never been in the same array.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Apr 09, 2018 11:52 am    Post subject: Reply with quote

Code:
{nocache}{foreach...}{include} {/foreach}
Is wrong as {include} is now inside the nocache section and all includes are executed in nocache mode.

It's currently not possible to make the nocache flag variable because of all the compiling and caching procedures.

An other important note: when caching is on and $section.template can have same name with different data in $section you must use some unique data as 'cache_id' otherwise you will get just the first page displayed.

like
Code:
{foreach...}{include file=$section.template cache_id=$section.somename}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General 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