This is another layout sevlet that will preserve the direct streaming of the page towards the response stream without caching it into memory, built on the basis of Nathan's VelocityLayoutServlet.

The following directives are used to control the behaviour of the page:

The layout page can then use equivalent properties on the $page object: $page.layout, $page.title, $page.head, every defined $page.parameter-name, and the page template path in $page.template.

It is the responsability of the layout template writer to use these references as adequate, as in:

Please note that the arguments you pass to those directives can contain VTL, but will be evaluated before the page is reached, when the request is received. It means you cannot use values that are dynamically calculated by previous statements in the page.

Let's say you want to have the title reflect some HTTP query parameters, you can do something like: #title("Viewing: $query.document") from within the page. What you cannot do is something like: #set($title="Viewing: $query.document") & #title($title).

The following properties of velocity.properties control the behaviour of the layout servlet:

When the resource loader cache is off, the page template is parsed twice per request. Apart from a performence issue, there is also a problem with the #head directive since it works in a cumulative way. Since there is no generic way for the servlet to know whether cache is on or off, there is currently no simple workaround to this problem, which means that cache MUST be turned on for #head() to work properly.

The patch is attached. Please note that the new directives make use of the new velocity-1.5 TemplateInitException, so you will have to compile velocity-tools against velocity-1.5-dev. Otherwise you can easily adapt the sources to have it work with velocity-1.4 by replacing TemplateInitException with a generic Exception.

StreamLayoutServlet (last edited 2009-09-20 22:06:01 by localhost)