Template manager [singleton].
TM
| js. | Template manager [singleton]. |
| Template manager can parse different types of templates: | I don’t know usage of this, but you can make a template without logic or variables. |
| Functions | |
| parse | |
| get | get already loaded template by name and proceed it with obj |
| getF | get already loaded parsed template function |
| load | function that takes template names and functions as parameters After templates are loaded - functions are called. |
I don’t know usage of this, but you can make a template without logic or variables. Maybe some views may be such.
Collects list of replacements in closure. When called — replace them from input object
TM.parse( 'test', "Hello, {username}. It's a good day, {userName}" );
TM.get( 'test', { userName: 'Sheldon' } );If you need to pass lots of values through template ( usually in cycle ), use this function. Now it can select nouns, depended on number. For example:
examples are for russian language. In russian keep in mind numbers: 1, 2, 5 to make correct forms of nouns. in English there are two forms, so keep in mind numbers 1, 2 and use only two forms like: {{catCount},cat,cats}
TM.parse( 'test', 'Вася поймал {fishes} {{fishes},рыбу,рыбы,рыб}');
TM.get( 'test', {fishes: 5} ).text;Also it can do such thing:
TM.parse( 'test', 'Вася поймал {fishes} {{fishes},рыбу,рыб,рыб} {{fishes}{fishNames}}');
TM.get( 'test', {fishes: 5, fishNames: 'осетр,осетров,осетров'} ).text;In logic templates variables and code are in double braces.
TM.parse( 'test', "Hello, {{username}}. It's a good day, {{userName}}" );
TM.get( 'test', { userName: 'Sheldon' } ).text;Output: Hello, Sheldon. It’s a good day, Sheldon
TM.parse( 'test', "Hello, {{userName}}. It's a good day, {{userName}}" );
TM.get( 'test', { user: 'Sheldon' } ).text;Output: Hello, . It’s a good day,
Ternar
TM.parse( 'test', "Hello, {{user ? user : userName}}" );
TM.get( 'test', { userName: 'Sheldon' } ).text; // Output: Hello, Sheldon
TM.get( 'test', { user: 'Sheldon' } ).text; // Output: Hello, Sheldon
TM.get( 'test', { } ).text; // Output: Hello,IF — conditional operator
TM.parse( 'test', '{{if userName == 'Sheldon'}}'+
'Hello, {{Sheldon}}'+
'{{else if userName == 'Mark'}}'+
'Hello, Mark'+
'{{else}}'+
'Hello, somebody'+
'{{/if}}'
);
TM.get( 'test', { userName: 'Sheldon' } ).text; // Output: Hello, Sheldon
TM.get( 'test', { userName: 'Mark' } ).text; // Output: Hello, Mark
TM.get( 'test', { userName: 'Peter' } ).text; // Output: Hello, somebody{{if ( ( a == 1 ) or ( b == 2) || ( c == 3 ) ) && d }} // this is also supportedForeach — cycling through object and arrays
__
__
Array
TM.parse( 'test', '{{foreach arr}}'+
'{{el.value}}'+
'{{el.last?"":","}}'+
'{{else foreach}}'+
'No elements in arr'+
'{{/foreach}}'
);
TM.get('test',{arr: [1,2,3]}).text // Output: 1,2,3
TM.get('test',{}).text // Output: No elements in arrObject
TM.parse( 'test', '{{foreach arr}}'+
'{{el.key}}: {{el.value}}'+
'{{el.last?"":", "}}'+
'{{else foreach}}'+
'No elements in arr'+
'{{/foreach}}'
);
TM.get('test',{arr:{a:1,b:3}}).text // a: 1, b: 3Cycling through arrays
To cycle through el.value you can just write foreach with no parameters
TM.parse( 'test', '{{foreach arr}}'+
'{{foreach}}'+
'{{el.value}}'+
'{{el.last ? "" : ", "}}'+
'{{/foreach}}'+
'{{el.last ? "" : "\n"}}'+
'{{/foreach}}'
);
TM.get('test',{arr:
[
[1,2,3],
[4,5,6]
]
}).text
// Output:
// 1, 2, 3
// 4, 5, 6Accessing parrents and children
You can access to parent variable, using dot (.) before variable name. Access to child is just using dot or []. See an example
TM.parse( 'test', '{{foreach People}}'+
'Name: {{name}}\n'+
'Age: {{age}}\n'+
'Can{{age >= .drinkAlcoholFrom ? "": " not"}} drink alcohol\n'+
'{{if lastVisit}}'+
'\n Last visits:'+
'{{/if}}'+
'{{foreach lastVisit}}'+
'{{"\n " + el.value}} - '+
'{{el.value >= ..drinkAlcoholFrom ? "Could drink" : "Couldnt drink"}} ( now: {{.age}})'+
'{{else foreach}}'+
'\n First visit'+
'{{/foreach}}'+
'{{!el.last ? "\n-----\n" : ""}}'+
'{{/foreach}}'+
'\n\nVasiliy is {{People[0].age}} years old'
);
TM.get('test',{
People:
[
{
name: 'Vasiliy',
age: 18
},
{
name: 'Anatolij',
age: 24,
lastVisit: [ 18, 20, 21, 22 ]
},
{
name: 'Elena',
age: 16
},
],
drinkAlcoholFrom: 21
}).textOutput:
Name: Vasiliy
Age: 18
Can not drink alcohol
First visit
-----
Name: Anatolij
Age: 24
Can drink alcohol
Last visits:
18 - Couldnt drink ( now: 24)
20 - Couldnt drink ( now: 24)
21 - Could drink ( now: 24)
22 - Could drink ( now: 24)
-----
Name: Elena
Age: 16
Can not drink alcohol
First visit
Vasiliy is 18 years old
load: function( )
function that takes template names and functions as parameters After templates are loaded - functions are called. If templates are already loaded, then functions would be called immidiatly Functions would be call with loaded templates functions as a parameter (an Array), or with just function if there was one template to load
| templateName* | Any count of template names |
| func* | Any count of functions |
undefined
parse: function( name )
get already loaded template by name and proceed it with obj
get: function( name, obj )
get already loaded parsed template function
getF: function( name )
function that takes template names and functions as parameters After templates are loaded - functions are called.
load: function( )