开发者

Velocity: How do I define a global variable

I tried adding a bunch of #set($x=abc) statements in the VM_global_library.vm file, b开发者_如何学Gout these variables aren't available in my VM templates.

I'd like to set a single global variable for things like the base path to images and such. Is this possible?


You can add that variable to the VelocityContext and then it will be available to everyone and will act as a Global Variable.


Your VM_global_library.vm should only contain Velocity macros, I suspect any variables declared outside of a macro are just ignored.

You could create a separate .vm file that holds all of your globals, and then make sure you #parse it in every template where you need them (or you could write some code to automatically parse it). I've previously extended the VelocityLayoutServlet for example to do something similar: merge my "global-variables.vm" first to add them to the Context, and then carry on and render the view.

If your globals are just simple strings it would probably be more efficient to put them in a properties file and have your code push them directly into the VelocityContext.


It's easy to set up and manage global data if you create your Context with VelocityTools.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜