Skip to content

Latest commit

 

History

History
126 lines (94 loc) · 3.11 KB

File metadata and controls

126 lines (94 loc) · 3.11 KB
layout default
title Template Binding
subtitle <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2F%3Ca+href%3D"http://prollyfill.org/" rel="nofollow">http://prollyfill.org/" target="_blank">prollyfill</a>
feature
spec status code summary
None
<buildbot-list project="TemplateBinding"></buildbot-list>
Extends the capabilities of the [HTML Template Element](http://www.w3.org/TR/html-templates/) by enabling it to create, manage, and remove instances of content bound to data defined in JavaScript.
links

{% include spec-header.html %}

Learn the tech

Template Binding extends the <template> element with new attributes for binding data:

  1. bind
  2. repeat
  3. if
  4. ref

Basic usage

bind

{% raw %} Creates a single instance with {{ bindings }} when singleton model data is provided. {% endraw %}

repeat

{% raw %} Creates an instance with {{ bindings }} for every element in the array collection. {% endraw %}

Named scopes:

{% raw %} {{user.name}} {% endraw %}

Indexing:

{% raw %} {{ i }}:{{ j }}. {{ value }} {% endraw %}

if

{% raw %} Binds if and only if conditionalValue is truthy.

<template if="{{ conditionalValue }}">
  Binds if and only if conditionalValue is truthy. (same as *bind if*)
</template>

<template repeat if="{{ conditionalValue }}">
  Repeat if and only if conditionalValue is truthy.
</template>

{% endraw %}

ref

{% raw %} Used by any template which refers to this one by the ref attribute

<template bind ref="myTemplate">
  When creating an instance, the content of this template will be ignored,
  and the content of #myTemplate is used instead.
</template>

{% endraw %}

Activating a template

Setting data model on the template causes any bind, repeat or if attribute directive to begin acting:

document.querySelector('template').model = {...};

Unbinding a model {#nodeunbind}

Node.unbind(<property>) can be used to unbind a property. For example, to unbind a model set using the bind attribute, call template.unbind('bind'):

{% raw %} test Hello, {{who}} - {{what}}

<script>
  var t = document.querySelector('#greeting');
  var model = {
    salutations: { what: 'GoodBye', who: 'Imperative' }
  };
  t.model = model;
    
  function removeGo() {
    t.unbind('bind');
  }
</script>

{% endraw %}

Examples

Please refer to the HowTo examples.