Arttemplate Template Use Supplement

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.