blueimp/JavaScript-Templates

CDATA

Closed this issue · 3 comments

<script id="foobar" type="text/html">
  //<![CDATA[
    code / template
  //]]>
</script>

if you use it without CDATA it will not validate as xhtml.
I my self use html5 where it doesn't matter, but some people do care about that, and so does the framework I want to use it with.
Is there a chance this script will support the use of templates with // in it?
(So far the template output is messed up, it doesn't output anything between start and the first variable in the template)

Since CDATA is not required in HTML5 script sections I will probably not include such a feature in the templates engine itself. There are also different ways how developers actually use the CDATA declaration in combination with different comment tags.

However, in scenarios where the CDATA section is required (XML) you can load the templates the following way:

window.tmpl.cache['tmpl-example'] = document.getElementById('tmpl-example').innerHTML
    .replace('//<![CDATA[', '').replace('//]]>', '');

And then just use it the usual way:

var result = window.tmpl('tmpl-example', data);

awwww :/
would have been nice if the template system handled it
but thanks for the example.

As a supplement, you can also override the tmpl.load method to always replace CDATA sections:

tmpl.load = function (id) {
    return document.getElementById(id).innerHTML
        .replace('//<![CDATA[', '')
        .replace('//]]>', '');
};

And then load templates the usual way:

var result = tmpl('some-id', some-data-object);