Using Velocity in Tagtypes

If you want to use Velocity in your tagtypes you have to use at least 2 parts of the tagtype in the right manner.

1 Basic Usage

  1. Add a Velocity part. Do not hide it and don’t make it editable.
  2. Add any HTML part, set “template” as the key and hide it

Note: Make sure that the Velocity part is not editable. Editable Velocity parts are not supported. It could lead to side effects and there is no possibility that users can edit the Velocity part anyway.

2 Render Directives

In the velocity template, two directives will be available for rendering other parts or tags.

Name Description Usage example
#gtx_render Renders the specified part or tag not editable (even if in edit mode) #gtx_render(“text”), #gtx_render($cms.tag.parts.text), #gtx_render($cms.page.tags.content)
#gtx_edit Renders the specified part or tag editable (if in edit mode) or not editable (if not in edit mode) #gtx_edit(“text”), #gtx_edit($cms.tag.parts.text), #gtx_edit($cms.page.tags.content)

3 GIS Directive

The directive #gtx_gis can be used to include resized images in velocity tags. See “GenticsImageStore”: for details about this directive.

4 Channel Directive

The directive #gtx_channel can be used to render its body in the context of another channel, which is for example useful to create links to another channel.

Example

#gtx_channel($cms.tag.parts.channel)
<a href="$cms.tag.part.pageurl">$cms.tag.parts.text</a>
#end

5 Defining Macros

There are three possible ways to define macros:

5.1 Inline Macros in Templates

Define the macro directly in the template, which will use it. The drawback of this approach is, that macros can not be reused in other tags in a proper manner.

Note: Starting with Gentics Content.Node 5.2.5, inline macros will only be callable in the template and not interfere with macros from other tags.

5.2 Global Macro Library

For correctly reusing macros, they must be defined in a global macro library.

/Node/etc/node.conf

$VELOCITY_PROPERTIES["resource.loader"] = "string,file"; 
$VELOCITY_PROPERTIES["file.resource.loader.path"] = "/Node/etc/"; 
$VELOCITY_PROPERTIES["file.resource.loader.cache"] = "true"; 
$VELOCITY_PROPERTIES["file.resource.loader.modificationCheckInterval"] = "60"; 
$VELOCITY_PROPERTIES["velocimacro.library"] = "velocimacro-lib.vm"; 

You define a single file /Node/etc/velocimacro-lib.vm (which should be readable for user node) to contain the global macros. Those macros will automatically be available to all Velocity Tags. With the above settings, changes to the velocimacro-lib.vm will be available after up to 60 seconds.

After changing the /Node/etc/node.conf file, the Tomcat must be restarted in order for the setting to take effect.

See the Velocity Developer Guide for details about the velocity configuration settings.

5.3 Macro Library in a pagetag

The third possibility to reuse macros is to create e.g. a special page, that contains the macros in a tag. Then add a third tagpart with keyword “macros” and parttype “Tag (page)” to the Velocity Tag, and let it link to the tag of the macro library page. In this way, the macros can be maintained in a global place, but will be used as inline macros in the tags.