View previous topic :: View next topic |
Author |
Message |
lastexile Smarty Rookie
Joined: 03 Jan 2008 Posts: 15
|
Posted: Sat Sep 12, 2009 6:25 am Post subject: Using CACHE and logged in/logged out states on webpage |
|
|
Hi, I have a basic question that you guys should be able to answer pretty well. It's a real world scenario.
Lets say I am using cache and $smarty->force_compile = false;
What happens if the page changes depending on whether someone is logged in or not?
Am I still suppose to use cache or always force compile to true? Or is there another way?
Thanks
Scott |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sat Sep 12, 2009 12:04 pm Post subject: |
|
|
In production systems force_compile should always be false. It controls compiling of templates and has nothing to do with caching.
If you have different content depending on somebody is logged in or not and you want caching use must use a cache_id with your display calls to keep the two versions separated in the cache. |
|
Back to top |
|
lastexile Smarty Rookie
Joined: 03 Jan 2008 Posts: 15
|
Posted: Sat Sep 12, 2009 7:20 pm Post subject: |
|
|
Thanks for the reply!
Hmm... still confused. My settings are like this:
Code: | $smarty->caching = 2;
$smarty->cache_lifetime = 7200;
$template_cache_id =
if (!$smarty->is_cached('index.tpl')) {
do something
}
$smarty->display('header.tpl');
$smarty->display('index.tpl');
|
The header part is dynamic. So if someone is logged in, it shows "Hi Scott". Otherwise it's "Log In".
How would I make the header not cache?
U.Tews wrote: | In production systems force_compile should always be false. It controls compiling of templates and has nothing to do with caching.
If you have different content depending on somebody is logged in or not and you want caching use must use a cache_id with your display calls to keep the two versions separated in the cache. |
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sat Sep 12, 2009 8:10 pm Post subject: |
|
|
One way, move the display code:
Code: | $smarty->display('header.tpl');
$smarty->caching = 2;
$smarty->cache_lifetime = 7200;
$template_cache_id =
if (!$smarty->is_cached('index.tpl')) {
do something
}
$smarty->display('index.tpl'); |
|
|
Back to top |
|
lastexile Smarty Rookie
Joined: 03 Jan 2008 Posts: 15
|
Posted: Sat Sep 12, 2009 9:15 pm Post subject: |
|
|
Ah ha... Yes.. that works for me.
Thanks!
Scott
mohrt wrote: | One way, move the display code:
Code: | $smarty->display('header.tpl');
$smarty->caching = 2;
$smarty->cache_lifetime = 7200;
$template_cache_id =
if (!$smarty->is_cached('index.tpl')) {
do something
}
$smarty->display('index.tpl'); |
|
|
|
Back to top |
|
yoyo Smarty n00b
Joined: 14 Sep 2009 Posts: 1
|
Posted: Mon Sep 14, 2009 7:36 am Post subject: additional general questions about cache usage |
|
|
I'm very new to smarty and I've got a few questions about cache, they sound similar to this issue and this is why I don't open a new thread.
In my case, I have a layout.tpl template which includes the equivalent of header.tpl, then index.tpl.
Is there a way for me to have header.tpl dynamic while index.tpl is not ?
Of course, I only have $smarty->display('layout.tpl'); in my code.
Another one :
I am designing a web "application" more than a web "site".
Imagine I've got a page displaying a user profile.
I'd like such a page in the cache so that it won't systematically query the DB for read-only display. I guess the $cacheid requires to contain my $profileid to make different pages from a profile to another.
Then, this profile page may be viewed by several kind of users :
- unauthenticated user (public view)
- authenticated user
-- not profile owner (same as public view)
-- profile owner (editable view is then required)
-- a 'friend', which can see some private details
Upon owner modification of his profile, I guess I have to clear_cache($cacheid) so that the display will be refreshed for next readers... right ?
Also, it is possible to view the page in different languages..
so my $cacheid should contain
- $pagetype ('profile' for instance)
- $profileid
- $userkind
- $lang
yet sounds complicated for such a simple example.. I hope it won't get in trouble for more complex situations... is it 'normal' that I have to worry about this $cacheid structure which is probably going to be different for each page type of my website ? (I'd prefer it generic, automatic)
Are there documentation pages, explaining the best practice for using the smarty cache in such real world situations ? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Sep 14, 2009 3:08 pm Post subject: |
|
|
Yoyo there is no general answer to this. It depends very much on the application.
If you have dynamic content just in header.tpl the best approach is the example of mohrt. Call display just for the header.tpl with caching disabled and then enable caching for the main template file.
You can disable caching also for sections of a template. See example 14-11 in the documentation how to do this.
Caching the complete pages by user may not make too much sense if you have a lot of users. But as I said before it depends much on your application. |
|
Back to top |
|
|