开发者

Freemarker variable definition referencing another variable

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. 开发者_StackOverflow中文版 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?


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 FMPP DataLoader that does all the substitution you need, right when the file is loaded. (Or, the tdd data-loader can also achieve something similar using get(varname) and maybe some eval(...)-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 custom TemplateScalarModel implementation that does this repeated evaluation whenever something reads the value of the string. (To use such a custom TemplateModel in FMPP, you need a custom FMPP data-loader too, which will wrap the strings in this special way before returning them to FMPP.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜