The WIP implementation of the HTML5 <template> v2 with parse() support and part of WHATWG/HTML proposal #2254
For tests open index.html and use the test() function in DevTools. Tests, data and templates defined at the bottom of index.html.
template.js contains the implementation of the .parse(data) method on the Template prototype.
- parse() should return a single Node. outerHTML can be used to check the result string.
- Only first child element in DocumentFragment (template) is used as a template (root node). If no root node found, exception thrown.
- Current algorithm transforms first child element to an HTML string (outerHTML) and parses template as a single string
- It finds all the expressions
- If data was not found, expression should be replaced with an empty string
- It should trim() spaces, i.e. both
{{var}}
and{{ var }}
should be valid
<template id="1">
<div class="box">
<h1>{{ title }}</h1>
<p>{{ message }}</p>
</div>
</template>
document.getElementById('1').parse({title: 'Hello, World', message: 'Lorem Ipsum'});
- It should replace
{{ title }}
withHello, World
and{{ message }}
withLorem Ipsum
. - If there are no any argument specified in data (key is undefined) then according to Global 5 it should be replaced with an empty string
<template id="2">
<box>
<h1>{{ title }}</h1>
<p>{{ message }}</p>
<p>{{ title }}</p>
</box>
</template>
Same data set.
It should replace all occurrences of {{ title }}
.
It should replace nested vars
<template id="3">
<div class="box">
<h1>{{ title }}</h1>
<p>{{ message }}</p>
<div>{{ author.name }}</div>
</div>
</template>
Currently block statements (if/endif) in progress:
<template id="4">
<div class="box">
<h1>{{ title }}</h1>
<p>{{ message }}</p>
{{ if (author) }}
<div>{{ author.name }}</div>
{{ endif }}
</div>
</template>
It should parse if block and do not display it if expression is false