From 437056a7e76b2e374607c08cdc6be39411160850 Mon Sep 17 00:00:00 2001 From: Alexander Lang Date: Sat, 17 Oct 2009 21:01:33 +0200 Subject: [PATCH] added processing arrays of plain values --- examples/array_of_strings.html | 1 + examples/array_of_strings.js | 1 + examples/array_of_strings.txt | 1 + mustache.js | 16 ++++++++++++++-- test/mustache_spec.rb | 14 +++++++++----- test/mustache_test.js | 14 -------------- 6 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 examples/array_of_strings.html create mode 100644 examples/array_of_strings.js create mode 100644 examples/array_of_strings.txt delete mode 100644 test/mustache_test.js diff --git a/examples/array_of_strings.html b/examples/array_of_strings.html new file mode 100644 index 0000000..38fa5ab --- /dev/null +++ b/examples/array_of_strings.html @@ -0,0 +1 @@ +{{#array_of_strings}} {{.}} {{/array_of_strings}} \ No newline at end of file diff --git a/examples/array_of_strings.js b/examples/array_of_strings.js new file mode 100644 index 0000000..12c4992 --- /dev/null +++ b/examples/array_of_strings.js @@ -0,0 +1 @@ +var array_of_strings = {array_of_strings: ['hello', 'world']}; diff --git a/examples/array_of_strings.txt b/examples/array_of_strings.txt new file mode 100644 index 0000000..4a1f475 --- /dev/null +++ b/examples/array_of_strings.txt @@ -0,0 +1 @@ +hello world diff --git a/mustache.js b/mustache.js index 333a032..3772938 100644 --- a/mustache.js +++ b/mustache.js @@ -29,7 +29,7 @@ var Mustache = { } // keep context around for recursive calls - this.context = context = this.merge((this.context || {}), view); + this.context = context = this.merge((this.context || {}), this.create_context(view)); // first, render all sections var html = this.render_section(template); @@ -42,7 +42,19 @@ var Mustache = { // render until all is rendered return this.render_tags(html); }, - + + create_context: function(_context) { + if(this.is_object(_context)) { + return _context; + } else { + return {'.': _context}; + } + }, + + is_object: function(a) { + return a && typeof a == 'object' + }, + /* Tries to find a partial in the global scope and render it */ diff --git a/test/mustache_spec.rb b/test/mustache_spec.rb index da22d55..22310cf 100644 --- a/test/mustache_spec.rb +++ b/test/mustache_spec.rb @@ -18,11 +18,15 @@ describe "mustache" do mustache = File.read(__DIR__ + "/../mustache.js") runner = <<-JS - #{mustache} - #{view} - var template = #{template}; - var result = Mustache.to_html(template, #{testname}); - print(result); + try { + #{mustache} + #{view} + var template = #{template}; + var result = Mustache.to_html(template, #{testname}); + print(result); + } catch(e) { + print('ERROR: ' + e.message); + } JS File.open("runner.js", 'w') {|f| f << runner} diff --git a/test/mustache_test.js b/test/mustache_test.js deleted file mode 100644 index 94b0d9d..0000000 --- a/test/mustache_test.js +++ /dev/null @@ -1,14 +0,0 @@ -var a = function(s) { - return s.replace(/[&"\<>]/g, function(s) { - switch(s) { - case "&": return "&"; break; - case "\\": return "\\\\"; break; - case '"': return '\"'; break; - case "<": return "<"; break; - case ">": return ">"; break; - default: return String(s); break; - } - }); - }; - -print(a("as&<")); \ No newline at end of file