|
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 |
neotruth Smarty Rookie
Joined: 02 Dec 2009 Posts: 7
|
Posted: Thu Jan 07, 2010 8:19 pm Post subject: Caching web service results and insert tag |
|
|
Greetings all,
I am working on an auto auction's website. There is a sort of 'navbar' that is on every page and displays specific information for a logged in user (i.e. info other users shouldn't see). Certain pages on the site make web service calls to the auction's application server, which usually results in a time consuming process that I'd like to prevent the users from going through as often as possible. My solution was to cache the results from one of these calls -- let's say a list of cars for one day's auction -- for a specified interval so the next time someone accesses that list, they won't have to wait.
In the past, before I used Smarty, I would accomplish this by using php's output buffer functions to cache only the list, and the rest of the page (ie the navbar) would always be dynamic. Now with smarty, I've got the navbar script included in a common footer file with {insert}, and on the page with the list, I set Smarty's caching to 1. This caches the page just fine, but the next time the page is accessed and smarty pulls from the cache, it spits out the insert code and then displays the rest of the page. This results in all the HTML output from the navbar being put at the very top of the page's code, before the doctype, opening html bracket, etc.
The list page calls Smarty's display function and passes it a template which includes a header file at the top and a footer file at the bottom, and the footer file has the {insert} for the navbar function.
What am I doing wrong? |
|
Back to top |
|
neotruth Smarty Rookie
Joined: 02 Dec 2009 Posts: 7
|
Posted: Thu Jan 07, 2010 8:27 pm Post subject: |
|
|
I've also just realized that caching two very different pages which use the same template file results on the same page being pulled from the cache for both. Is this the way it's supposed to work?
I think things might be easier if I can just cache the "middle" part of the page where the results from the web service call are being displayed. Is this possible? |
|
Back to top |
|
waro Smarty Regular
Joined: 12 Jun 2008 Posts: 38
|
Posted: Sat Jan 09, 2010 6:30 am Post subject: |
|
|
neotruth wrote: | I've also just realized that caching two very different pages which use the same template file results on the same page being pulled from the cache for both. Is this the way it's supposed to work?
I think things might be easier if I can just cache the "middle" part of the page where the results from the web service call are being displayed. Is this possible? |
HTH, http://www.smarty.net/manual/en/caching.multiple.caches.php |
|
Back to top |
|
neotruth Smarty Rookie
Joined: 02 Dec 2009 Posts: 7
|
Posted: Wed Jan 13, 2010 2:08 pm Post subject: |
|
|
Thanks, Waro. That solves the latter problem.
Any suggestions for how to cache the webservice results? All in all, it's pretty much the same as making a database call. I need to cache the page for an hour or so without caching the user-specific "nav bar". I could just brute force it with PHP's output buffer, but then I'm not sure if the page will be parsed correctly by Smarty. |
|
Back to top |
|
ChrisFah Smarty Pro
Joined: 02 Nov 2009 Posts: 159 Location: Traun, Austria
|
Posted: Thu Jan 14, 2010 3:01 pm Post subject: |
|
|
I had the same problem with nav bar.
I solved it like this:
$smarty->caching=2;
$smarty->lifetime=3600;
php code goes here
$smarty-assign('var1', 'foo'); // the cached
$smarty->fetch(template_file1, $cache_id)
$smarty->lifetime=0;
php code goes here
$smarty->assign('var2', 'bar');
$smarty->fetch('template2', $cache_id2);
$smarty->lifetime=1800;
and so on
Using $smarty->caching=2, you can select different cache lifetime for each templatefile, so you can choose a spezific time to cache. lifetime=0 means all followinf templatefiles will not be cached until lifetime is set again.
Regards, Chris |
|
Back to top |
|
neotruth Smarty Rookie
Joined: 02 Dec 2009 Posts: 7
|
Posted: Tue Jan 19, 2010 8:00 pm Post subject: |
|
|
Thanks, Chris. This solution works for me. Now if I can figure out how to get a "loading" message to display while the web service call takes place on the first page load... |
|
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
|