JsRender Demos
Example Scenario: Inserting "and" and "," separators between words
Example 1: Expressions in tags, and template parameters ({{if}} tag):
{{for languages ~count=languages.length}}
...
{{if #index === ~count-2}} and {{else #index < ~count-2}}, {{/if}}
...
{{/for}}
Example 2: Expressions in tags, and template parameters (ternary expression):
{{for languages ~count=languages.length}}
...
{{: #index === ~count-2 ? " and " : #index < ~count-2 ? ", " : ""}}
...
{{/for}}
Example 3: Custom helper functions:
{{for languages}}
...
{{if ~nextToLast()}} and {{else ~notLast()}}, {{/if}}
...
{{/for}}
Using "allowCode"
Note: The allowCode feature is powerful, but leads to poor separation of concerns, and poor maintainability.
It is therefore only available as an opt-in feature on a per template basis.
The following two examples illustrate its use, but are not the recommended approach. The built-in expression support,
custom tags, helper functions etc. provide a better solution for almost all scenarios, as in the two examples above.
Example 4: allowCode, for program flow - with if(...) { ... }:
$.templates( "movieTmpl", {
markup: "#movieTemplate",
allowCode: true
});
{{*
if ( myexpression ) {
}}
...
{{*
}
}}
Example 5: allowCode, for returning content - with ternary expression:
$.templates( "movieTmpl", {
markup: "#movieTemplate",
allowCode: true
});
{{*: myexpression ? ... : ...}}