|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Ahri Smarty n00b
Joined: 05 Jun 2004 Posts: 2
|
Posted: Sun Jun 06, 2004 12:06 am Post subject: Variables stored inside cached templates? |
|
|
I guess this has been asked before but I couldn't find anything on it despite searching about, so I'll attempt to butter you up in the hope of getting a reasonable answer I've successfuly integrated Smarty into a framework I'm working on, and am extremely pleased with the results thus far; the ability to not only cache a page but to have dynamic portions within this cache is great, not to mention the ease of scripting for it.
So anyway, my problem is this; I want to store a timestamp, or perhaps other variables in a cached template and then use these variables in a dynamic portion of the same template. Eg. I could store the current timestamp as some variable, and every time the page is viewed the dynamic part could output something like:
<!-- Template created 1086479930 seconds after the UNIX epoch began -->
So I tried this out;
Code: | {assign var="then" value=$smarty.now}<!-- Template created {dynamic}{$then}{/dynamic} seconds after the UNIX epoch began --> |
And the first time I load the page there is no cached copy so it is processed, great stuff, we get what we'd hope for:
<!-- Template created 1086479930 seconds after the UNIX epoch began -->
Unfortunately when I next load the page I check for a cached copy and load that if it exists - as I should when caching - but $then does not exist in this copy (as I kinda expected):
<!-- Template created seconds after the UNIX epoch began -->
So I now know that Smarty doesn't store variables inside the template by default. My question then is this; does Smarty support *any* way of storing variables inside a template (ignoring the possibility of creating a function that parses the template string looking for commented vars - which I consider to be bad form). If not then oh well, it's not that important - but I'm here to learn so I figured I'd ask!
Ultimately I'd like to do something like:
Code: | {assign var="then" value=$smarty.now}<!-- Template is {dynamic}{$smarty.now-$then}{/dynamic} seconds old --> |
But this isn't possible with my current understanding.
Edit: forgot to point out that I'm using the block function smarty_block_dynamic suggested in the Smarty docs here. |
|
Back to top |
|
electr0n Smarty Rookie
Joined: 26 Mar 2004 Posts: 32 Location: Germany
|
Posted: Sun Jun 06, 2004 3:29 pm Post subject: |
|
|
$smarty.now is already a timestamp which contains the seconds from the begin of the UNIX Time till now .
Code: | <!-- Template is {dynamic}{$smarty.now}{/dynamic} seconds old --> |
|
|
Back to top |
|
Ahri Smarty n00b
Joined: 05 Jun 2004 Posts: 2
|
Posted: Sun Jun 06, 2004 8:43 pm Post subject: |
|
|
electr0n wrote: | $smarty.now is already a timestamp which contains the seconds from the begin of the UNIX Time till now .
Code: | <!-- Template is {dynamic}{$smarty.now}{/dynamic} seconds old --> |
|
I was talking about storing the timestamp in a variable when the cached template is created and then echoing that variable in a dynamic portion. Unfortunately the variable doesn't seem to be stored inside the cached template, and therefore cannot be recalled when loading said cached template.
I'm asking if there is a way to store a variable in a cached template for later recall in a dynamic portion of the same template. Alternative methods to achieve the example I gave are not useful unless this is understood and built upon. |
|
Back to top |
|
neildogg Smarty n00b
Joined: 23 Sep 2004 Posts: 1 Location: Pottstown, PA
|
Posted: Thu Sep 23, 2004 9:25 pm Post subject: Use this |
|
|
http://smarty.php.net/manual/en/caching.cacheable.php
The way I use this is that I cache one of the variables, specified in the fourth parameter. I then override it with a temporary parameter of another name. The function checks to see if the temporary parameter exists, and if it does not, it returns the cached one.
-Neil |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Sep 23, 2004 9:34 pm Post subject: |
|
|
Another option is to have your dynamic logic grab the filemtime() from the cached file (time it was created/updated) |
|
Back to top |
|
|
|
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
|