Freemarker 变量定义引用另一个变量
我正在使用 Freemarker (FMPP) 来自定义文本文件。我想要一个定义插值数据的属性文件,如下所示:
VAR1=some-value
VAR2=${VAR1}
这与我的实际文件高度简化,但保留了我的用例的本质。该模板包含 ${VAR2}
形式的引用,我希望它会导致 some-value
被插值。相反,插值是文字 ${VAR1}
。
请注意,这与 Can a freemarker interpolation contains an interpolation? 不同,后者指的是使用变量的值作为变量的名称变量(间接引用)。另外,can freemarker do secondary replacement的解决方案涉及修改模板。我希望替换在概念上发生在处理模板“之前”,以便模板只能引用 ${VAR2}
而无需了解双重插值。
FreeMarker 有没有办法实现这一点?
如果没有,谁能告诉我 Velocity 是否可以轻松做到这一点?
I'm using Freemarker (FMPP) to customize a text file. I want to have a properties file defining the interpolation data as follows:
VAR1=some-value
VAR2=${VAR1}
This is highly simplified from my actual files but retains the essence of my use-case. The template contains a reference of the form ${VAR2}
, which I expect to result in some-value
being interpolated. Instead, the interpolated value is the literal ${VAR1}
.
Note that this is not the same as Can a freemarker interpolation contain an interpolation?, which refers to using a variable's value as the name of a variable (indirect reference). Also, the solution to can freemarker do second replacement involves modifying the template. I'd like the substitution to happen conceptually 'before' the template is processed so the template can refer only to ${VAR2}
and not need to be aware of the double interpolation.
Is there a way to accomplish this in FreeMarker?
If not, can anybody tell me if Velocity will do this easily?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
.properties
文件中引用的变量是否始终来自同一个.properties
文件?如果是这样,那么您只需编写一个自定义 FMPPDataLoader
即可在加载文件时执行您需要的所有替换。 (或者,tdd
数据加载器也可以使用get(varname)
以及一些eval(...)
-s 实现类似的功能,但这对于这个目的来说可能太冗长了。)在模板语言级别上解决这个问题时,需要将
${...}
的输出重新解释为模板片段,然后我也假设它,直到有输出中没有任何内容看起来像模板语言构造。我不知道有任何模板语言可以做到这一点。但是,如果需要此操作的字符串使用自定义TemplateScalarModel
实现进行包装,只要读取字符串的值,该实现就会重复执行此操作,那么在 FreeMarker 中就可以解决这个问题。 (要在 FMPP 中使用此类自定义TemplateModel
,您还需要一个自定义 FMPP 数据加载器,它将在将字符串返回到 FMPP 之前以这种特殊方式包装字符串。)Are the variables that you refer to in the
.properties
file always coming from the same.properties
file? If so, then you could just write a custom FMPPDataLoader
that does all the substitution you need, right when the file is loaded. (Or, thetdd
data-loader can also achieve something similar usingget(varname)
and maybe someeval(...)
-s, but that's probably too verbose for this purpose.)As of solving this on the template-language level, it would require that the output of a
${...}
is re-interpreted as a template fragment, and then I assume the out of that too, until there's nothing in the output that looks like a template language construct. I don't know about any template language that does this. However, it's solvable in FreeMarker if the strings where this is required are wrapped with a customTemplateScalarModel
implementation that does this repeated evaluation whenever something reads the value of the string. (To use such a customTemplateModel
in FMPP, you need a custom FMPP data-loader too, which will wrap the strings in this special way before returning them to FMPP.)