Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

reusing a previously loaded template

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
dawm
Smarty n00b


Joined: 01 Jun 2003
Posts: 4

PostPosted: Sun Jun 01, 2003 1:48 am    Post subject: reusing a previously loaded template Reply with quote

is there anyway to reuse a previously loaded template, i scanned the manual and the source, and either i missed it or it doesnt exist, but with the current template syntax im using, im experiencing an extra delay caused by smarty constantly reloading the same template because its used alot in the current page.

so basically im wondering if theres any way for smarty to check to see if a template is already loaded, if not load it, if so reuse it (and parse it as if it was just loaded). otherwise i guess i can deal with the extra time it takes for the page to generate, but i do believe this would be a performance gain. but maybe i missed something, please correct me if im wrong.

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


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

PostPosted: Sun Jun 01, 2003 2:57 am    Post subject: Reply with quote

Can you give an example of how it is actually being used?

Do you have Smarty setup to cache?
Back to top
View user's profile Send private message
dawm
Smarty n00b


Joined: 01 Jun 2003
Posts: 4

PostPosted: Sun Jun 01, 2003 3:35 am    Post subject: Reply with quote

basically i have 2 templates that are reused alot in my site, a block_start and a block_end. they help display my tables in a more "window'd" look. and on each template that they are needed they are included (cause i cant figure out to store them as a variable yet reparse them when needed)
for example:
Code:
{* block_start.html *}
<table border=1 width=50%>
   <tr>
      <td>{$block_start_title}</td></tr>
   <tr>
      <td>
{* block_start.html *}

---SNIP---

{* block_end.html *}
      </td>
   </tr>
</table>
{* block_end.html *}

---SNIP--

{* blocks.html *}
{section name=blockid loop=$BLOCKS}
{include file="block_start.html" block_start_title=$BLOCKS[blockid].title}
my content goes here
{include file="block_end.html"}
{/section}
{* blocks.html *}


thats a rough example of what im doing, and as you can see if i have alot of "blocks" theres a lot of include calls being done, and id like to avoid that if possible, but still keeping the multiple templates. i know i could just add the contents of block_start and block_end to the blocks template, but i dont want to have to do that (right now if i change block_start and block_end it changes the majority of my sites look&feel)

and caching is currently disabled, but i plan to use it when the site is completed more.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sun Jun 01, 2003 4:04 am    Post subject: Reply with quote

AFAIK, you are using a valid pattern for this type of process. I sometimes use a variation of this (using objects) with good results. I'm curious to see a sample of the debug window ouput to see the actual template load times.

Smarty only compiles your block templates once (which is a nice time saver) but the resultant multiple php includes in the final compiled template is at the root of your problem.

This is easily solved without changing your code by using a PHP caching accelarator. If you aren't already using one, it is a tool which tremendously speeds up php (and thus Smarty) includes.

If you don't have one installed, then IMHO unless you are just experimenting with Smarty on personal projects, a PHP accelarator is a necessity. Actually, I really think that all PHP users should use one!

See: Why scripts shouldn't be written in ONE PHP FILE! and also (PHP Accelerator + Smarty) = problems ?

If you are using one and you still think there is an issue, may I suggest that you try profiling relevant portions of the template so that you can pinpoint the hotspots? You may be interested in this plugin.

As an alternative, I think you would have to choose a different method to produce your blocks. Perhaps a process to pre-style all of your blocks so that when your main loop includes a block, it come from the Smarty cache. Hmmm. Must be late--I can't decide if that's a good idea or a bad idea right now, so take it for what its worth Smile
Back to top
View user's profile Send private message
dawm
Smarty n00b


Joined: 01 Jun 2003
Posts: 4

PostPosted: Sun Jun 01, 2003 4:15 am    Post subject: Reply with quote

Code:
news_article_small.html
   block_start.html (0.03204)
   block_end.html (0.02662)
   block_spacer.html (0.01910)
   block_start.html (0.02859)
   block_end.html (0.02701)
   block_spacer.html (0.01921)
   block_start.html (0.02871)
   block_end.html (0.02680)
   block_spacer.html (0.01923)
   block_start.html (0.02866)
   block_end.html (0.02648)
   block_spacer.html (0.01921)
block_user.html
   form_login_block.html (0.08528)
block_track_users.html
blocks.html
   block_start.html (0.02736)
   block_end.html (0.01887)
   block_spacer.html (0.01904)
   block_start.html (0.02920)
   block_end.html (0.01863)
   block_spacer.html (0.01913)
   block_start.html (0.02910)
   block_end.html (0.01873)
   block_spacer.html (0.01915)
   block_start.html (0.02909)
   block_end.html (0.01873)
   block_spacer.html (0.01913)
blocks.html
   block_start.html (0.02734)
   block_end.html (0.01877)
   block_spacer.html (0.01909)
wrapper.html (0.12264)
   header.html (0.02840)
   footer.html (0.02865)


thats the debug output from smarty, as you can see i reuse a few templates alot.

would using a db resource instead of flat files speed up the process? it could be possible to store loaded templates in a array before they are parsed, and to be looked up later on during excution to avoid a reload of the same template.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sun Jun 01, 2003 4:29 am    Post subject: Reply with quote

I posted a resource that does exactly that here. Smile You may not want to cache all of your templates though since that can be a little wasteful. Never-the-less, that's not what you want. That only caches the loading of the template file, which should only happen once anyways, since it needs only be compiled by Smarty once. I assume you are not forcing compilation, correct?

What you need is a php accelerator. Smile

NB: flat files served from a memory cache are often much faster than a db query since they don't incur socket overhead (and less function calling overhead as well).
Back to top
View user's profile Send private message
dawm
Smarty n00b


Joined: 01 Jun 2003
Posts: 4

PostPosted: Sun Jun 01, 2003 5:30 am    Post subject: Reply with quote

thanks for your help, i added a few lines of code to _read_file, it worked as expected (checked before reloading a previously loaded template) but it didnt reduce the generation time by much. but i redid some of my templates and reduced the number of repetitive includes, that shortened the time by almost half a second. again thanks for all your help, if i stumble apon something ill be sure to post it.

dawm
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    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