wbond / pybars3

Handlebars.js template support for Python 3 and 2
GNU Lesser General Public License v3.0
179 stars 46 forks source link

Inverted blocks not providing expected output #18

Closed ticklemepierce closed 8 years ago

ticklemepierce commented 9 years ago

Code (modified from the tests):

compiler = Compiler()

source = u"Goodbye {{^each things}}cruel{{/each}} world!"

template = compiler.compile(source)

output = template({
    'things': None
})

print output

In this case output is Goodbye cruel world! as expected.

However, if 'things' is modified to not be None, then the output should be Goodbye world! The following code still outputs Goodbye cruel world!

compiler = Compiler()

source = u"Goodbye {{^each things}}cruel{{/each}} world!"

template = compiler.compile(source)

output = template({
    'things': ["thing1", "thing2"]
})

print output
mjumbewu commented 9 years ago

I actually can't find good documentation on what the behavior of this should be in Handlebars. In Mustache it's pretty clear, but I'm not sure the Handlebars docs say anything about using carrot (^) with a block helper. Does this work as expected if you use {{^things}}...{{/things}} instead?

ticklemepierce commented 9 years ago

It works as expected in that case, however the caret should invert every block.

This website lets you run handlebars code. I was able to invert both an if statement and a custom defined block helper.

HTML:

<script id="block-expressions-template" type="text/x-handlebars-template">
  <p>{{#if code}}{{code}}{{else}}Test{{/if}}</p>
  <p>{{^if code}}{{code}}{{else}}Test{{/if}}</p>
  <p>{{#has code}}{{code}}{{else}}Test{{/has}}</p>
  <p>{{^has code}}{{code}}{{else}}Test{{/has}}</p>
</script>

<div class="content-placeholder"></div>

JS:

$(function () {
  // Grab the template script
  var theTemplateScript = $("#block-expressions-template").html();

  Handlebars.registerHelper('has', function(value, options) {
    return options.fn(this);
  });

  // Compile the template
  var theTemplate = Handlebars.compile(theTemplateScript);

  // Define our data object
  var context = {
    "code": "test"
  };

  // Pass our data to the template
  var theCompiledHtml = theTemplate(context);

  // Add the compiled html to the page
  $('.content-placeholder').html(theCompiledHtml);

});

The inverted ones should print Test and the regular ones should print test

isaacdd commented 8 years ago

Fixed with 0.9.2.