Java:有没有办法从Freemarker模板中获取自定义参数

时间:2018-03-16 17:38:32

标签: java freemarker

我想在我的java代码中获取模板中定义的所有自定义参数。 实施例

<pre><code><html><head><style>td {
font-family: monospace;
font-size: 12px;
}</style></head><body>
<table width="16%" border="0" align="center" cellspacing="0" cellpadding="0">
   <tr>
     <td colspan="5" align="center">${test1}</td>
   </tr>
   <tr>
    <td colspan="5" align="center">${test2}</td>
   </tr>
</table>
</body></html></code></pre>

我希望在我的java代码中获得test1和test2参数。所以我想要下面的东西

Template template = this.getTemplate(ftl);
// a, should have test1, test2 and in case template any other template then it should get parameters for that too
String[] a= template.getAttributes();

1 个答案:

答案 0 :(得分:0)

没有,在一般情况下,它是不可能的。即使AST API是公开的(但它不是),也有一些复杂因素使得很难分辨出使用了哪些数据模型变量。在更复杂的模板中,如果test1来自数据模型,或者设置在模板内,则不明显。还有#import - s和#include - s可以引用更多的数据模型变量,但是是动态链接的。

FreeMarker的方法是首先知道数据模型,然后为它编写模板。

相关问题