Skip to content

Commit 75d173d

Browse files
committed
🔨 code refactoring
1 parent ad9da9c commit 75d173d

File tree

5 files changed

+29
-29
lines changed

5 files changed

+29
-29
lines changed

.moban.d/helper_and_partial.rst.jj2

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ Let's save the following file a `script.py` under `helper_and_partial` folder:
44

55
.. code-block:: python
66

7-
from moban_handlebars.helpers import HandlebarHelper
8-
from moban_handlebars.partials import register_partial
7+
from moban_handlebars.api import Helper, register_partial
98

109
register_partial('header', '<h1>People</h1>')
1110

1211

13-
@HandlebarHelper('list')
12+
@Helper('list')
1413
def _list(this, options, items):
1514
result = [u'<ul>']
1615
for thing in items:
@@ -24,7 +23,7 @@ Let's save the following file a `script.py` under `helper_and_partial` folder:
2423
Let invoke handlebar template:
2524
{% raw %}
2625

27-
.. code-block: bash
26+
.. code-block:: bash
2827

2928
$ moban --template-type hbs -pd helper_and_partial -c data.json "{{>header}}{{#list people}}{{name}} {{age}}{{/list}}"
3029
Handlebars-ing {{>header}... to moban.output

moban_handlebars/api.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from pybars import Compiler
2+
from lml.plugin import PluginInfo
3+
4+
from moban_handlebars.constants import HELPER_EXTENSION, PARTIALS_EXTENSION
5+
6+
7+
class Helper(PluginInfo):
8+
"""
9+
@Helper('tag_used_inside_handlebar')
10+
def the_actual_implementation(this, options, items):
11+
...
12+
"""
13+
def __init__(self, helper):
14+
super(Helper, self).__init__(HELPER_EXTENSION)
15+
self.helper = helper
16+
17+
def tags(self):
18+
yield self.helper
19+
20+
21+
def register_partial(identifier, partial):
22+
plugin = PluginInfo(PARTIALS_EXTENSION, tags=[identifier])
23+
partial = Compiler().compile(partial)
24+
plugin({identifier: partial})

moban_handlebars/helpers.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

moban_handlebars/partials.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/regression_tests/level-11-helpers-and-partials/helper_and_partial/script.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from moban_handlebars.helpers import HandlebarHelper
2-
from moban_handlebars.partials import register_partial
1+
from moban_handlebars.api import Helper, register_partial
32

43
register_partial("header", "<h1>People</h1>")
54

65

7-
@HandlebarHelper("list")
6+
@Helper("list")
87
def _list(this, options, items):
98
result = [u"<ul>"]
109
for thing in items:

0 commit comments

Comments
 (0)