简体   繁体   English

自省或操纵Freemarker模板

[英]Introspection or manipulation of Freemarker template

I am working with "dynamic" Freemarker templates, meaning templates that can change based on user-submitted input. 我正在使用“动态” Freemarker模板,这意味着可以根据用户提交的输入进行更改的模板。 I would like to be able to do 2 things: 我希望能够做两件事:

  1. Inspect the parsed syntax tree of a template to see what it contains, especially identifiers and ${variables}. 检查模板的语法分析树以查看其包含的内容,尤其是标识符和$ {variables}。
  2. Manipulate that parsed tree, adding/removing elements. 操作该解析的树,添加/删除元素。

Right now, I'm accomplishing this with some ugly regex and string manipulations. 现在,我通过一些丑陋的正则表达式和字符串操作来完成此操作。 It's ugly and error prone, so I'd love to take advantage of Freemarker's template representation in Java. 这很丑陋且容易出错,所以我很想利用Freemarker的Java模板表示形式。

According to the JavaDoc, Template.getRootTreeNode seems to be the beginning of what I want: I can retrieved a DOM-like parse tree of a template, an instance of TemplateElement. 根据JavaDoc,Template.getRootTreeNode似乎是我想要的开始:我可以检索模板的DOM类解析树,即TemplateElement的实例。 But many concrete implementations of TemplateElement are not public. 但是TemplateElement的许多具体实现不是公开的。 The JavaDoc doesn't even document TemplateElement. JavaDoc甚至没有记录TemplateElement。 It appears the only way I can get useful information from a TemplateElement is to write code that lives in the Freemarker package. 看来我可以从TemplateElement获得有用信息的唯一方法是编写驻留在Freemarker软件包中的代码。 Is there a Freemarker-blessed way to inspect a template? 是否有Freemarker祝福的方式来检查模板?

Ideally, we'd have proper documentation of the TemplateElement hierarchy and make its subclasses public. 理想情况下,我们将拥有有关TemplateElement层次结构的适当文档,并将其子类公开。 While that won't let me directly modify the template, at least that would let me inspect the template's elements. 虽然那不能让我直接修改模板,但至少可以让我检查模板的元素。

The following question was also asked a few years ago, with limited results: 几年前还提出了以下问题,但结果有限:

How can I introspect a freemarker template to find out what variables it uses? 如何自检freemarker模板以找出其使用的变量?

Update : 更新

I have started a Github open source project to enable introspection of the parsed Freemarker template, to allow the kind of programmatic analysis and manipulation I discussed above. 我已经启动了一个Github开源项目,以对已解析的Freemarker模板进行自省,以允许我上面讨论的那种程序分析和操纵。 Feedback is welcome: 欢迎反馈:

freemarker-introspection freemarker的内省

FreeMarker currently doesn't have a published API for inspecting templates, let alone modifying them. FreeMarker当前没有发布的用于检查模板的API,更不用说对其进行修改了。 It would be useful, but it just doesn't have it. 它会很有用,但没有它。 So, I wonder if you can attack the problem from a different angle. 因此,我想知道您是否可以从另一个角度解决问题。 You say you are generating templates dynamically, and unless the templates themselves are user-submitted, you could store the user-submitted information based on which you generate the template, rather than the template itself... or something like that. 您说您是在动态生成模板,除非模板本身是用户提交的,否则您可以存储用户提交的信息(而不是模板本身...)或模板之类的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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