Skip to content

Commit

Permalink
📚 ☔ Add extension docs and tests
Browse files Browse the repository at this point in the history
Related to moremoban#90
  • Loading branch information
ayan-b committed Jan 17, 2019
1 parent ce1a96c commit 2c3d190
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/level-12-use-template-engine-extensions/.moban.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
targets:
- a.output: a.template
- b.output: b.template
extensions:
jinja2:
- jinja2.ext.with_
15 changes: 12 additions & 3 deletions docs/level-12-use-template-engine-extensions/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ Level 11: use template engine extensions

jinja2 comes with a lot of extensions. In order not to be the blocker in the
middle, **extensions** is allowed in moban file to initialize jinja2 engine
with desired extensions
with desired extensions. Two extensions, expression-statement and loop-controls
are enabled by default.

The extensions syntax is::

extensions:
jinja2:
- jinja2.ext.do
- jinja2.ext.loopcontrols
- jinja2.ext.i18n


Evaluation
--------------------------------------------------------------------------------
Please go to `docs/level-12-use-template-engine-extensions` directory.

If you notice the file `a.template`, we are using a for loop control. This is
because moban comes with two default extensions loop-controls and
expression-statement.

Now, let us try to use the extension `with`. To do that, we have to enable the
extension in the `.moban.yml` file following the above syntax. Now, the
extension can be used in the jinja2 templates. One such example is shown in the
`b.template` file.
3 changes: 3 additions & 0 deletions docs/level-12-use-template-engine-extensions/a.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% for _ in range(1,5) %}
{{ hello }}
{% endfor %}
9 changes: 9 additions & 0 deletions docs/level-12-use-template-engine-extensions/b.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% with %}
{% set foo = 142 %}
{{ foo }}
{% with %}
{% set foo = 42 %}
{{ foo }}
{% endwith %}
{{ foo }}
{% endwith %}
1 change: 1 addition & 0 deletions docs/level-12-use-template-engine-extensions/data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello: world
14 changes: 14 additions & 0 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ def test_level_11(self):
folder = "level-11-use-handlebars"
self._raw_moban(["moban"], folder, expected, "a.output")

def test_level_12(self):
expected_a = """world
world
world
world
"""
expected_b = """142
42
142
"""
folder = "level-12-use-template-engine-extensions"
self._raw_moban(["moban"], folder, expected_a, "a.output")
self._raw_moban(["moban"], folder, expected_b, "b.output")

def test_misc_1(self):
expected = "test file\n"

Expand Down

0 comments on commit 2c3d190

Please sign in to comment.