Incorrect {{else}} behavior

Bug #1053534 reported by David Smith
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
pybars
Fix Released
High
Robert Collins

Bug Description

Pybars behaves differently than Handlebars.js for the {{# if ...}}{{else}}{{/if}} construct. Handlebars.js acts exactly as I would expect, while Pybars always treats the values between {{else}} and {{/if}} as if they didn't exist. Here are solutions I see in decreasing order of desirability.

1. Pybars should handle {{else}} just like Handlebars.js
2. Otherwise, Pybars should fail when you try to compile a template with an {{else}} block.
3. Otherwise, Pybars should document the {{else}} situation.

from pybars import Compiler
template = Compiler().compile(u"""
{{# if check}}
  yep.
{{else}}
  nope.
{{/if}}""")
template({"check": True}) # Outputs yep.
template({"check": False}) # Outputs nothing. It should output nope.
template({}) # Outputs nothing. It should output nope.

var template = Handlebars.compile([
  '{{# if check}}',
  ' yep.',
  '{{else}}',
  ' nope.',
  '{{/if}}'].join("\n"));
template({"check": true}); // yep.
template({"check": false}); // nope.
template({}); // nope.

Related branches

Revision history for this message
David Smith (dasmith2+launchpad) wrote :

Oh, whoops, I see you've already seen this. Release a new version pleeeeease!

Revision history for this message
David Smith (dasmith2+launchpad) wrote :

Oh, but I HAD the latest version and it still didn't work. This works incorrectly also:

from pybars import Compiler

template = Compiler().compile(u"""
{{# if check}}
  yep.
{{^}}
  nope.
{{/if}}""")
template({"check": True}) # Works as expected.
template({"check": False}) # Does not work.
template({}) # Does not work.

Revision history for this message
David Smith (dasmith2+launchpad) wrote :

Here's a patch that fixes it.

=== modified file 'pybars/_compiler.py'
--- pybars/_compiler.py 2012-07-30 03:42:07 +0000
+++ pybars/_compiler.py 2012-09-20 18:41:01 +0000
@@ -205,6 +205,8 @@
         context = context(this)
     if context:
         return options['fn'](this)
+ else:
+ return options['inverse'](this)

 def _log(this, context):

Revision history for this message
David Smith (dasmith2+launchpad) wrote :

oh, that formatted weird. Let me upload it as an attachment.

Revision history for this message
Robert Collins (lifeless) wrote :

So, I suspect unless will fail in a similar fashion; the cause here is that helpers need to handle such things themselves. I ported all the tests handlebars had at the time; clearly insufficient though :(.

Changed in pybars:
status: New → Triaged
importance: Undecided → High
Revision history for this message
Robert Collins (lifeless) wrote :

I've added a test to ensure the fix doesn't regress and am doing a new release now.

Changed in pybars:
assignee: nobody → Robert Collins (lifeless)
status: Triaged → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.