TemplateFunctionThis interface

An object used to render plugin defined template functions, passed as this when the implementing function is called.

Construction

TemplateFunctionThis object are automatically created by the templates rendering system. The objects may be reused for efficiency, but your implementation must make no assumptions.

Interface

property functionName

The name of the function being rendered.

function write(string)

Write the string, escaped for the current context.

function deferredRenderBlock(blockName)

Return a deferred render for the named block, or null for the anonymous block.

Where you’re using a template with render(), this allows you to include blocks from the template which uses your function.

For example, with this template function:

P.globalTemplateFunction("example:link-with-block-title", function(object) {
    this.render(P.template("example-link").deferredRender({
        href: object.url(),
        title: this.deferredRenderBlock()
    }));
});

example-link is a template in the defining plugin:

<a href=href>
  render(title)
</a>

And the function can be used in any other plugin like this:

<p>
  example:link-with-block-title(object) {
    <i> "Title: " </i>
    object.title
  }
</p>

function hasBlock(blockName)

Return true if the function has been called with a given named block, or null for the anonymous block.

function getAllNamedBlockNames()

Returns an array of all the names of the named blocks, in the order they appear in the template.

The anonymous block is not included in this array. If the array is non-empty, then the existence of the anonymous block is implied, but if it’s empty, an explicit hasBlock(null) is required.

function writeBlock(blockName)

Write the given named block, or null for the anonymous block.

function renderIncludedTemplate(template)

Render the given template (objected from P.template()), in the context of this function. The template will use the current view, and can yield to any blocks, just like included templates.

function render(thing)

Render something. thing may be a deferred render or a template.

function unsafeWriteHTML(html)

Output the html without any escaping. Use with care.

function assertContext(context)

Assert that the function is being called in the right HTML context. context may be "TEXT" or "ATTRIBUTE_VALUE".

property view

The current view at the point this template function was invoked.

Use with caution, and prefer to pass all the information you need as arguments. Use of the view property creates implicit behaviour and makes it hard to reason about your function.