<p>Say you have a standard template with included (parsed) header, body, footer templates.</p>

<p>In the body template a variable like $subject is defined and you want that also displayed in the header template.</p>

<p>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)</p>

<p>In velocity it would look something like</p>

<p>set ($body=#parse("body.vm"))</p>

<p>parse("header.vm")</p>

<p>${body}</p>

<p>parse("footer.vm")</p>

<p>This however doesn't seem to work,  any thoughts on how to do this?</p>

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
.