1. Adding auxiliary methods
The "Template.helper (name, callback) ' helper method is commonly used for string substitution, such as UBB substitution, swearing substitution, and so on.
For example, extend a ubb substitution method:
template.helper (' $ubb 2html ', function (content) {return content. Replace (/\[b\] ([^\[]*?) \[\/b\]/igm, '<b>$</b>'). Replace (/\[i\] ([^\[]*?) \[\/i\]/igm, '<I>$</I>'). Replace (/\[u\] ([^\[]*?) \[\/u\]/igm, '<u>$</u>'). Replace (/\[url= ([^\]]*) \] ([^\[]*?) \[\/url\]/igm, '<ahref= "$">$</a>'). Replace (/\[img\] ([^\[]*?) \[\/img\]/igm, '<imgsrc= "$" />'); });
How to use in a template:
<%= $ubb 2html (content)%>
Note: The engine does not escape HTML characters from the auxiliary method output.
2. embed sub-templates
The ' <%include (ID, [data])%> ' statement can embed a child template, where the second parameter is optional and it passes through the current data by default.
3. Do not escape HTML
The template engine default data contains HTML characters that are escaped to avoid XSS vulnerabilities, and "= =" can be used where it is not necessary to escape.
such as:<%==value%>
If you need to turn off the default escape, you can set ' Template.isescape = False '.
4. Storing templates in JS
var Source = '<ul>' + '<% for(var i= 0; I<list.length; I++) { %>' + '<Li>Index<%=I+ 1 %>:<%=List[i]%></Li>' + '<% } %>' + '</ul>'; var data = {list: [' Literary ', ' blog ', ' Photography ', ' movie ', ' Ballad ', ' Travel ', ' guitar ']}; var render = Template.compile (source), var html = render (data);d Ocument.getelementbyid (' content '). InnerHTML = html;
5. Template Coding Specification
1. You cannot use the JavaScript keyword as a template variable (including keywords added in ECMA5 strict mode):
2. The template runs in the sandbox, and external variables cannot be accessed internally unless a helper method is defined for the template. Example: template.helper (' math ', math)
All demo Examples: http://aui.github.com/artTemplate/demo/index.html
This article excerpt from: http://www.lvtao.net/web/javascript-artTemplate.html
Arttemplate Template Use Supplement