Velocity #parse but assign it to a variable
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.
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)
In velocity it would look something like
set ($body=#parse("body.vm"))
parse("header.vm")
${body}
parse("footer.vm")
This however doesn't seem to work, any thoughts on how to do this?
Asked by: Lily531 | Posted: 23-01-2022
Answer 1
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.
A third method is simply to put the #parse within quotes:
set ($body="#parse('body.vm')")
Within a #set, anything in double quotes is evaluated. Strings within single quotes are passed in literally.
Answered by: Marcus569 | Posted: 24-02-2022Answer 2
You can do this using VelocityLayoutServlet which is part of 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_content
declaration, e.g:
<html>
<head>
<title>$subject</title>
</head>
<body>
#parse("header.vm")
$screen_content
#parse("footer.vm")
</body>
</html>
VelocityLayoutServlet
will evalulate the templates (and, hence, variables) before rendering which allows you to set a $subject
variable in your body template, e.g:
#set($subject = "My Subject")
<div id="content">
</div>
More detailed information can be found in the Velocity documentation.
Answered by: Rafael745 | Posted: 24-02-2022Answer 3
If I understand you correctly, you want to have a Velocity variable named $subject
interpolated into the header.vm
and the body.vm
templates. Right now, the variable is defined in the body.vm
template, so you cannot refer to it in the earlier template header.vm
.
Why don't you abstract out the definition of $subject into its own template snippet, called globals.vm
say, then include that in the top-level template. So you'd have:
#parse("globals.vm")
#parse("header.vm")
#parse("body.vm")
#parse("footer.vm")
Answered by: Stuart422 | Posted: 24-02-2022
Similar questions
java - Velocity does not resolve variable in foreach loop
I have very simple velocity template:
<html>
<head>
<title>Velocity template</title>
</head>
<body>
#foreach($p in $products)
$p.name
#end
</body>
</html>
and code that processes it:
VelocityEngine engine = new VelocityEngine();
engine.init();
Template t = engin...
java - How to work with velocity context variable as local variable
In velocity i have a problem. Let me explain clearly as below
I have declared in class
private final static int MAX_TOOL_TITLE_LENGTH = 20;
//Putting into context
context.put("maxToolTitleLength","MAX_TOOL_TITLE_LENGTH");
Now in vm file i am setting the context variable value as different
#if(true)
#set($maxToolTitleLength=99)
#end
In the above li...
apache - Java velocity vm file #set use boolean variable
We can not use Boolean variable by #set when I find velocity project guidelines in Apache official site, but it is also worked when I use in my project.
#set($isRight=true)
#if($isRight)
##execute
#end
#set($isRight=false)
#if($isRight)
##not execute
#end
I want to know whether #set Boolean variable is supported by velocity and the way I use whether is legal....
java - How to use Struts 2 with Velocity and Tiles
Has anyone been able to get velocity and tiles working with struts 2?
I am having some problem finding examples or tutorials online and from what I have gathered from mailing lists it seems it might not even be possible at all (but the mails were quite old).
java - How to access static members in a Velocity template?
I'm not sure if there is a way to do this in Velocity or not:
I have a User POJO which a property named Status, which looks like an enum (but it is not, since I am stuck on Java 1.4), the definition looks something like this:
public class User {
// default status to User
private Status status = Status.USER;
public void setStatus(Status status) {
this.status = status;
}
...
java - Error in velocity and log4J
I built a webapp that works perfectly fine in my localhost (tomcat). But when I tried to deploy, velocity crashes in init(), leaving me with this strange stack trace here (sorry for the size):
ERROR [main] (VelocityConfigurator.java:62) - Error initializing Velocity!
org.apache.velocity.exception.VelocityException: Failed to initialize an instance of org.apache.velocity.runtime.log.Log4JLogChute with the ...
java - Is there an Apache Velocity plugin for Netbeans 6.5 or higher?
I'm looking for a plugin similar to the Eclipse plugins Veloeclipse or Velocity Web Edit for NetBeans.
Update: I tried the Velo...
java - How to use String as Velocity Template?
What is the best way to create Velocity Template from a String?
I'm aware of Velocity.evaluate method where I can pass String or StringReader, but I'm curios is there a better way to do it (e.g. any advantage of creating an instance of Template).
java - Useful velocity gadgets
There are lots of interesting JSP tag libraries.
I wanted to know what good Velocity gadgets or libraries or resusable components are out there.
Please one per answer.
java - JSP with Struts 1.2 from a velocity user
We have a mature application running struts 1.2 and velocity and I need to covert a page from a vm to a jsp.
So I modified my struts-config to change the forward to a new JSP file and in the JSP I try to display some data assigned to the form bean but all the form properties show empty in the JSP. When I look at the form itself I see that they are different objects. So somehow the form bean I used in my Action is n...
java - session scope in velocity
How can I use session scope in VELOCITY (in view part am using sample.vm like that).
My requirement is when I login into a page, I want to store the user's name & some details in session and if I press logout I want to clear all the information in that session.
java - Where does Velocity search for the template?
I need to use Velocity from Java-code in a web-application (I use it as mail-templates processor).
So, I have a standard code:
VelocityEngine ve = new VelocityEngine ();
try {
ve.init ();
Template t = ve.getTemplate (templatePath);
...
} catch (Exception e) {
throw new MailingException (e);
}
This code always throws the ResourceNotFoundException. Where s...
java - How do I display velocity vectors of different pixels of an image over the image?
How do I display velocity vectors of different pixels of an image over the image?
I have an array of vectors( vx and vy for each pixel). I want to display velocity vectors(by arrows of corresponding magnitude and direction). I need to know how to use the velocity vector data( 2 arrays vx and vy) to a graph/chart/image showing arrows. This is part of work to display the output of optical flow(image processing)
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)