This feature is experimental. Please share your experiences so we know what to keep or change.
This event is emitted before a template is cloned.
Listeners can use this event to integrate template engines by following these steps:
up:template:clone
event on selected <template>
or <script>
elements.event.target
) and data (event.data
).event.nodes
to a list of Node
objects representing
the template results.When no listener sets event.nodes
, Unpoly will simply parse the template's inner HTML. Any variables in event.data
will be made available for post-processing in a compiler.
We want to integrate Mustache to enable templates like this:
<script id="greeter-template" type="text/mustache"> <!-- mark-phrase "text/mustache" -->
<div id="greeter">
Hello, {{name}}!
</div>
</script>
An event handler for an integration would look like this:
up.on('up:template:clone', '[type="text/mustache"]', function(event) {
const template = event.target.innerHTML
const result = Mustache.render(template, event.data)
event.nodes = up.element.createNodesFromHTML(result)
})
Also see documentation for up.element.createNodesFromHTML()
.
The template element being cloned.
Any template variables that should be used when cloning.