[Templates] Order of evaluation

Andrew Beattie andrew@tug.com
Thu, 21 Nov 2002 07:57:53 -0000


Is there a flag or option to ask Template Toolkit to evaluate all of the
variables passed to it *first*, before it does anything else?

Since that sounds like an odd request, klet me explain what I'm doing...

I'm using template toolkit to display a table.

1) I have an array of strings, which are column names.

2) I have an array of hashes that will contain the body of the table.

The key to the hashes in (1) is the column names (2)

The function that populates the body (2) sparsely populates the array of
hashes (2), then checks for any columns which are completely empty.
If a column is empty, I remove the column name from
the array of column names (1) (because I don't want TT to display empty
columns.

I pass to TT:
 - A reference to the array of column names (1)
 - A reference to the function that builds the body (2)

In the template, use the array (1) to build the table header of column names,
then I use the array (1) to key into the array of hashes (2) to build the
table body.  Finaly I use the array again to build a table footer.

The problem is that the array of hashes(2) isn't evaluated until it's needed.
Since a side-effect of evaluating the array of hashes is to splice lumps
out of the array of column names, I find that my header has the original,
complete list of all possible column names, while the body and footer only
includes the columns actualy required.

Now, I can work round this by making a dummy reference to the body early on
in my template, but this seems very ugly, so again my question:

Is there a flag or option to ask Template Toolkit to evaluate all of the
variables passed to it *first*, before it does anything else?