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

Caching of assigned variables within {dynamic}
Goto page 1, 2  Next
 
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 -> General
View previous topic :: View next topic  
Author Message
do1gdo
Smarty Rookie


Joined: 05 Aug 2007
Posts: 6

PostPosted: Sun Aug 05, 2007 1:16 pm    Post subject: Caching of assigned variables within {dynamic} Reply with quote

Hello,

is it possible to cache assigned variables within dynamic-blocks?
(dynamic-block see manual/en/caching.cacheable.php)

Example:
Code:
$smarty->caching = true;
$smarty->assign('userid', $_COOKIE['userid']);

if (!$smarty->is_cached())
{
  $ownerid = some_complex_sql_calls();
  $smarty->assign('ownerid', $ownerid);
}

$smarty->display();


Code:
...
{dynamic}
  {if $userid==$ownerid}
    Editlink
  {/if}
{/dynamic}
...


Editlink should only be showed when the logged in user is the owner of the posting ...
The problem is, that $ownerid is not set when the template is cached.


Any solution?
(without setting up the complex sql each time)


Thank you very much,
Oliver
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sun Aug 05, 2007 3:24 pm    Post subject: Reply with quote

Smarty does just cache the generated HTML output and not the values of any variables etc. For that reasion it is not possible what you are trying to achieve.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Aug 06, 2007 3:05 am    Post subject: Reply with quote

Sort of, but you would need to indicate the variables to cache when registering the plugin as dynamic. That implies that you probably couldn't use a catch-all "dynamic" plugin but would rather have to create custom ones for each of your purposes. Note that in contrast to non-caching plugins, {insert} plugins work that way by default. The manual has more info.

That said, you likely don't really need this and/or will find it more trouble than it is worth. What are you trying to do in particular?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Aug 06, 2007 6:04 pm    Post subject: Reply with quote

I thought a little bit more about this problem and have rewriten the {dynamic} plugin to resolve this problem.

Code:
function smarty_block_dynamic($param, $content, &$smarty)
{
    if (isset($smarty->_cache_info['cached_vars'])) {
        foreach ($smarty->_cache_info['cached_vars'] As $thisvar => $value) {
            $smarty->_tpl_vars[$thisvar] = $value;
        }
    } else {
        if (isset($param['cached_vars'])) {
            $vars = explode(',', $param['cached_vars']);

            foreach ($vars As $thisvar) {
                $smarty->_cache_info['cached_vars'][$thisvar] = $smarty->_tpl_vars[$thisvar];
            }
        }
    }
    return $content;
}


{dynamic} has now a paramter 'cached_vars' which is a comma separated list of variable names which should be cached and be available in the dynamic area of your template.

Code:
{dynamic cached_vars='var1,var2,....'}


Your example should now read in the template like this:

Code:
{dynamic cached_vars='ownerid'}
  {if $userid==$ownerid}
    Editlink
  {/if}
{/dynamic}


I hope this helps.
Back to top
View user's profile Send private message
do1gdo
Smarty Rookie


Joined: 05 Aug 2007
Posts: 6

PostPosted: Mon Aug 06, 2007 8:31 pm    Post subject: Reply with quote

Thank you very much for your answers.


@U.Tews:
The sample does not work correct within {foreach}-loop. Only the value of the first loop is cached. I will try to modify your sample to support foreach ... and if i'm successfull, post it here. The _cache_info-var is a good point to start Smile


@boots "What are you trying to do in particular?"

See http://www.opencaching.de/viewcache.php?wp=OC0ED3
(i'm currently porting it to smarty)

The logentries at the bottom have links to edit or delete it, when you are the owner of the listing or the particular logentry. To let caching work effectivly, i dont want to add the current userid / loginid to $smarty->cache_id ...

The users are logged in most the time ... so activating caching only when the user is not logged in is no option to improve performance. As you see at the bottom, rendering-time is ~0.02 sec (without caching) ... so it's actually no realy big problem to disable caching at all.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Aug 06, 2007 9:23 pm    Post subject: Reply with quote

You can use it in this way in {foreach}

Code:
{foreach name=test item=aitem from=$array}
this is loop for {$aitem}<br>
{dynamic cached_vars='array'}
<br>{$b}<br>
{if $array[$smarty.foreach.test.index] eq $b}   ist gleich <br>
{/if}
{/dynamic}
{/foreach}
Back to top
View user's profile Send private message
do1gdo
Smarty Rookie


Joined: 05 Aug 2007
Posts: 6

PostPosted: Tue Aug 07, 2007 4:43 pm    Post subject: Reply with quote

<removed, because does not work>

Last edited by do1gdo on Tue Aug 07, 2007 7:55 pm; edited 1 time in total
Back to top
View user's profile Send private message
do1gdo
Smarty Rookie


Joined: 05 Aug 2007
Posts: 6

PostPosted: Tue Aug 07, 2007 6:42 pm    Post subject: Reply with quote

Attention: does not work with {include file="..."}!
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Aug 07, 2007 7:08 pm    Post subject: Reply with quote

yeah, your rewrite looks also better to me.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Aug 07, 2007 7:24 pm    Post subject: Reply with quote

Probably won't work with nested nocache's either. Seems like you are going to do a is_cached() checked in your view/application logic. maybe consider doing the heavy lifting in there instead of inside the template itself? Tricky code is tricky to debug Wink
Back to top
View user's profile Send private message
do1gdo
Smarty Rookie


Joined: 05 Aug 2007
Posts: 6

PostPosted: Tue Aug 07, 2007 8:02 pm    Post subject: Reply with quote

ok, this one works better ... and solves my problem Smile

The name-attribute has to be set and has to be unique in all included files.
Nesting of {nocache} is not supported (and should not be required).

Code:
/* block nocache
 *
 * usage
 *
 * {nocache}...{/nocache}
 *
 * OR
 *
 * {nocache name="<unique blockname>" <varname1>=$<value1> [...]}...{/nocache}
 */
function smarty_block_nocache($param, $content, &$smarty, &$repeat)
{
  static $counter = array();

  if ($repeat)
  {
    if (!isset($param['name']))
      return $content;

    $name = $param['name'];
    unset($param['name']);

    if (!isset($counter[$name]))
      $counter[$name] = 0;
    $counter[$name]++;

    if ($smarty->_cache_including)
    {
      $param = isset($smarty->_cache_info['cached_vars'][$name]['items'][$counter[$name]]) ? $smarty->_cache_info['cached_vars'][$name]['items'][$counter[$name]] : array();
    }
    else
    {
      $smarty->_cache_info['cached_vars'][$name]['items'][$counter[$name]] = $param;
    }

    foreach ($param AS $k => $v)
    {
      $smarty->_tpl_vars[$k] = $v;
    }
  }

  return $content;
}


Code:
$tpl->caching = true;
$tpl->assign('user', array('userId' => $_GET['userid']));

if (!$tpl->is_cached())
{
  $a = array(array('objectId' => 102, 'ownerId' => 2),
             array('objectId' => 101, 'ownerId' => 1),
             array('objectId' => 103, 'ownerId' => 3));

   $tpl->assign('objects', $a);
}


Code:
{foreach from=$objects item=objectItem}
   {$objectItem.objectId}
   {nocache name="editlink" ownerId=$objectItem.ownerId objectId=$objectItem.objectId}
      {if $user.userId==$ownerId}
         <a href="#a{$objectId}">Editlink</a>
      {/if}
   {/nocache}
   <br />
{/foreach}
Back to top
View user's profile Send private message
do1gdo
Smarty Rookie


Joined: 05 Aug 2007
Posts: 6

PostPosted: Tue Aug 07, 2007 8:06 pm    Post subject: Reply with quote

boots wrote:
Probably won't work with nested nocache's either.


I dont need nesting nocache at the moment.

[quote="boots"]Seems like you are going to do a is_cached() checked in your view/application logic. maybe consider doing the heavy lifting in there instead of inside the template itself?

what do you mean with "heavy lifting"?
(sorry, my english is not that good)

boots wrote:
Tricky code is tricky to debug Wink


I dont know if the nocache-block is too tricky ... i will test it some weeks and maybe its too tricky or raises other problems ...
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Aug 07, 2007 10:41 pm    Post subject: Reply with quote

As far as I have tested it does work with nested nocache's.

Good job do1gdo
Back to top
View user's profile Send private message
markwu
Smarty Rookie


Joined: 28 Sep 2005
Posts: 6

PostPosted: Fri Apr 04, 2008 5:46 pm    Post subject: Reply with quote

do1gdo, Good job!

You really save my life.

Thanks.

Mark
Back to top
View user's profile Send private message
Webslave
Smarty n00b


Joined: 04 May 2008
Posts: 3

PostPosted: Sun May 04, 2008 9:43 am    Post subject: PHP5 <> dynamic-module Reply with quote

Hello,
Sorry for my bad english.

I`m been changed from PHP4 to PHP5.
All Smarty-Templates where work with the dynamic-module are don`t work since the change.
I`m get a white site. Also with debug.
register_globals are on, safe_mode are off

can you help me?
Thank you very mutch.

Kind regards,
Webslave
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 -> General All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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