{{#each}}
?is a block expression, that iterates through items of passed Array or through properties of passed Object.
The following additional variables are available inside of this helper:
@index
?- index number of the item. For arrays only
@first
?- equal to?true?for the first item in array. For arrays only
@last
?- equal to?true?for the last item in array. For arrays only
@key
?- name of current object property. For objects only
{{#if}} helper renders content if passed context is not "false" (or "undefined" or "null" or "" or "0") , otherwise it renders inverse content that optionally could be passed to {{else}} expression inside of helper:
{{#unless}} helper renders content if passed context is "false" (or "undefined" or "null" or "" or "0") , otherwise it renders inverse content that optionally could be passed to {{else}} expression inside of helper:
{{#with}} helper changes rendering context to the passed context:
If you pass a block expression with helper name that is in the expression context, then it will work like {{#each}} helper for this context if it is an Array, and will work like {{#with}} helper if it is an Object:
This plain helper will join Array items to single string with passed delimiter
This plain helper returns escaped HTML string. It escapes only the following characters:?< > " &
This inline helper allows to execute some simple JavaScript directly in template to modify/check context on the fly or for some JS calculations
Block helper for easier compares of context variables. It renders content if JavaScript expression is not "false" (or "undefined" or "null" or "" or "0") , otherwise it renders inverse content that optionally could be passed to {{else}} expression inside of helper
Template7 allows to register custom helpers with the following method:
Template7.registerHelper(name, helper)
Helper function could accepts as many arguments as required, arguments could be context, strings and hash data.
Let‘s look how to register helper on example of simple {{#if}} helper:
Or on example of plain {{join}} helper:
Or we can create helper to create?Framework7‘s list-block?link to work with this syntax:
输出
Note, that all custom helpers should be registered before you compile templates with these helpers!
Template7 allows to remove custom helpers with the following method:
Template7.unregisterHelper(name)
Template7 also supports global context which is accessible from any context.
We can specify it in?Template7.global
?property:
To access it in templates we need to use?{{@global}}
?variable:
Sometimes we may need to access to initially passed root context in our templates. For this case we need to use?{{@root}}
?variable. This is especially helpful when we are deep in context:
Template7 allows to reuse template using through partials. Partials are normal usual Template7 templates that may be called by other templates.
We can register and unregister partials using the following methods:
Template7.registerPartial(name, template)?- register partial
Template7.unregisterPartial(name)?- unregister partial
Then we can use our partials using special helper?{{> "partialName"}}
Template:
Register partial:
Apply to the template this context:
And we will get the following output:
We can even use partials to make recursive templates, like nested comments:
And the output will be:
Template7 is fast and you can make it even faster in your apps. The slowest part (but still very fast in T7) in compilation/rendering process is the compilation from string to pure JS function when you do?Template7.compile()
. So don‘t compile the same templates multiple times, one time will be enough:
移动端安卓和IOS开发框架Framework7教程-Template7 示例
原文:http://zaixianshouce.iteye.com/blog/2307757