java Velocity #parse 但将其分配给变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/214094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Velocity #parse but assign it to a variable
提问by
Say you have a standard template with included (parsed) header, body, footer templates.
假设您有一个标准模板,其中包含(已解析)页眉、正文、页脚模板。
In the body template a variable like $subject is defined and you want that also displayed in the header template.
在正文模板中定义了一个像 $subject 这样的变量,并且您希望它也显示在标题模板中。
In some other template languages like HTML::Mason(perl based) you would evaluate the body template first to pick up the $subject variable but store it's output temporarily in a variable so your final output could end up in the correct order (header, body, footer)
在其他一些模板语言中,例如 HTML::Mason(perl based),您将首先评估主体模板以获取 $subject 变量,但将其输出临时存储在一个变量中,以便您的最终输出可以以正确的顺序结束(标题,正文,页脚)
In velocity it would look something like
在速度上它看起来像
set ($body=#parse("body.vm"))
设置 ($body=#parse("body.vm"))
parse("header.vm")
解析(“header.vm”)
${body}
${body}
parse("footer.vm")
解析(“footer.vm”)
This however doesn't seem to work, any thoughts on how to do this?
然而,这似乎不起作用,关于如何做到这一点的任何想法?
回答by Will Glass
Either of the two solutions above would work. The VelocityLayoutServlet solution requires an extra package (also from Velocity) called Velocity Tools. I'm partial to this approach (and variants) myself.
上述两种解决方案中的任何一种都可以。VelocityLayoutServlet 解决方案需要一个名为 Velocity Tools 的额外包(也来自 Velocity)。我自己偏爱这种方法(和变体)。
A third method is simply to put the #parse within quotes:
第三种方法是简单地将#parse 放在引号内:
set ($body="#parse('body.vm')")
Within a #set, anything in double quotes is evaluated. Strings within single quotes are passed in literally.
在#set 中,评估双引号中的任何内容。单引号内的字符串按字面传递。
回答by Olly
You can do this using VelocityLayoutServletwhich is part of VelocityTools.
您可以使用VelocityLayoutServlet来完成此操作,它是VelocityTools 的一部分。
This allows you to define a layout for your application -- let's call it application.vm-- in which you can parse in headers, footers etc and declare where the main body content is placed using the screen_contentdeclaration, e.g:
这允许您为您的应用程序定义一个布局——让我们称之为application.vm——您可以在其中解析页眉、页脚等并使用声明声明主体内容的放置位置screen_content,例如:
<html>
<head>
<title>$subject</title>
</head>
<body>
#parse("header.vm")
$screen_content
#parse("footer.vm")
</body>
</html>
VelocityLayoutServletwill evalulate the templates (and, hence, variables) before rendering which allows you to set a $subjectvariable in your body template, e.g:
VelocityLayoutServlet将在渲染之前评估模板(以及变量),这允许您$subject在正文模板中设置一个变量,例如:
#set($subject = "My Subject")
<div id="content">
</div>
More detailed information can be found in the Velocity documentation.
更多详细信息可以在 Velocity 文档中找到。
回答by Dov Wasserman
If I understand you correctly, you want to have a Velocity variable named $subjectinterpolated into the header.vmand the body.vmtemplates. Right now, the variable is defined in the body.vmtemplate, so you cannot refer to it in the earlier template header.vm.
如果我理解正确,您希望将一个名为 Velocity 的变量$subject插入到header.vm和body.vm模板中。现在,该变量是在body.vm模板中定义的,因此您不能在之前的模板中引用它header.vm。
Why don't you abstract out the definition of $subject into its own template snippet, called globals.vmsay, then include that in the top-level template. So you'd have:
为什么不将 $subject 的定义抽象到它自己的模板片段中,称为globals.vmsay,然后将其包含在顶级模板中。所以你会有:
#parse("globals.vm")
#parse("header.vm")
#parse("body.vm")
#parse("footer.vm")

