RE: [SMARTY] About caching

From: Date: Wed, 25 Dec 2002 22:13:24 +0000
Subject: RE: [SMARTY] About caching
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-6940@lists.php.net to get a copy of this message
Oops, mistake :) /* code */ function insert_MembersOnline() { MembersOnline(); $GLOBALS['SMARTY']->caching = 0; /* I had forgotten this! */ $output = $GLOBALS['SMARTY']->fetch(SMARTY_DEFAULT_DIR . SMARTY_FILE_MEMBERS_ONLINE); $GLOBALS['SMARTY']->caching = 1; return $output; } It is correct now? Peter -----Original Message----- From: Peter Billen [mailto:peter@clueless.be] Sent: woensdag 25 december 2002 23:04 To: 'M Mohr' Cc: smarty-general@lists.php.net Subject: RE: [SMARTY] About caching Aha! Now i understand the insert function :) This is what I've got now: /* template */ <table cellspacing=0 border=0 width=100%> <tr> <td colspan=2 class="titel">> Online members</titel></td> </tr> {insert name="MembersOnline"} </table> /* code */ function insert_MembersOnline() { MembersOnline(); return $GLOBALS['SMARTY']->fetch(SMARTY_DEFAULT_DIR . SMARTY_FILE_MEMBERS_ONLINE); } This seems to work! :) Although, I asks a little bit "goodwill" from the designers/skinners of the site. Thanks Messju! If someone still has something to add, I'm pleased to hear it. Peter -----Original Message----- From: M Mohr [mailto:messju@lammfellpuschen.de] Sent: woensdag 25 december 2002 22:32 To: Peter Billen Cc: smarty-general@lists.php.net Subject: Re: [SMARTY] About caching Hello, On Wed, Dec 25, 2002 at 09:53:53PM +0100, Peter Billen wrote: > Thanks for your reply! > > for 1) > > The problem is about the insert function is that "it's against the > principes of using templates". By using templates, you want to separate > code and html. But if you use the insert function, you have html into > your php code? your insert-function could be something like: function smarty_insert_get_user_online($params, &smarty) { return $smarty->assign('num_users', $GLOBALS['users_obj']->userOnline()); } and your template contains all the html and only the number is replaced. alternatively you can try to use $smarty->fetch() inside your insert function and return the result of fetch(). > Beside that, I don't understand for 100% how the insert function works. > Imagine my usersOnline() function is part of a class. If smarty looks > for a usersOnline() function, how can it work? I suppose Smarty calls > the function at this way: MyClass::usersOnline(). But now, the > constructor of MyClass isn't called + the vars of that class aren't set > + I don't have a database connection there + ... ? > > The insert function is a nice idea, but I don't think it's a good idea > to use it for your whole site. No work-around known? in such cases i often assign my objects with $smarty->assign_by_ref('varname', $obj) to the template(s) and then use $smarty->_tpl_vars['varname'] inside my plugin-functions (like insert-function, blocks etc.) to reference them. i don't know if that is the best and the "right" way to do it, but it works for me for quite some time. > for 2) > > I do only have one memberlist for my site and it is shown on nearly > every page. With "flushing the whole cache", you mean "deleting the > whole cache directory"? use $smarty->clear_cache() for it, see: http://smarty.php.net/manual/en/api.clear.cache.html use null for template and you whole cache gets cleared out. > The timestamp is a good idea, but then I must > hard-code a variabele like $lastModificationMemberList ? yes, i think so. > Thanks again, have fun messju > Peter > > -----Original Message----- > From: M Mohr [mailto:messju@lammfellpuschen.de] > Sent: woensdag 25 december 2002 21:24 > To: Peter Billen > Cc: smarty-general@lists.php.net > Subject: Re: [SMARTY] About caching > > Hello, > > On Wed, Dec 25, 2002 at 08:15:40PM +0100, Peter Billen wrote: > > Hi all, > > > > First of all: merry chrismas to all of you! I hope you'll have a nice > > dinner tonight ;) > > > > I have a question about caching. I've read the caching section (in the > > manual) twice, but I couldn't find an answer on my question. > > > > My whole site is based on 1 template file: index.html. In that > > index.html, I have construction like: > > > > ... > > <table ...> > > {if $smarty.get.section eq "news"} > > {include file="v1/news.html"} > > {elseif ...} > > </table> > > ... > > > > So actually, by using the include function, I have several index.html > > files. > > > > Now I wanted to enable caching on index.html, but separated for each > > situation (in this example only the news-situation). In my PHP code I > > wrote the following code: > > > > switch ($_GET['section']) > > { > > case SECTION_NEWS: require_once("news.php"); > > > > $cacheGroup = SECTION_NEWS; /* > > to separate each situation! */ > > > > if > > (!$GLOBALS['SMARTY']->checkCached($cacheGroup)) /* = > > is_cached("news.html", $cacheGroup) smarty function */ > > showNews(); > > > > break; > > > > case ... > > } > > > > ... > > > > /* display whole site */ > > $GLOBALS['SMARTY']->display("v1/index.html", $cacheGroup); > > > > Now I can easily set cache_lifetime to -1, and only delete the cached > > files when an UPDATE, INSERT or DELETE sql function is called (thanks > to > > the $cacheGroup var, I don't need to delete the whole cache directory. > I > > can only delete the files which are really changed). This works > perfect. > > > > However, I have 2 problems about this way of working with caching. I > > hope someone can help me out with this. > > > > 1) What if your cached page exists of contents that can change every > > second (so if you don't want to cache the whole template, but only > some > > blocks of it)? Imagine you have a "who is online" tracker on your > site. > > If your index.html is cached, the "who is online" tracker will not be > > "refreshed" on each click? I can't find a good solution to this > problem. > > > > 2) Imagine that you have a "member list" on each (!) page (-> on each > > cached index.html). If you add a new member to the database, you have > to > > delete ALL (!!!) cached files, since every cached page still contain > the > > old member list. How do you deal with such situations? > > > > I think the real question is: how can I enable caching on BLOCKS of a > > template file, and not on a whole template file? I've read about the > > "insert" function, but I don't see how It can be used in those 2 > > situations. I've already tried splitting up my index.html into several > > template files (such as memberlist.html, who-is-online.html) and > > including them into my index.html. But it seems Smarty still caches > them > > as a whole, and not as separated files. > > yes, smarty caches the *output* of your index.html-template, so no > cache-checks > are done for memberlist.html etc., when a valid cached version of > index.html > is found in the cache. > > i think, "insert" does the opposite of what you want. you want parts of > your (non-cached) template to be cached. "insert" makes parts of your > cached template non-cached. > > for 1): > > define an insert function that returns the html-block of your 'x users > are online'-box. so everytime your cached template is delivered, the > insert function is called and inserts the correct (non-cached) > information about the users. > > for 2): > > if you only have one memberlist for your whole site, that does not > change too often and is shown on nearly every page, then maybe > flushing the whole cache, when something canges in the memberlist is a > good solution for the problem. > > alternatively you can add a 2nd cache-group to your cache_id, so that > your $cacheGroup looks like something like 'SECTION_NEWS|[timestamp]' > with [timestamp] being the time when the most recent modification to > your memberlist was done. > > HTH > messju > > > > I hope someone can help me :) Thanks! > > > > Peter > > > > > > -- > > Smarty General Mailing List (http://smarty.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > -- > Smarty General Mailing List (http://smarty.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > > -- > Smarty General Mailing List (http://smarty.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- Smarty General Mailing List (http://smarty.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Smarty General Mailing List (http://smarty.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.smarty.general (#6940) next »