[英]Apache Velocity: Which variables are available in templates?
Is it possible to get a list of variables inside the template and fill them in using the list? 是否可以获取模板中的变量列表并使用列表填充它们? I would like my users to create their templates that means i won't know before hand what variables will be available?
我希望用户创建他们的模板,这意味着我事先不知道会有哪些变量可用?
EDIT: 编辑:
In my template users will decide what gets printed. 在我的模板中,用户将决定要打印的内容。 Such as
如
$users $latest $ users $ latest
but from my application i won't know which variables are used in the template. 但是从我的应用程序中,我将不知道模板中使用了哪些变量。 I would like to get a list such as [users latest] that includes all the variables in the template so that i can fill them in according to the user spec.
我想要一个列表,例如[users Latest],其中包括模板中的所有变量,以便我可以根据用户规范进行填写。
You may be able to gather a list by rendering the template and using a ReferenceInsertionEventHandler that builds a list. 您可以通过渲染模板并使用构建列表的ReferenceInsertionEventHandler来收集列表。 The trouble with that though is if the templates have things like:
但是,问题在于模板是否具有以下内容:
#if( $foo ) $bar #else $woogie #end
Your event handler would only ever see either $bar or $woogie, not both. 您的事件处理程序只会看到$ bar或$ woogie,而不会两者都见。
This unsupported (and perhaps outdated) class may help: 这个不受支持(也许已经过时)的类可能会有所帮助:
http://svn.apache.org/viewvc/velocity/engine/branches/1.x/experimental/templatetool/ http://svn.apache.org/viewvc/velocity/engine/branches/1.x/experimental/templatetool/
I don't think there is an easy way of doing that without overriding some of velocity classes. 我认为没有覆盖某些速度类的简单方法就可以了。
Here is some options how I would do it: 这是我的一些选择:
<!--%%__VARS__%%users,latest%%__VARS__%%-->
<!--%%__VARS__%%users,latest%%__VARS__%%-->
I found this one liner, (save it as a snippet) if you can't remember it. 如果您不记得它了,我就找到了这支衬板(另存为片段)。
## #foreach($key in $context.keys) <pre> $key</pre> #end
Hope it helps ... 希望能帮助到你 ...
Oh, remove the ##
as it is commented 哦,删除注释中的
##
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.