Re: Template pool class
| From: | Anders Janson | Date: | Tue, 20 Nov 2001 09:12:15 +0000 |
| Subject: | Re: Template pool class | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-1982@lists.php.net to get a copy of this message | ||
Ivo Jansch wrote:
> Hello Anders,
>
> anders.janson@mailbox.swipnet.se wrote:
>
> > This class is helpful if your app is like mine - you need several
> > Smarty objects concurrently,
>
> What do you mean with concurrent here? I don't think PHP is a threaded
> language, so couldn't you reuse just one Smarty object?
Maybe a bad choice of words :)
What I mean is that there are several objects in use at one time.
Now, about reusing ONE object....
I guess many of you out there have some kind of nested layout, with a
master layout template, and some more or less modular content.
In my case, even the level of nesting is unknown. All modules use one or
more templates. On top of that, every module may use submodules and so
on....
So, for each nesting level, I need at least one object. If I need two or
more, then sometimes I do not need them all at once, I could have used the
same one in my code for that level. But with a dynamic level of nesting,
using (reusing) global objects becomes messy. Which one is available? This
code helped me a lot. Whenever I need to generate output, I just grab an
object from the pool, use it, then drop it (return it to the pool).
Same line of code everywhere, and the pool will handle everything. The
number of generated smarty objects happens to be equal to the level of
nesting, most of the time, if the code is properly structured.
Example: 8 objects, nesting level 5
Traditional programming, with temporary objects in every function: Create 8
smarties.
Traditional programming, trying to use global objects: Aaaaahh.....
With this class: Just get them. The class generates 5 objects, you don't
need to know where.
The overhead generated by my class is neglectable, compared to creating new
objects every time you need them. And it helps preventing brain damage
caused by overheating, trying to manage a lot of global objects ;)
I don't expect this class to be perfect for everybody, but for complex
layouts it can be very useful.
/Anders