Apache speed: what variables are available in templates?
Is it possible to get a list of variables inside a template and fill them with a list? I would like my users to create their templates, which means I won't know what variables will be available?
EDIT:
In my template, users will decide what gets printed. For example,
$ users $ last
but from my application, I will not know what variables are used in the template. I would like to get a list like [users latest] that includes all the variables in the template so that I can populate them according to the custom specification.
source to share
You might be able to collect the list by dropping the template and using the ReferenceInsertionEventHandler, which builds the list. The problem is that while templates have things like:
#if( $foo ) $bar #else $woogie #end
Your event handler will only see $ bar or $ woogie, not both.
This unsupported (and possibly deprecated) class might help:
http://svn.apache.org/viewvc/velocity/engine/branches/1.x/experimental/templatetool/
source to share
I don't think there is an easy way to do this without overriding some of the speed classes.
Here are some options for how I would do it:
- Add all the possible variables (I'm assuming there is a predefined set of them). If this is high performance look into caching.
- Ask users what data they need before creating the template (if it's one time, just a form, if these variables don't change often write them in db).
- Ask users to provide a list of the variables they need in some specific format inside the template for easy analysis of the rendering of the template, for example:
<!--%%__VARS__%%users,latest%%__VARS__%%-->
- Use regexp to locate the template file and find instances of $ var, which can be tricky.
source to share