Przeglądaj źródła

Add dot notation support for interpolation tags

tags/0.5.1-vsc
Sahab Yazdani 14 lat temu
rodzic
commit
519f23ff54
2 zmienionych plików z 67 dodań i 17 usunięć
  1. +28
    -17
      mustache.js
  2. +39
    -0
      test/unit.js

+ 28
- 17
mustache.js Wyświetl plik

@@ -319,6 +319,7 @@ var Mustache = (function(undefined) {
} }
var value; var value;
if (is_kinda_truthy(context[name])) { if (is_kinda_truthy(context[name])) {
value = context[name]; value = context[name];
} }
@@ -364,9 +365,32 @@ var Mustache = (function(undefined) {
escape = prefix = true; escape = prefix = true;
} }
var variable = get_variable_name(state, token, prefix, postfix);
var variable = get_variable_name(state, token, prefix, postfix),
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 value = find_in_stack(variable, context);
var variable_components,
i, n, value;
if ( variable === implicit_iterator ) { // special case for implicit iterator (usually '.')
value = {}; value[implicit_iterator] = context[context.length-1];
value = find(variable, value);
} 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;
}
}

if (value!==undefined) { if (value!==undefined) {
if (!escape) { if (!escape) {
value = escapeHTML('' + value); value = escapeHTML('' + value);
@@ -422,25 +446,12 @@ var Mustache = (function(undefined) {
} }
function section(state) { function section(state) {
// by @langalex, support for arrays of strings
function create_context(_context) {
if(is_object(_context)) {
return _context;
} else {
var ctx = {},
iterator = (state.pragmas['IMPLICIT-ITERATOR'] || {iterator: '.'}).iterator;
ctx[iterator] = _context;
return ctx;
}
}
var s = state.section, template = s.template_buffer.join(''), var s = state.section, template = s.template_buffer.join(''),
program, program,
new_state = create_compiler_state(template, state.partials, state.openTag, state.closeTag); new_state = create_compiler_state(template, state.partials, state.openTag, state.closeTag);
new_state.metrics = s.metrics; new_state.metrics = s.metrics;
new_state.pragmas = state.pragmas;
program = compile(new_state); program = compile(new_state);
if (s.inverted) { if (s.inverted) {
@@ -455,7 +466,7 @@ var Mustache = (function(undefined) {
var value = find_in_stack(variable, context); var value = find_in_stack(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(create_context(value[i]));
context.push(value[i]);
program(context, send_func); program(context, send_func);
context.pop(); context.pop();
} }


+ 39
- 0
test/unit.js Wyświetl plik

@@ -136,6 +136,45 @@ test("Basic Variables", function() {
}); });


test("Dot Notation", function() {
equals(
Mustache.to_html(
'{{a.b.c}}',
{ a: { b: { c: 0 } } },
{}
),
'0'
);

equals(
Mustache.to_html(
'{{a.b.c}}',
{ a: { b: {} } },
{}
),
''
);

equals(
Mustache.to_html(
'{{a.b.c}}',
{ a: { b: 0 } },
{}
),
''
);

equals(
Mustache.to_html(
'{{a.b.c}}',
{ a: { b: function() { return { c: 5 } } } },
{}
),
'5'
);
});


test("'{' or '&' (Unescaped Variable)", function() { test("'{' or '&' (Unescaped Variable)", function() {
// matches unescaped.html // matches unescaped.html
equals( equals(


Ładowanie…
Anuluj
Zapisz