Clinton Gormley wrote:
One of the issues that I am finding in my templates as they become more
complex, is the problem of namespace collision, ie just remembering what
variables I have used where, and whether using this variable name is
going to cause problems at a distance.

I use PROCESS rather than INCLUDE to process other blocks, because "I
have read" that there is a significant performance cost to localising
all the variables.

Yes, that's true.  INCLUDE takes a copy of the stash (where your variables
live) before processing the template whereas PROCESS doesn't.

How bad is this?

It all depends how many variables you have defined and what type they
are.  If you have lots of variables that contain large chunks of text
then there will be significant amount of memory copying to be done.
In that case, you should use INCLUDE sparingly.

The alternative would be to use my own namespaces in naming variables,
as in : main.title.xyz

How does this perform in comparison?

Much better.  A good general rule is to have a small number of hashes
to define your variables.  I typically have something like this:

    site    # all my site-wide data
    page    # all data relating to current page
    app     # application object (if applicable)

This gives you the "namespace" protection so that a temporary variable
(like 'url', for example) doesn't overwrite a value you didn't want
overwritten (like 'page.url').

The other benefit is that cloning the stash for an INCLUDE becomes
much more efficient because you only have to copy the references to
a few top-level hashes (and any other miscellaneous temporary variables
you happen to be using at the time).

I've tended to avoid that, assuming that anything that has to access the
stash (I'm using the XS stash) is going to have an impact as it has to
decide at each level is this a hash, or an array, or a scalar, or a
method etc

Yes, that's the downside.  Each variable access becomes that little bit
slower from the extra dotop required.  But until you're absolutely
convinced that performance is a problem, I wouldn't worry about it.

Cheers
A


_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to