简体   繁体   English

Apache Velocity:模板中提供哪些变量?

[英]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/

Here there are some discussion and ideas to resolve this. 这里有一些讨论和想法可以解决这个问题。 The preferred option is basically implement a walker for the generated AST of a template. 首选选项基本上是为模板的生成的AST实现walker。 Not trivial. 不平凡。

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: 这是我的一些选择:

  1. Add all possible variables (I am assuming there is a predefined set of them). 添加所有可能的变量(我假设有一组预定义的变量)。 If it is performance heavy look into caching. 如果性能过高,请查看缓存。
  2. Ask users which data they need before rendering template (if it is a one time thing just a form, if those variables don't change often write them into db). 在呈现模板之前询问用户他们需要哪些数据(如果是一次性表单,那这些变量是否不变,通常将它们写入db)。
  3. Ask users to provide list of variables they need in some specific format inside the template for easy parsing before rendering a template, like: <!--%%__VARS__%%users,latest%%__VARS__%%--> 要求用户在呈现模板之前以模板的某种特定格式提供所需的变量列表,以便于解析,例如: <!--%%__VARS__%%users,latest%%__VARS__%%-->
  4. Use regexp to search the template file and look for $var instances, which could be tricky. 使用regexp搜索模板文件并查找$ var实例,这可能很棘手。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM