muu

DEPRECATED lightweight JS framework
git clone https://git.ce9e.org/muu.git

commit
5a1c7b1f92b694940a725ac2df4252256256d638
parent
9e04bec7fd2799047a15a32f89f860b6436cb9a8
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2015-08-23 17:24
add this special var

Diffstat

M src/muu-template.js 35 ++++++++++++++++++++++++++++-------
M test/test-template.js 3 +++

2 files changed, 31 insertions, 7 deletions


diff --git a/src/muu-template.js b/src/muu-template.js

@@ -39,6 +39,23 @@
   39    39  * });  // 'foo baz'
   40    40  * ```
   41    41  *
   -1    42  * ## Special variable `this`
   -1    43  *
   -1    44  * `this` always refers to the current context. So the following expressions
   -1    45  * are equivalent:
   -1    46  *
   -1    47  * ```
   -1    48  * muuTemplate('{{#items}}{{content}}{{/items}}', {
   -1    49  *   item: [{
   -1    50  *     content: 1
   -1    51  *   }, {
   -1    52  *     content: 2
   -1    53  *   }]
   -1    54  * });
   -1    55  *
   -1    56  * muuTemplate('{{#this}}{{this}}{{/this}}', [1, 2]);
   -1    57  * ```
   -1    58  *
   42    59  * @module muu-template
   43    60  * @param {string} template
   44    61  * @param {object} data
@@ -50,12 +67,16 @@ define(['muu-js-helpers', 'muu-dom-helpers'], function(_, $) {
   50    67     var openTag = '{{';
   51    68     var closeTag = '}}';
   52    69 
   -1    70     var getValue = function(key, data) {
   -1    71         return key === 'this' ? data : data[key];
   -1    72     };
   -1    73 
   53    74     var parseVariableTemplate = function(template) {
   54    75         var content = template.slice(2, -2);
   55    76 
   56    77         if (template.indexOf(':') === -1) {
   57    78             return function(data) {
   58    -1                 return $.escapeHtml(data[content] || '');
   -1    79                 return $.escapeHtml(getValue(content, data) || '');
   59    80             };
   60    81         } else {
   61    82             var pairs = content.split(',').map(function(pair) {
@@ -72,7 +93,7 @@ define(['muu-js-helpers', 'muu-dom-helpers'], function(_, $) {
   72    93                     var key = pairs[i][0];
   73    94                     var value = pairs[i][1];
   74    95 
   75    -1                     if (data[value]) {
   -1    96                     if (getValue(value, data)) {
   76    97                         results.push(key);
   77    98                     }
   78    99                 }
@@ -91,19 +112,19 @@ define(['muu-js-helpers', 'muu-dom-helpers'], function(_, $) {
   91   112 
   92   113         var render = function(data) {
   93   114             if (inverted) {
   94    -1                 if (data[tagName]) {
   -1   115                 if (getValue(tagName, data)) {
   95   116                     return '';
   96   117                 } else {
   97   118                     return inner(data);
   98   119                 }
   99   120             } else {
  100    -1                 if (_.isArray(data[tagName])) {
   -1   121                 if (_.isArray(getValue(tagName, data))) {
  101   122                     var result = '';
  102    -1                     for (var i = 0; i < data[tagName].length; i++) {
  103    -1                         result += inner(data[tagName][i]);
   -1   123                     for (var i = 0; i < getValue(tagName, data).length; i++) {
   -1   124                         result += inner(getValue(tagName, data)[i]);
  104   125                     }
  105   126                     return result;
  106    -1                 } else if (data[tagName]) {
   -1   127                 } else if (getValue(tagName, data)) {
  107   128                     return inner(data);
  108   129                 } else {
  109   130                     return '';

diff --git a/test/test-template.js b/test/test-template.js

@@ -38,6 +38,9 @@ define(['muu-template'], function(muuTemplate) {
   38    38             var result = muuTemplate(template, {});
   39    39             expect(result).to.equal('foo  bar');
   40    40         });
   -1    41         it('allows to refer to the full context as `this`', function() {
   -1    42             expect(muuTemplate('{{#this}}{{this}}{{/this}}', [1, 2])).to.equal('12');
   -1    43         });
   41    44         it('escapes HTML in variables', function() {
   42    45             var template = '{{asd}}';
   43    46             var result = muuTemplate(template, {asd: '<"&>'});