ww.parseTemplate

The parseTemplate global function allows creating client side templates that are evaluated as Javascript. The template is like a client side ASP template using <# #=> and <#= #=> for expression evalution based on the data element passed in as a parameter.

The template parser turns the template into Javascript code that is executed.

Templates can be unobtrusively stored inside of the HTML document like this:

<script type="text/html" id="script">
<div>
<b><#= content #><b>
<# for(var i=0; i < names.length; i++) { #>
   Name: <#= names[i] #> <hr />
<# } #>
</div>
</script>

And can then be called like this:

var tmpl = $("#itemtempalte").html();  // grab the template content
var data = { content: "This is some textual content",
             names: ["rick","markus"] };

//assign the content with jQuery
$("#divOutput").html( parseTemplate(tmpl,data) );
```</#=></#>
parseTemplate(templateString,data)

Parameters

template
A string representation of the template that is to be expanded. Can also be a DOM element that holds the template as its innerHtml.

data
The data item that is applied to the template. You can only pass a single item in but you can use a root object that has hierarchical children.

Remarks

Note there's a known bug in the template parser that causes problems with single quotes in the template's literal text. Try to avoid using single quotes in literal text and when you do escape it with ' (although that will not always work either).


See also:

Class global

© West Wind Technologies, 1996-2024 • Updated: 10/09/17
Comment or report problem with topic