Merge lp:~lifeless/pybars/bug-1040096 into lp:pybars

Proposed by Robert Collins
Status: Merged
Merged at revision: 7
Proposed branch: lp:~lifeless/pybars/bug-1040096
Merge into: lp:pybars
Diff against target: 73 lines (+23/-2)
3 files modified
NEWS (+4/-0)
pybars/_compiler.py (+11/-2)
pybars/tests/test_acceptance.py (+8/-0)
To merge this branch: bzr merge lp:~lifeless/pybars/bug-1040096
Reviewer Review Type Date Requested Status
James Westby (community) Approve
Review via email: mp+125874@code.launchpad.net

Description of the change

handlebars.js comes with a default helperMissing, pybars doesn't, but should for compatability.

To post a comment you must log in.
Revision history for this message
James Westby (james-w) wrote :

Hi,

Looks good.

Maybe a test for the helpers ordering change would be good?

Thanks,

James

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

On Mon, Sep 24, 2012 at 12:45 PM, James Westby
<email address hidden> wrote:
> Review: Approve
>
> Hi,
>
> Looks good.
>
> Maybe a test for the helpers ordering change would be good?

Its covered already - I had to change it to keep tests passing :)

-Rob

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'NEWS'
2--- NEWS 2012-09-20 23:42:40 +0000
3+++ NEWS 2012-09-23 08:15:25 +0000
4@@ -6,6 +6,10 @@
5 NEXT
6 ----
7
8+* A default `helperMissing` helper is now present in the global set of helpers.
9+ This gives default behaviour the same as handlebars.js, though it can still
10+ be overridden. (Robert Collins, lp:1040096)
11+
12 0.0.3
13 -----
14
15
16=== modified file 'pybars/_compiler.py'
17--- pybars/_compiler.py 2012-09-20 23:42:40 +0000
18+++ pybars/_compiler.py 2012-09-23 08:15:25 +0000
19@@ -233,15 +233,23 @@
20 return options['fn'](callwith)
21
22
23+def _helperMissing(scope, name, *args):
24+ if not args:
25+ return None
26+ raise Exception(u"Could not find property %s" % (name,))
27+
28+
29 def _with(this, options, context):
30 return options['fn'](context)
31
32+
33 # scope for the compiled code to reuse globals
34 _pybars_ = {
35 'helpers': {
36 'blockHelperMissing': _blockHelperMissing,
37 'each': _each,
38 'if': _if,
39+ 'helperMissing': _helperMissing,
40 'log': _log,
41 'unless': _unless,
42 'with': _with,
43@@ -265,8 +273,9 @@
44 # when profiling.
45 self._result.grow(u"def render(context, helpers=None, partials=None):\n")
46 self._result.grow(u" result = strlist()\n")
47- self._result.grow(u" if helpers is None: helpers = {}\n")
48- self._result.grow(u" helpers.update(pybars['helpers'])\n")
49+ self._result.grow(u" _helpers = dict(pybars['helpers'])\n")
50+ self._result.grow(u" if helpers is not None: _helpers.update(helpers)\n")
51+ self._result.grow(u" helpers = _helpers\n")
52 self._result.grow(u" if partials is None: partials = {}\n")
53 # Expose used functions and helpers to the template.
54 self._locals['strlist'] = strlist
55
56=== modified file 'pybars/tests/test_acceptance.py'
57--- pybars/tests/test_acceptance.py 2012-09-20 23:42:40 +0000
58+++ pybars/tests/test_acceptance.py 2012-09-23 08:15:25 +0000
59@@ -547,6 +547,14 @@
60 source = u"{{#truthy}}yep{{/truthy}}"
61 self.assertEqual("yep", render(source, {'truthy': lambda this: True}))
62
63+ def test_default_helperMissing_no_params(self):
64+ source = u"a{{missing}}b"
65+ self.assertEqual("ab", render(source, {}))
66+
67+ def test_default_helperMissing_with_param(self):
68+ source = u"a{{missing something}}b"
69+ self.assertRaises(Exception, render, source, {})
70+
71 def test_with(self):
72 source = u"{{#with person}}{{first}} {{last}}{{/with}}"
73 self.assertEqual("Alan Johnson",

Subscribers

People subscribed via source and target branches

to all changes: