dw.template
Class Velocity
dw.template.Velocity
This class renders an Apache Velocity template. For Velocity template syntax, see
Velocity 1.7.
The render() methods identify the template to render from:
- a template file name, which is resolved in the Dynamic WebDAV file location for the current site. Template file names must end with either '.vm' or '.vs'.
- a dw.io.File object, which can point to any file system location that is accessible from a script
- a string that holds the template content directly
#parse
or #include
) are always resolved
in the Dynamic location, and it is not possible to provide an absolute path.
On the target side of rendering, by default the render() methods write to the current response writer. When needed,
a dw.io.Writer
can be supplied as a target.
Parameters for rendering can be passed as a single object holding the parameters as properties.
To create a URL, pass the URLUtils
class.
To access localized strings, pass the Resource
class:
var urlUtil = require('dw/web/URLUtils');
velocity.render("$url.abs('Foo-Bar','cgid',$res.msg('key')", {'url' : urlUtil, 'res' : dw.web.Resource});
The complete set of VelocityTools
are provided to the template. You can use the tools to escape dynamic data, format text, and for other common tasks.
Template files are cached for different amounts of time, depending on the instance type.
Constructor Summary
This class does not have a constructor, so you cannot create it directly.
Method Summary
static remoteInclude(action
:
String, namesAndParams
:
String...)
:
String
Includes the rendered content of the specified action URL, which usually is a pipeline or controller.
static render(templateContent
:
String, args
:
Object)
:
void
Renders an inline template to the response writer.
static render(templateContent
:
String, args
:
Object, writer
:
Writer)
:
void
Renders an inline template to the provided writer.
static renderTemplate(templateFileName
:
String, args
:
Object)
:
void
Renders a template file to the response writer.
static renderTemplate(templateFileName
:
String, args
:
Object, writer
:
Writer)
:
void
Renders a template file to the provided writer.
static renderTemplate(templateFile
:
File, args
:
Object)
:
void
Renders a template file to the response writer.
static renderTemplate(templateFile
:
File, args
:
Object, writer
:
Writer)
:
void
Renders a template file to the provided writer.
Methods inherited from class
Object
assign, create, create, defineProperties, defineProperty, entries, freeze, fromEntries, getOwnPropertyDescriptor, getOwnPropertyNames, getOwnPropertySymbols, getPrototypeOf, hasOwnProperty, is, isExtensible, isFrozen, isPrototypeOf, isSealed, keys, preventExtensions, propertyIsEnumerable, seal, setPrototypeOf, toLocaleString, toString, valueOf, values
Method Detail
remoteInclude
Includes the rendered content of the specified action URL, which usually is a pipeline or controller. Must only be used inside a Velocity
template, such as
$velocity.remoteInclude('Product-Show','sku','42')
Parameters:
action
-
the URL (pipeline or controller) to be included
namesAndParams
-
several strings with name=value pairs , e.g.: 'pid', 'value1', 'cgid', value2'.
Returns:
the string that needs to be added to the Velocity template to execute the remote include
render
Renders an inline template to the response writer.
Parameters:
templateContent
-
the template content
args
-
the argument object
render
Renders an inline template to the provided writer.
Parameters:
templateContent
-
the template content
args
-
the argument object
writer
-
the target writer
renderTemplate
Renders a template file to the response writer.
Parameters:
templateFileName
-
the file name of the template, relative to the current sites Dynamic file location. For example, for a file at a location similar to:
https://mydomain.com/on/demandware.servlet/webdav/Sites/Dynamic/SiteGenesis/myfile.vm
render the file in this way: velocity.renderTemplate("myfile.vm", null);
args
-
the argument object
renderTemplate
Renders a template file to the provided writer.
Parameters:
templateFileName
-
the file name of the template, relative to the current sites Dynamic file location. For example, for a file at a location similar to:
https://mydomain.com/on/demandware.servlet/webdav/Sites/Dynamic/SiteGenesis/mydir/myfile.vm
render the file in this way: velocity.renderTemplate("mydir/myfile.vm", null, result);
args
-
the argument object to pass to the template
writer
-
the target writer