View previous topic :: View next topic |
Author |
Message |
ihorko Smarty n00b
Joined: 13 Mar 2014 Posts: 4
|
Posted: Thu Mar 13, 2014 11:32 am Post subject: How to pass a template "value" variable? |
|
|
The main page in the cache. vote.tpl - block survey.
$absnum = 123, generated only at the first boot template and cached.
Template contains:
Code: | {%include file="vote.tpl" сache_id="block|vote_"|cat:$absnum%} |
result in cache:
Code: | <?php echo $_smarty_tpl->getSubTemplate ("vote.tpl", ("block|vote_").($_smarty_tpl->tpl_vars['absnum']->value)..... |
how to get the result?
Code: | <?php echo $_smarty_tpl->getSubTemplate ("vote.tpl", "block|vote_123"..... |
Code
Code: | {%include file="vote.tpl" сache_id="block|vote_123"%} |
works as expected.
How to pass сache_id "value" variable (123) and not a link to it ($absnum)? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Mar 13, 2014 8:29 pm Post subject: |
|
|
If you specify a variable cache_id for an included sub template a separate cache file is created for that sub template (because the cache_id could be variable even if the main template is cached).
I think you did use $smarty->isCached() in your script and did not assign variables when the cache was valid.
Solution assign variable $absnum in any case. |
|
Back to top |
|
ihorko Smarty n00b
Joined: 13 Mar 2014 Posts: 4
|
Posted: Fri Mar 14, 2014 7:07 am Post subject: |
|
|
I gave a simplified example. ($absnum a complex chain....)
It does not solve the problem.
There is data to be written as "data" and not a variable. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Mar 14, 2014 3:42 pm Post subject: |
|
|
Is the $absnum value assigned in you PHP script by $smarty->assign() or is it calculated in the template.
As I said because of the variable cache_id the call of $_smarty_tpl->getSubTemplate() is executed in nocache mode. If $absnum is calculated within template code the calculation must run also a nocache section {nocache} ...{/nocache} or $absnum must be assigned by the PHP script. |
|
Back to top |
|
ihorko Smarty n00b
Joined: 13 Mar 2014 Posts: 4
|
Posted: Mon Mar 17, 2014 7:25 pm Post subject: |
|
|
This example does not work:
index.php
Code: | $article = null;
if (!$smarty->isCached($template, $id)) {
/* db query, query, .... ....*/
$article = array( /*mysql*/ );
$smarty->assign('article', $article);
} |
index.tpl
Code: | {%nocache%}
{%assign var=absnum value=$article.absnum nocache%}
{%/nocache%}
{%include file="vote.tpl" absnum=$absnum сache_id="block|vote_"|cat:$absnum%}
|
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Mar 17, 2014 8:04 pm Post subject: |
|
|
It does not work because 'article' is assigned only if the cache is not valid.
$absnum must be assigned or build by something which will be assigned always if cache is valid or not valid.
But for my understanding, what is the idea of caching the vote.tpl with a special cache_id when $absum can change only when the main template cache is not valid. I can't understand the logic of it.
Why don't you just include it without cache_id so that it's content gets cached in the main template.
Code: | {%include file="vote.tpl"%} |
|
|
Back to top |
|
ihorko Smarty n00b
Joined: 13 Mar 2014 Posts: 4
|
Posted: Thu Mar 20, 2014 11:22 am Post subject: |
|
|
Because Code: | vote.tpl cache_id=block|vote_123 | is
survey, which refers to the Article $article.absnum = 123
I can Vote, and the content in the block|vote_123 changed (percentage of survey).
After the vote, I can clear the cache ONLY unit block|vote_123
At the same time the entire page main.tpl always remains in the cache
"vote.tpl cache_id=block|vote_123" used on multiple pages and I can clear the cache ONLY unit "block|vote_123" and do not clear all cache |
|
Back to top |
|
|