View previous topic :: View next topic |
Author |
Message |
do1gdo Smarty Rookie
Joined: 05 Aug 2007 Posts: 6
|
Posted: Sun Aug 05, 2007 1:16 pm Post subject: Caching of assigned variables within {dynamic} |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Aug 05, 2007 3:24 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Aug 06, 2007 3:05 am Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Aug 06, 2007 6:04 pm Post subject: |
|
|
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 |
|
do1gdo Smarty Rookie
Joined: 05 Aug 2007 Posts: 6
|
Posted: Mon Aug 06, 2007 8:31 pm Post subject: |
|
|
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
@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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Aug 06, 2007 9:23 pm Post subject: |
|
|
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 |
|
do1gdo Smarty Rookie
Joined: 05 Aug 2007 Posts: 6
|
Posted: Tue Aug 07, 2007 4:43 pm Post subject: |
|
|
<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 |
|
do1gdo Smarty Rookie
Joined: 05 Aug 2007 Posts: 6
|
Posted: Tue Aug 07, 2007 6:42 pm Post subject: |
|
|
Attention: does not work with {include file="..."}! |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Aug 07, 2007 7:08 pm Post subject: |
|
|
yeah, your rewrite looks also better to me. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Aug 07, 2007 7:24 pm Post subject: |
|
|
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 |
|
Back to top |
|
do1gdo Smarty Rookie
Joined: 05 Aug 2007 Posts: 6
|
Posted: Tue Aug 07, 2007 8:02 pm Post subject: |
|
|
ok, this one works better ... and solves my problem
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 |
|
do1gdo Smarty Rookie
Joined: 05 Aug 2007 Posts: 6
|
Posted: Tue Aug 07, 2007 8:06 pm Post subject: |
|
|
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 |
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Aug 07, 2007 10:41 pm Post subject: |
|
|
As far as I have tested it does work with nested nocache's.
Good job do1gdo |
|
Back to top |
|
markwu Smarty Rookie
Joined: 28 Sep 2005 Posts: 6
|
Posted: Fri Apr 04, 2008 5:46 pm Post subject: |
|
|
do1gdo, Good job!
You really save my life.
Thanks.
Mark |
|
Back to top |
|
Webslave Smarty n00b
Joined: 04 May 2008 Posts: 3
|
Posted: Sun May 04, 2008 9:43 am Post subject: PHP5 <> dynamic-module |
|
|
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 |
|
|