/trisquel

JS template engine

Primary LanguageJavaScriptMIT LicenseMIT

trisquel

JS template engine

Build Status

Installation

npm install trisquel --save

Usage

var data = {
  foo: 'bar',
  crash: {
    test: 'dummy'
  },
  list: ['foo', 'bar', 'foobar'],
  map: {
    hi: 'all',
    bye: 'nobody'
  }
};

template.put('partial-map', '$each{ item,key in map }[${foo}:${key}:${item}]{/}');

template.put('partial-list', '$each{ item,i in list }[${foo}:${i}:${item}]{/}');

console.log( template('$if{ foo !== \'bar\' }whoops{:}map: $include{\'partial-map\'} {/}', data) );
// returns 'map: [bar:hi:all][bar:bye:nobody]'

console.log( template('$if{ foo !== \'bar\' }whoops{:}list: $include{\'partial-list\'} {/}', data) );
// returns 'list: [bar:0:foo][bar:1:bar][bar:2:foobar]'


var i18n = {
  months: '${n} mes$if{ n > 1 }es{/}'
};
template.filter('i18n', function (key, data) {
  if( data ) {
    return template(i18n[key.trim()])(data);
  }
	return i18n[key.trim()];
});

console.log( template('${ \'months\' | i18n:{ n: 5 } }')() );
// returns '5 meses'
console.log( template('${ \'months\' | i18n:{ n: 1 } }')() );
// returns '1 mes'

Tests

travis Build Status Wercker wercker status

npm test