|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
jetter Smarty Rookie
Joined: 01 Feb 2016 Posts: 5
|
Posted: Thu Mar 01, 2018 1:39 pm Post subject: include and dynamic attribute value |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Mar 01, 2018 1:44 pm Post subject: |
|
|
Your attempts are failing because "nocache" is a flag, not option. It has no parameters. |
|
Back to top |
|
jetter Smarty Rookie
Joined: 01 Feb 2016 Posts: 5
|
Posted: Thu Mar 01, 2018 2:16 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Mar 01, 2018 5:17 pm Post subject: |
|
|
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Thu Mar 01, 2018 10:47 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Mar 01, 2018 11:35 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Apr 09, 2018 11:52 am Post subject: |
|
|
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 |
|
|
|
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
|