Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Selective cache or nested smarty code

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


Joined: 26 Jul 2006
Posts: 7
Location: Poland, land o' the duck brothers.

PostPosted: Wed Jul 26, 2006 3:17 am    Post subject: Selective cache or nested smarty code Reply with quote

Hi,

I've looked at the forum archives for a while and haven't found a solution, so here's my problem:

I'm displaying a part of the page which is a search form - one text input, two selects. Selectible values are taken from database (DB_DataObjects), then placed in an array, and finally placed in an option list, like this one:

{html_options values=$regionCodes output=$regionNames}

Okay, nice so far. But the template (page header) is cached, and I want it cached, but there's just one thing I want to leave dynamic - form field values. The form has method="GET". I want my form to display the values from $_GET when I submit the form. I wrote myself and registered a tiny function:

function getvar($params, &$smarty) {
$varname = $params['varname'];
return isset($_GET[$varname])?$_GET[$varname]:'';
}

$smarty->register_function('getvar', 'getvar', false, array('varname'));

So in smarty I call like: <input type="text" value="{getvar varname=searchPhrase}"/>

The problem appears with {html_options}. Setting a value there is done via a function parameter, selected=

I tried {html_options ..... selected={getvar varname=searchID}}, but this nesting doesn't work

When I put my {getvar...} call into a {capture} block and save the output to $searchID, it gets cached no matter what. I don't want to leave the whole <select> uncached, cause it will give me extra sql queries that I wanted to avoid. Can you think of any other way to put one function's output into other function's parameter in Smarty?

I know I could solve my issue with javascript, since javascript gives me access to my url's query string, but I wanted to do it in an elegant way, leaving javascript for things like onclick and xmlhttprequest.

{eval} is of no use here, since it's results get cached anyway. How nice would it be if the <select> tag had a "selected=" parameter Wink

Thanks in advance,

owczi
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Jul 26, 2006 6:05 am    Post subject: Reply with quote

See if this is of help to you: http://smarty.php.net/manual/en/caching.cacheable.php and if you are still having troubles, be sure to ask again Smile

NOTE: you will have to manually load and register plugins (including plugins like html_options) for this to work for you.

HTH
Back to top
View user's profile Send private message
owczi
Smarty Rookie


Joined: 26 Jul 2006
Posts: 7
Location: Poland, land o' the duck brothers.

PostPosted: Wed Jul 26, 2006 10:47 am    Post subject: Reply with quote

Well you see that I used what you pointed in the manual. As to the manual plugin registering, I'd have to disable caching for the whole {html_options function, and if I wanted that, I'd just make a {dynamic block or something. I want to cache the whole {html_options except for ONE parameter, which is selected=. It works with <input type=text, where I can simply put a value="{smarty block}"

Is it doable at all in Smarty? I believe it is, just still wondering how Wink
Back to top
View user's profile Send private message
owczi
Smarty Rookie


Joined: 26 Jul 2006
Posts: 7
Location: Poland, land o' the duck brothers.

PostPosted: Wed Jul 26, 2006 11:13 am    Post subject: Reply with quote

Haha, I could us ae cache id based on the query string Wink This would solve my problem and give me about 400 cached files after some time ;]
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Jul 26, 2006 3:46 pm    Post subject: Reply with quote

owczi wrote:
Well you see that I used what you pointed in the manual. As to the manual plugin registering, I'd have to disable caching for the whole {html_options function, and if I wanted that, I'd just make a {dynamic block or something. I want to cache the whole {html_options except for ONE parameter, which is selected=. It works with <input type=text, where I can simply put a value="{smarty block}"

Is it doable at all in Smarty? I believe it is, just still wondering how Wink


Did you try using $cache_attrs? At anyrate, it seems unsensical -- you want to cache it except for a given parameter, but the thing is, even if there is only one parameter changing, you will need to re-run the entire function...so you can't cache it in that sense....but you seem to see the way in your last post where you show that you want to control it by a cache_id based on that parameter. That could work.
Back to top
View user's profile Send private message
owczi
Smarty Rookie


Joined: 26 Jul 2006
Posts: 7
Location: Poland, land o' the duck brothers.

PostPosted: Wed Jul 26, 2006 9:59 pm    Post subject: Reply with quote

Apparently I was just too sleepy to think, now I see it... It is in fact impossible not to loop over the whole option set to do the check where selected="selected" should be, and thus re-run the whole function (and a general conclusion about feature-rich opensource gizmos - if some naturally nifty feature isn't implemented, it's probably not quite possible to do it Wink ). Actually I only needed a quick peek at smarty's sources to get it.

But since I got two select fields in that part of the page, one with 16 options (list of regions in my country) and the second one with about 20 options (category list), that would give me 320 cached template instances, about 2k each. And this is a trivially low amount of diskspace, especially compared to other cached files' amounts. I'll push it on a nice separate reiserFS partition - perfect for lots of small files, and leave it this way (as long as the whole project doesn't finally land on FreeBSD). Maybe a little dirty, but then again who cares these days when diskspace is just a matter of a 'plop!' sound of another hotswap bay being stuffed into the array.

Anyway, I just became another smarty lover out there. Lighttpd + php-fcgi + smarty + little AJAX magic + XHTML is the ultimate source of everlasting joy Smile

Though I'm thinking of a separate database trigger-based project to automatically clear smarty's cache whenever it's needed because of changes in database content. Yay, this is fun.
Back to top
View user's profile Send private message
owczi
Smarty Rookie


Joined: 26 Jul 2006
Posts: 7
Location: Poland, land o' the duck brothers.

PostPosted: Wed Jul 26, 2006 10:12 pm    Post subject: Reply with quote

There, works like a charm Smile
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Jul 27, 2006 7:08 am    Post subject: Reply with quote

excellent Smile keep coming by and telling us your stories, problems and solutions Wink
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