js.util.Template

Template manager [singleton].

Global variable

TM

Summary
js.util.TemplateTemplate 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
getget already loaded template by name and proceed it with obj
getFget already loaded parsed template function
loadfunction that takes template names and functions as parameters After templates are loaded - functions are called.

Template manager can parse different types of templates:

1.  Simple string

I don’t know usage of this, but you can make a template without logic or variables.  Maybe some views may be such.

2.  Template with {keys}

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' } );

3.  Template with noun forms

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;

4.  Logic Templates js.util.LogicTemplate

In logic templates variables and code are in double braces.

Using variables

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,

Conditions

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 supported

Cycles

Foreach — cycling through object and arrays

In cycle you have got variable `el`, it have got

  • value - value of current element
  • key - key of current element [useful for Objects]
  • current - number of current element ( starts from 1 )

__

  • last - true if element is last
  • first - true if element is first

__

  • length - length of array or object
  • count - length of array or object

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 arr

Object

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: 3

Cycling 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, 6

Accessing 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

}).text

Output:

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

Functions

parse

parse: function(name)

Parameters

namename of template
template*template, that would be joined from arguments[ 1 - inf ]

Returns

<TemplateFunction>

get

get: function(name,
obj)

get already loaded template by name and proceed it with obj

Parameters

namename of template
objobject of parameters for template

Returns

<TemplateFunctionOutput>

getF

getF: function(name)

get already loaded parsed template function

Parameters

namename of template

Returns

<TemplateFunction>

load

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

Parameters

templateName*Any count of template names
func*Any count of functions

Returns

undefined

parse: function(name)
get: function(name,
obj)
get already loaded template by name and proceed it with obj
getF: function(name)
get already loaded parsed template function
load: function( )
function that takes template names and functions as parameters After templates are loaded - functions are called.
Parse templates with logic [singleton].
Close