Skip to content

Commit

Permalink
Merge pull request #56765 from s0undt3ch/port-to-master/50152
Browse files Browse the repository at this point in the history
Port #50152 to master
  • Loading branch information
dwoz authored Apr 22, 2020
2 parents 1ce5f24 + 8e2c32f commit e3e5797
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 91 deletions.
52 changes: 51 additions & 1 deletion doc/topics/jinja/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ the context into the included file is required:
.. code-block:: jinja
{% from 'lib.sls' import test with context %}
Includes must use full paths, like so:

.. code-block:: jinja
Expand Down Expand Up @@ -649,6 +649,56 @@ Returns:
1, 4
.. jinja_ref:: method_call

``method_call``
---------------

.. versionadded:: Sodium

Returns a result of object's method call.

Example #1:

.. code-block:: jinja
{{ [1, 2, 1, 3, 4] | method_call('index', 1, 1, 3) }}
Returns:

.. code-block:: text
2
This filter can be used with the `map filter`_ to apply object methods without
using loop constructs or temporary variables.

Example #2:

.. code-block:: jinja
{% set host_list = ['web01.example.com', 'db01.example.com'] %}
{% set host_list_split = [] %}
{% for item in host_list %}
{% do host_list_split.append(item.split('.', 1)) %}
{% endfor %}
{{ host_list_split }}
Example #3:

.. code-block:: jinja
{{ host_list|map('method_call', 'split', '.', 1)|list }}
Return of examples #2 and #3:

.. code-block:: text
[[web01, example.com], [db01, example.com]]
.. _`map filter`: http://jinja.pocoo.org/docs/2.10/templates/#map


.. jinja_ref:: is_sorted

``is_sorted``
Expand Down
5 changes: 5 additions & 0 deletions salt/utils/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,11 @@ def symmetric_difference(lst1, lst2):
)


@jinja_filter("method_call")
def method_call(obj, f_name, *f_args, **f_kwargs):
return getattr(obj, f_name, lambda *args, **kwargs: None)(*f_args, **f_kwargs)


@jinja2.contextfunction
def show_full_context(ctx):
return salt.utils.data.simple_types_filter(
Expand Down
Loading

0 comments on commit e3e5797

Please sign in to comment.