Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
How to pass a template "value" variable?

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


Joined: 13 Mar 2014
Posts: 4

PostPosted: Thu Mar 13, 2014 11:32 am    Post subject: How to pass a template "value" variable? Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Thu Mar 13, 2014 8:29 pm    Post subject: Reply with quote

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
View user's profile Send private message
ihorko
Smarty n00b


Joined: 13 Mar 2014
Posts: 4

PostPosted: Fri Mar 14, 2014 7:07 am    Post subject: Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Fri Mar 14, 2014 3:42 pm    Post subject: Reply with quote

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
View user's profile Send private message
ihorko
Smarty n00b


Joined: 13 Mar 2014
Posts: 4

PostPosted: Mon Mar 17, 2014 7:25 pm    Post subject: Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Mar 17, 2014 8:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
ihorko
Smarty n00b


Joined: 13 Mar 2014
Posts: 4

PostPosted: Thu Mar 20, 2014 11:22 am    Post subject: Reply with quote

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
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