Преглед изворни кода

Add dot notation support for section and inverted section tags

tags/0.5.1-vsc
Sahab Yazdani пре 14 година
родитељ
комит
b2aed8f86f
2 измењених фајлова са 30 додато и 17 уклоњено
  1. +21
    -17
      mustache.js
  2. +9
    -0
      test/unit.js

+ 21
- 17
mustache.js Прегледај датотеку

@@ -345,6 +345,23 @@ var Mustache = (function(undefined) {
return undefined; return undefined;
} }
function find_with_dot_notation(name, context) {
var name_components = name.split('.'),
i = 1, n = name_components.length,
value = find_in_stack(name_components[0], context);
while (value && i<n) {
value = find(name_components[i], value);
i++;
}
if (i!==n && !value) {
value = undefined;
}
return value;
}
/* END Run Time Helpers */ /* END Run Time Helpers */


function text(state, token) { function text(state, token) {
@@ -369,26 +386,13 @@ var Mustache = (function(undefined) {
implicit_iterator = (state.pragmas['IMPLICIT-ITERATOR'] || {iterator: '.'}).iterator; implicit_iterator = (state.pragmas['IMPLICIT-ITERATOR'] || {iterator: '.'}).iterator;
state.send_code_func((function(variable, escape) { return function(context, send_func) { state.send_code_func((function(variable, escape) { return function(context, send_func) {
var variable_components,
i, n, value;
var value;
if ( variable === implicit_iterator ) { // special case for implicit iterator (usually '.') if ( variable === implicit_iterator ) { // special case for implicit iterator (usually '.')
value = {}; value[implicit_iterator] = context[context.length-1]; value = {}; value[implicit_iterator] = context[context.length-1];
value = find(variable, value); value = find(variable, value);
} else { } else {
variable_components = variable.split('.');
i = 1;
n = variable_components.length;
value = find_in_stack(variable_components[0], context);
while (value && i<n) {
value = find(variable_components[i], value);
i++;
}
if (i!==n && !value) {
value = undefined;
}
value = find_with_dot_notation(variable, context);
} }


if (value!==undefined) { if (value!==undefined) {
@@ -456,14 +460,14 @@ var Mustache = (function(undefined) {
if (s.inverted) { if (s.inverted) {
state.send_code_func((function(program, variable){ return function(context, send_func) { state.send_code_func((function(program, variable){ return function(context, send_func) {
var value = find_in_stack(variable, context);
var value = find_with_dot_notation(variable, context);
if (!value || is_array(value) && value.length === 0) { // false or empty list, render it if (!value || is_array(value) && value.length === 0) { // false or empty list, render it
program(context, send_func); program(context, send_func);
} }
};})(program, s.variable)); };})(program, s.variable));
} else { } else {
state.send_code_func((function(program, variable, template, partials){ return function(context, send_func) { state.send_code_func((function(program, variable, template, partials){ return function(context, send_func) {
var value = find_in_stack(variable, context);
var value = find_with_dot_notation(variable, context);
if (is_array(value)) { // Enumerable, Let's loop! if (is_array(value)) { // Enumerable, Let's loop!
for (var i=0, n=value.length; i<n; ++i) { for (var i=0, n=value.length; i<n; ++i) {
context.push(value[i]); context.push(value[i]);


+ 9
- 0
test/unit.js Прегледај датотеку

@@ -172,6 +172,15 @@ test("Dot Notation", function() {
), ),
'5' '5'
); );

equals(
Mustache.to_html(
'{{#a.b.c}}{{d}}{{/a.b.c}}',
{ a: { b: function() { return { c: [ {d: 'a'}, {d: 'b'}, {d: 'c'} ] } } } },
{}
),
'abc'
);
}); });






Loading…
Откажи
Сачувај