Created
April 29, 2010 15:59
-
-
Save defunkt/383818 to your computer and use it in GitHub Desktop.
Adding {{? question_sections}} to mustache.rb {{/ question_sections}}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/lib/mustache/generator.rb b/lib/mustache/generator.rb | |
| index 5e8c067..8943903 100644 | |
| --- a/lib/mustache/generator.rb | |
| +++ b/lib/mustache/generator.rb | |
| @@ -128,6 +128,17 @@ class Mustache | |
| compiled | |
| end | |
| + def on_question_section(name, content) | |
| + code = compile(content) | |
| + | |
| + ev(<<-compiled) | |
| + v = ctx[#{name.to_sym.inspect}] | |
| + if v.respond_to?(:empty?) ? !v.empty? : !!v | |
| + #{code} | |
| + end | |
| + compiled | |
| + end | |
| + | |
| # Fired when the compiler finds a partial. We want to return code | |
| # which calls a partial at runtime instead of expanding and | |
| # including the partial's body to allow for recursive partials. | |
| diff --git a/lib/mustache/parser.rb b/lib/mustache/parser.rb | |
| index e427570..c3eadab 100644 | |
| --- a/lib/mustache/parser.rb | |
| +++ b/lib/mustache/parser.rb | |
| @@ -45,7 +45,7 @@ EOF | |
| end | |
| # After these types of tags, all whitespace will be skipped. | |
| - SKIP_WHITESPACE = [ '#', '^', '/' ] | |
| + SKIP_WHITESPACE = [ '#', '^', '?', '/' ] | |
| # The content allowed in a tag name. | |
| ALLOWED_CONTENT = /(\w|[?!\/-])*/ | |
| @@ -109,7 +109,7 @@ EOF | |
| # Since {{= rewrites ctag, we store the ctag which should be used | |
| # when parsing this specific tag. | |
| current_ctag = self.ctag | |
| - type = @scanner.scan(/#|\^|\/|=|!|<|>|&|\{/) | |
| + type = @scanner.scan(/#|\^|\?|\/|=|!|<|>|&|\{/) | |
| @scanner.skip(/\s*/) | |
| # ANY_CONTENT tags allow any character inside of them, while | |
| @@ -136,6 +136,11 @@ EOF | |
| @result << [:mustache, :inverted_section, content, block] | |
| @sections << [content, position, @result] | |
| @result = block | |
| + when '?' | |
| + block = [:multi] | |
| + @result << [:mustache, :question_section, content, block] | |
| + @sections << [content, position, @result] | |
| + @result = block | |
| when '/' | |
| section, pos, result = @sections.pop | |
| @result = result | |
| diff --git a/test/mustache_test.rb b/test/mustache_test.rb | |
| index f935ff3..e3bad2e 100644 | |
| --- a/test/mustache_test.rb | |
| +++ b/test/mustache_test.rb | |
| @@ -473,4 +473,31 @@ template | |
| assert_equal value, tmpl.send(attr) | |
| end | |
| end | |
| + | |
| + def test_question_mark | |
| + view = Mustache.new | |
| + view.template = <<template | |
| +{{? people}} | |
| + <ul> | |
| + {{# people}} | |
| + <li>{{ name }}</li> | |
| + {{/ people}} | |
| + </ul> | |
| +{{/ people}} | |
| +template | |
| + | |
| + view[:people] = [ | |
| + { :name => "Chris" }, | |
| + { :name => "Bob" }, | |
| + { :name => "Mislav" } | |
| + ] | |
| + | |
| + assert_equal <<template, view.render | |
| +<ul> | |
| + <li>Chris</li> | |
| + <li>Bob</li> | |
| + <li>Mislav</li> | |
| + </ul> | |
| +template | |
| + end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment