Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Cache and config variables don't mix

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


Joined: 01 Oct 2012
Posts: 14

PostPosted: Mon Oct 01, 2012 8:16 pm    Post subject: Cache and config variables don't mix Reply with quote

Hello,
Let's start from this thread.
http://www.smarty.net/forums/viewtopic.php?t=21547

That said, I'm using the latest smarty version and I believe that I found a more general version of the bug.
Let's put the following code in example.php:
Code:

$layout = new Smarty();
$layout->setTemplate_dir ('templates/');
$layout->setCompile_dir('templates_c/');
$layout->setConfig_dir  ('config/');
$layout->setCache_dir   ('cache/');
$layout->setCompile_check(false);
$layout->setCaching(Smarty::CACHING_LIFETIME_SAVED);
$layout->setConfig_booleanize(false);
$layout->config_overwrite=false;
$layout->assign('logged',false, true);
//or, as well: $layout->assign('logged',false);
$layout->setCacheLifetime(-1); //this page never expires
$layout->display('base.tpl');


and in base.tpl:
Code:

{config_load 'it.conf' section='common'}
<html>
<head>
<title>{#default_page_title#}</title>
</head>
<body>
{nocache}
{if $logged===true}
{#welcome#} <a href="logout.php">{#logout#}</a>
{else}                               
<form  method="post">                       
{#email#}
<input type="text" name="email">
{#password#}
<input type="password" name="password">
<input type="submit" value="{#login#}">
</form>
{/if}
{/nocache}
</body>
</html>


and in it.conf:
Code:

[common]
default_page_title='mytitle'
email='email'
password='password'
login='login'
welcome='hello '
logout='logout'


let's query:
http://localhost/example.php

the page shows as expected.

let's now click on the login button or refresh the page:

the page shows, but without config variables!
Note that "config load" is not in the nocache section, but outside.
Reading the forum I guess that for the thread cited above there is no solution at the moment. Maybe, being this bug more general, is there
a Solution?

Thanks
Have a nice day
Martino
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Oct 01, 2012 10:38 pm    Post subject: Reply with quote

You are using config variables inside and outside {nocache} sections.
As {config_load} is outside {nocache} the config vars are not loaded when the page is refreshed.

In such case you could load the config file not from within the template but in the PHP script with $layout->configLoad('it.conf','common').
See http://www.smarty.net/docs/en/api.config.load.tpl

Or you must use in the template {config_load} twice, one outside and once inside the {nocache}.
But this will work only with $layout->config_overwrite=true;
Back to top
View user's profile Send private message
martinello
Smarty Rookie


Joined: 01 Oct 2012
Posts: 14

PostPosted: Tue Oct 02, 2012 9:10 am    Post subject: Cache and config variables don't mix Reply with quote

Hello,
Thanks for the reply.
Let's say that I delete the nocache tags and assign the logged variable as non cacheable, as I did. The problem persists.
This is because the logged variable is in a {if} tag and, since the variable is non-cached, the {if} block itself is not cached also and we get to the same situation with the nocache block.
From my point of view the purpose of non cacheable blocks is to allow refreshing dynamic data, keeping static the rest of the page.
In this sense, config files are static and should remain static even if included in a non cacheable block.
I had a look to the compiled .php files that smarty produces and understood the following, briefly:
if a block is non cacheable, the cached version of the template contains for that block the same instructions that the compiled version has. SO, if a config variable is in a non cached block, the cached version of the template will contain get_config_vars, like in the compiled version. The difference is, as you say, that in the cached version config_load is not executed, because the config file itself is cached.
Config variables used out of non cached blocks, are normally shown.
Resuming, my point of view is that config variables should be threated as html tags, I mean that they should be expanded even if they are in a non cached block, because they are static.
In other words, I find that a non cached block in the cached version of a template shouldn't call the get_config_vars function to retrieve config variables, but it should have the variable already expanded, as in a cached block.
What is your idea?
Have a nice day,
Martino
U.Tews wrote:
You are using config variables inside and outside {nocache} sections.
As {config_load} is outside {nocache} the config vars are not loaded when the page is refreshed.

In such case you could load the config file not from within the template but in the PHP script with $layout->configLoad('it.conf','common').
See http://www.smarty.net/docs/en/api.config.load.tpl

Or you must use in the template {config_load} twice, one outside and once inside the {nocache}.
But this will work only with $layout->config_overwrite=true;
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Oct 02, 2012 2:29 pm    Post subject: Reply with quote

it's not true that the config vars can always be cached.
For example the config value may be involved in nocache calculation.

Some people use the config vars for language dependent stuff, where everything is cached, but the config file name is dynamic.

In your application they could be staict, but we have to cover all use cases.

Again, if you use config vars in both cached and not nocache blocks don't use {config_load} in the template but $smarty->configLoad() in your php script
Back to top
View user's profile Send private message
martinello
Smarty Rookie


Joined: 01 Oct 2012
Posts: 14

PostPosted: Tue Oct 02, 2012 2:49 pm    Post subject: Cache and config variables don't mix Reply with quote

Hello,
I see your idea.
For the config values used in nocache calculations, it's actually, as you say, a design choice that, in my opinion, breaks the separation between presentation and logic, because caching depends on logic, not on presentation and smarty config files should be used for presentation purposes.
But this is not the point, let's see the thing from another perspective.
I use configLoad in my php as you say, it works. But in the cached version of the template I always have a call to getconfigvars, instead of the config variable value.
Can you tell me where should I modify the smarty code to change its behaviour in a direct way?
I mean, without use pre filtering or post filtering output. In other words, what should I modify in the smarty source code to obtain the following:
when smarty compiles the template and produces the cached version, in all cases config variables have to be expanded, even if they are in nocache blocks.
Thank's for your cooperation,
have a nice day
Martino
U.Tews wrote:
it's not true that the config vars can always be cached.
For example the config value may be involved in nocache calculation.

Some people use the config vars for language dependent stuff, where everything is cached, but the config file name is dynamic.

In your application they could be staict, but we have to cover all use cases.

Again, if you use config vars in both cached and not nocache blocks don't use {config_load} in the template but $smarty->configLoad() in your php script
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Oct 03, 2012 5:30 am    Post subject: Reply with quote

Arg....

I did forget that exactly for reasons like this the {nocache} tag is excluded from block structure checking.

The following should work
Code:
{config_load 'it.conf' section='common'}
<html>
<head>
<title>{#default_page_title#}</title>
</head>
<body>
{nocache}
{if $logged===true}
{/nocache}
{#welcome#} <a href="logout.php">{#logout#}</a>
{nocache}
{else}                               
{/nocache}
<form  method="post">                       
{#email#}
<input type="text" name="email">
{#password#}
<input type="password" name="password">
<input type="submit" value="{#login#}">
</form>
{nocache}
{/if}
{/nocache}
</body>
</html>
Back to top
View user's profile Send private message
martinello
Smarty Rookie


Joined: 01 Oct 2012
Posts: 14

PostPosted: Wed Oct 03, 2012 7:10 am    Post subject: Cache and config variables don't mix Reply with quote

Great!
forum Members, please forgive me if I classified it as a bug at the beginning. A syntax like this is counterintuitive for me and I didn't think to try something like this, because in a programming language, a syntax as:
{tag}{if condition}{/tag}
xxx
{tag}{/if}{/tag}
is generally not allowed, because tags have to be properly nested.
The key, as you say, is that {nocache} isn't subject to block checking.
I suggest to add this affirmation, together with this solution as an example, to the documentation in the caching section.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Bugs 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