Template.assign( 'foo' , 'bar' ); // foo bar
var str = Template.transform( 'foo is {%foo}' ); // str = 'foo is bar'
* This source code was highlighted with Source Code Highlighter .
< p > You have < strong > {%num} </ strong > emails </ p >
* This source code was highlighted with Source Code Highlighter .
includeTemplate( 'var' );
Template.assign( 'num' , 5);
var str = Template.transform(VAR);
* This source code was highlighted with Source Code Highlighter .
< p > You have < strong > 5 </ strong > emails </ p >
* This source code was highlighted with Source Code Highlighter .
{%if num > 0}
< p > You have < strong > {%num} </ strong > emails </ p >
{%end}
* This source code was highlighted with Source Code Highlighter .
includeTemplate( 'cond' );
str = Template.transform(COND);
* This source code was highlighted with Source Code Highlighter .
< p > You have < strong > 5 </ strong > emails </ p >
* This source code was highlighted with Source Code Highlighter .
{%if num > 0}
< p > You have < strong > {%num} </ strong > emails </ p >
{%else}
< p > Your mailbox is empty :( </ p >
{%end}
* This source code was highlighted with Source Code Highlighter .
includeTemplate( 'contra' );
Template.assign( 'num' , 0);
str = Template.transform(CONTRA);
* This source code was highlighted with Source Code Highlighter .
< p > Your mailbox is empty :( </ p >
* This source code was highlighted with Source Code Highlighter .
var emails = [
{ 'from' : 'boss@example.com' , 'subject' : 'When will you finish the project??' },
{ 'from' : 'me@example.com' , 'subject' : 'Reminder: finish the project' },
{ 'from' : 'spam@example.com' , 'subject' : 'You have WON 1000000 dollars!' }
];
Template.assign( 'emails' , emails);
includeTemplate( 'loop' );
str = Template.transform(LOOP);
* This source code was highlighted with Source Code Highlighter .
{%if emails.length > 0}
< p > You have < strong > {%num} </ strong > emails </ p >
< table >
< thead >
< tr >
< th > From </ th >
< th > Subject </ th >
</ tr >
</ thead >
< tbody >
{%for email in emails}
< tr >
< td > {%email.from} </ td >
< td > {%email.subject} </ td >
</ tr >
{%end}
</ tbody >
</ table >
{%else}
Your mailbox is empty :(
{%end}
* This source code was highlighted with Source Code Highlighter .
< p > You have < strong > 0 </ strong > emails </ p >
< table >
< thead >
< tr >
< th > From </ th >
< th > Subject </ th >
</ tr >
</ thead >
< tbody >
< tr >
< td > boss@example.com </ td >
< td > When will you finish the project?? </ td >
</ tr >
< tr >
< td > me@example.com </ td >
< td > Reminder: finish the project </ td >
</ tr >
< tr >
< td > spam@example.com </ td >
< td > You have WON 1000000 dollars! </ td >
</ tr >
</ tbody >
</ table >
* This source code was highlighted with Source Code Highlighter .
var book = {
'Alice' : 'alice@example.com' ,
'Bob' : 'bob@example.com' ,
'Carol' : 'carol@example.com'
};
Template.assign( 'book' , book);
includeTemplate( 'loop2' );
str1 = Template.transform(LOOP2);
* This source code was highlighted with Source Code Highlighter .
< table >
< thead >
< tr >
< th > Name </ th >
< th > Email </ th >
</ tr >
</ thead >
< tbody >
{%for email in book = > name}
< tr >
< td > {%name} </ td >
< td > {%email} </ td >
</ tr >
{%end}
</ tbody >
</ table >
* This source code was highlighted with Source Code Highlighter .
< table >
< thead >
< tr >
< th > Name </ th >
< th > Email </ th >
</ tr >
</ thead >
< tbody >
< tr >
< td > Alice </ td >
< td > alice@example.com </ td >
</ tr >
< tr >
< td > Bob </ td >
< td > bob@example.com </ td >
</ tr >
< tr >
< td > Carol </ td >
< td > carol@example.com </ td >
</ tr >
</ tbody >
</ table >
* This source code was highlighted with Source Code Highlighter .
var linkify = function (email) {
return '<a href="mailto' + email + '">' + email + '</a>' ;
}
includeTemplate( 'mod' );
str = Template.transform(MOD);
* This source code was highlighted with Source Code Highlighter .
< table >
< thead >
< tr >
< th > Name </ th >
< th > Email </ th >
</ tr >
</ thead >
< tbody >
{%for email in book = > name}
< tr >
< td > {%name} </ td >
< td > {%email|linkify} </ td >
</ tr >
{%end}
</ tbody >
</ table >
* This source code was highlighted with Source Code Highlighter .
< table >
< thead >
< tr >
< th > Name </ th >
< th > Email </ th >
</ tr >
</ thead >
< tbody >
< tr >
< td > Alice </ td >
< td >< a href ="mailtoalice@example.com" > alice@example.com </ a ></ td >
</ tr >
< tr >
< td > Bob </ td >
< td >< a href ="mailtobob@example.com" > bob@example.com </ a ></ td >
</ tr >
< tr >
< td > Carol </ td >
< td >< a href ="mailtocarol@example.com" > carol@example.com </ a ></ td >
</ tr >
</ tbody >
</ table >
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/54086/
All Articles