Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Jinja render filter #122

Merged
merged 8 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Next release
Features
--------
- #119: New Jinja filter: increment_datetime (thanks @jmrgonza)
- #122: New Jinja filter: render (solves #121)

Internal changes
----------------
Expand Down
23 changes: 23 additions & 0 deletions docs/sphinx/scripts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ increment_datetime
base.context:
date_time: "{{ '2022-01-01 00:00:00' | datetime | increment_datetime(days=1, hours=6)}}""

.. versionadded:: 1.1
``increment_datetime`` filter added.

date
Converts a string to a ``datetime.date`` object::

Expand Down Expand Up @@ -437,6 +440,26 @@ path_join
base.echo:
msg: "{{ ['foo', 'bar.txt'] | path_join }}"

Other filters
^^^^^^^^^^^^^

render
Renders the expression (e.g. a variable) with Jinja2 and the current context. This can be used, for
example, to explicitly render context parameters that have been set with the ``!noparse`` tag::

- base.context:
foo: me
bar: !noparse "{{ foo }}"
- when: "{{ bar|render == 'me' }}"
base.echo:
msg: "It is {{ bar }}!"

Without using the ``render`` filter in the example, the ``when`` clause would evaluate to ``false``
because the value of ``bar`` would still be ``"{{ foo }}"`` as a consequence of ``!noparse``.

.. versionadded:: 1.1
``render`` filter added.


.. _PyYAML: https://pyyaml.org
.. _RFC5545: https://tools.ietf.org/html/rfc5545
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies = [
"python-dateutil",
"deepmerge",
"PyYAML",
"jinja2",
"jinja2>=3.0.0",
]

[project.optional-dependencies]
Expand Down
6 changes: 6 additions & 0 deletions src/scriptengine/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def path_join(pathlist):
return os.path.join(*pathlist)


@jinja2.pass_context
def render_filter(context, expression):
return render(expression, context)


def filters():
"""Return all defined Jinja2 filters by their name and corresponding function"""
return {
Expand All @@ -55,6 +60,7 @@ def filters():
"dirname": dirname,
"exists": exists,
"path_join": path_join,
"render": render_filter,
}


Expand Down
17 changes: 17 additions & 0 deletions tests/jobs/test_conditionals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import yaml

from scriptengine.engines import SimpleScriptEngine
from scriptengine.yaml.parser import parse


Expand Down Expand Up @@ -31,3 +32,19 @@ def test_when_false(capsys):
j.run({})
captured = capsys.readouterr()
assert "Hello, world!" not in captured.out


def test_when_clause_parses_noparse_parameter(capsys):
s = from_yaml(
"""
- base.context:
foo: me
bar: !noparse_jinja "{{ foo }}"
- when: "{{ bar|render == 'me' }}"
base.echo:
msg: Hello!
"""
)
SimpleScriptEngine().run(s, context={})
captured = capsys.readouterr()
assert "Hello!" in captured.out
Loading