Language functions
HSVT language template functions implement the core features necessary for useful HTML templating.
if(value) & unless(value)
A simple conditional control flow function. If value
is truthy, the anonymous block is rendered if it is, otherwise the else
block. unless()
inverts this logic.
ifAll(…), ifAny(…), unlessAll(…) & unlessAny(…)
Multiple argument versions of if()
and unless()
, which take at least two arguments. All arguments must be truthy with the All
variations, and at least one for the Any
variations.
each(value)
Where value
is an array-like object, then the anonymous block is rendered once per item in that list with the view set to that item.
Use ^{}
notation to access values in the parent context.
within(value)
The view is moved to the value
, then the anonymous block is rendered.
If the value is not defined, then nothing is rendered.
Use ^{}
notation to access values in the parent context.
switch(value)
Evaluate value
as a string, then render the named block with that name. If that block doesn’t exist, render the anonymous block.
do() { }
Render the anonymous block. yield:name()
within that block renders the named block to the do()
function.
This is useful for things like conditionally adding links around generated HTML.
template:X()
Include another template in the rendered output. The template name, X
, is part of the function name.
Templates may be a standard template, or a template defined by the plugin. Where a plugin template is in a subdirectory, the directory names are separated with :
characters.
yield() & yield:Y()
When rendering a do() anonymous block, or template which has been included in another using the template:X() function, render a block from the calling function. yield()
renders the anonymous block, yield:Y()
renders the block named Y
.
ifHasBlock() & ifHasBlock:Y() { } else { }
Test for whether a block is available, and can be rendered with yield()
. This allows you to write templates that can take optional blocks.
ifHasBlock()
tests for the anonymous block, and ifHasBlock:Y()
tests for the block named Y
.
This function behaves like an if
statement. If the block is available, the anonymous block will be rendered, otherwise the else
block.
render(deferred)
Include a deferred render into the current template, created using the deferredRender()
function on a Template
.
This is intended to prevent you from needing to use unsafeHTML()
by giving you a mechanism to safely include rendered templates.
See Deferred rendering for how to use render()
.
Use O.isDeferredRender()
to check whether an object is a deferred render.
render(convertible)
Render convertible
into the current template. convertible
must implement the toHTML()
function.
Use this to render Text
objects.
url(…)
Pseudo function with special parsing. See URLs.
concat(…)
Output all the arguments. No whitespace is output between them.
Use to override the automatic space separated values when using lists as attribute values, and building strings as arguments to template functions.
ifContent() & markContent()
The anonymous block of ifContent()
is rendered. Unless there is a markContent()
block within it which has non-empty content, the entire block is efficiently rolled back and does not appear in the rendered output. ifContent()
blocks can be nested.
These functions are typically used to avoid cumbersome if()
statements within a template.
See the full documentation for these functions.
scriptTag(…)
Pseudo function for generating <script>
tags. The arguments are a URL, as url()
, and the resulting URL is output as the src
attribute in <script src="..."></script>
tag pair.
Script tags are otherwise not allowed in templates, because including JavaScript is too dangerous and should be prohibited by your Content Security Policy.
unsafeHTML(unsafeValue)
Evaluate unsafeValue
as a string, then output directly in the template without escaping. A parse error is thrown if it is used outside the text context (eg can’t be used in an attribute value).
The value name, or one of the path elements, must begin with unsafe
so that it’s obvious in the code generating the view that the value will be used unsafely.
Try to use render()
and deferredRender()
instead.
unsafeAttributeValue(unsafeValue)
Override id
, class
and style
attribute restrictions by pretending a value is a literal string. The value is escaped within the attribute.
A parse error is thrown if it is used outside the attribute context.
The value name, or one of the path elements, must begin with unsafe
so that it’s obvious in the code generating the view that the value will be used unsafely.
Try to use a data-
attribute instead and select on that value in your JavaScript code.