''updated to version 2.1.0

[{TableOfContents}]

!!!ztemplates-render

Is the ztemplates-module responsible for creating the output of your application. This is done by transforming (rendering) a [render-pojo|RenderPojo] to a String. The rendering is done by a [ZRenderEngine].

!!How to use

!Define a render-pojo

Is a annotated Java class. The [@ZRenderer|ZRenderer] annotation specifies the [Renderer] class to use. 
Return values from Java Bean getters anotated with [@ZExpose|ZExpose] will get exposed to the renderer instance under the name of the Java Beans property.

If annotated with @ZExpose(render=true), the getter return value will automatically be rendered itself by the framework and the resulting String will be
exposed to the Renderer. The Renderer sees only the pojo class (to get its  annotations) and the exposed values as a Map<String, Object>.


HelloWorldVelocity.java

{{{

package org.ztemplates.homepage.render.helloworld.velocity;

import org.ztemplates.renderer.ZExpose;
import org.ztemplates.renderer.ZRenderer;
import org.ztemplates.renderer.velocity.ZVelocityRenderer;

@ZRenderer(ZVelocityRenderer.class)
public class HelloWorldVelocity
{
  /**
   * This one gets exposed to the context as
   * a property called message, because it is
   * annotated with ZExpose
   */
  @ZExpose
  public final String message = "Hello World!";
}
}}}

!Define a template

Create HelloWorldVelocity.vm in the same directory as HelloWorldVelocity.java

{{{
<html>
From Velocity template:

<h1 class="mystyle">${message}</h1>

</html>
}}}

!Define a stylesheet

It's recommended to put the styling in a [css stylesheet|Css]. 

Create a file HelloWorldVelocity.css in the same directory as HelloWorldVelocity.java

{{{

  .mystyle {
     font-style: italic;
  }

}}}

Access the style as ztemplates.css. All css styles found in the classpath corresponding to classes annotated with [@ZRenderer] are aggregated into one stylesheet: ztemplates.css

{{{

<html>
<head>
   <link rel="stylesheet" type="text/css" href="ztemplates.css">
</head>

...

}}}

To avoid name collisions, [since 0.9.8.6|Changelog] you can use ${cssId} in the stylename. This is replaced at runtime with a unique identifier, so the style does not collide with styles from other css-fragments.

{{{

  .${cssId}-mystyle {
     font-style: italic;
  }

}}}

{{{

<html>
<head>
   <link rel="stylesheet" type="text/css" href="ztemplates.css">
</head>
<body>
From Velocity template:

<h1 class="${cssId}-mystyle">${message}</h1>

</body>
</html>

}}}


!Use it


render to String

{{{

String s = ZTemplates.getRenderService().render(pojo);

}}}

or render to http-response

{{{

ZTemplates.getServletService().render(pojo);

}}}

or render to template, from the template

{{{

${renderService}.render(${pojo})

}}}

See [Render Pojo] for more details.