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

Caching question: nocache/dynamic blocks inside foreach loop

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


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

PostPosted: Fri Dec 25, 2009 1:29 am    Post subject: Caching question: nocache/dynamic blocks inside foreach loop Reply with quote

Hello,

i've the following question about smarty caching (i use now Smarty3 B5 but I had the same problem with Smarty2 -> instead of {nocache} tags from S3 i 've used dynamic blocks in S2):

I have a loop through a lot of objects for displaying a table like:

Code:

{foreach $objects as $object...}
  {Display a lot of properties from $object with heavy db load, but good cachable because values are not changing so often}
  {Diplaying a view count for the $object => not cachable}
{/foreach}


I didn't found a possibility to make only the view count value not cachable. The view count will be load by a special object that get the ID from the loop object. If i set this line with the {nocache} tags like

Code:
{nocache}{$cache->getViewCount($loopObject->getId())}{/nocache}


i get an error that i called the function ->getId() on a non object (because the $loopObject does not exists in the written cached file). If i try to assing the id value to a variable before like

Code:

{nocache}{assign var=ID value=$loopObject->getId()}{/nocache}

or
Code:

{assign var=ID value=$loopObject->getId() nocache}


it will also not work. The only way it to set the hole {foreach} as nocached what makes the cache sensless. What i need is a solution that in the final cache file the $loopObject ->getId() call is replaced with the real Value returned by the ->getId() call like:

Code:
{$cache->getViewCount(1234)}


where 1234 is the real value of the id of my $loopObject. Maybe Something like modifier

Code:
{nocache}{$cache->getViewCount($loopObject->getId()|realvalue)}{/nocache}


or the possibility of setting the{nocache} tags inside the function call like

Code:
{nocache}{$cache->getViewCount({/nocache}{$loopObject->getId()}{nocache})}{/nocache}


Hope that someone could understand my problem..
Thanks a lot for help
ndorphin
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Fri Dec 25, 2009 4:30 pm    Post subject: Reply with quote

This can't work as there is no way to get variable data from inside a cached {foreach} as nocached values into the cache file.

Would it be possible to assign with litle overhead also for a cached page an array with the id's from the script? In this case you could use something like this:
Code:

{nocache}{$i=0}{/nocache}
{foreach $objects as $object...}
  {Display a lot of properties from $object with heavy db load, but good cachable because values are not changing so often}
  {Diplaying a view count for the $object => not cachable}
{nocache}{$cache->getViewCount($idarray[$i])}{$i=$i+1}{/nocache}
{/foreach}
Back to top
View user's profile Send private message
ndorphin
Smarty Rookie


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

PostPosted: Fri Dec 25, 2009 6:58 pm    Post subject: Reply with quote

U.Tews wrote:
This can't work as there is no way to get variable data from inside a cached {foreach} as nocached values into the cache file.

Would it be possible to assign with litle overhead also for a cached page an array with the id's from the script? In this case you could use something like this:
Code:

{nocache}{$i=0}{/nocache}
{foreach $objects as $object...}
  {Display a lot of properties from $object with heavy db load, but good cachable because values are not changing so often}
  {Diplaying a view count for the $object => not cachable}
{nocache}{$cache->getViewCount($idarray[$i])}{$i=$i+1}{/nocache}
{/foreach}


Ok, fine that will help a bit.

But maybe this will be a good feature request to transform a loop variable value into a fixed variable within a cached block. Something like

Code:
{nocache}{assing var=fixed value=$someLoopVariableOrProperty makefixedvalue}{/nocache}


or the possibility to hard code a variable value into a nocached block like

Code:
{nocache}{$cache->getViewCount({$loopValue|makefixedvalue})}{/nocache}


From technical view this should possible during generate the cached file or not?

Thanks a lot!
ndorphin
[/b][/code]
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