diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 16:06:13 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 16:06:13 +0000 |
commit | ce859b2defe1fc90c7a16551291799fc37284add (patch) | |
tree | 7e6b04791662f8f268c7abc9c07d41e98a261a84 /examples | |
parent | Initial commit. (diff) | |
download | jinja2-upstream.tar.xz jinja2-upstream.zip |
Adding upstream version 3.1.2.upstream/3.1.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | examples/basic/cycle.py | 16 | ||||
-rw-r--r-- | examples/basic/debugger.py | 6 | ||||
-rw-r--r-- | examples/basic/inheritance.py | 13 | ||||
-rw-r--r-- | examples/basic/templates/broken.html | 6 | ||||
-rw-r--r-- | examples/basic/templates/subbroken.html | 3 | ||||
-rw-r--r-- | examples/basic/test.py | 29 | ||||
-rw-r--r-- | examples/basic/test_filter_and_linestatements.py | 27 | ||||
-rw-r--r-- | examples/basic/test_loop_filter.py | 13 | ||||
-rw-r--r-- | examples/basic/translate.py | 18 |
9 files changed, 131 insertions, 0 deletions
diff --git a/examples/basic/cycle.py b/examples/basic/cycle.py new file mode 100644 index 0000000..1f97e37 --- /dev/null +++ b/examples/basic/cycle.py @@ -0,0 +1,16 @@ +from jinja2 import Environment + +env = Environment( + line_statement_prefix="#", variable_start_string="${", variable_end_string="}" +) +print( + env.from_string( + """\ +<ul> +# for item in range(10) + <li class="${loop.cycle('odd', 'even')}">${item}</li> +# endfor +</ul>\ +""" + ).render() +) diff --git a/examples/basic/debugger.py b/examples/basic/debugger.py new file mode 100644 index 0000000..f6a9627 --- /dev/null +++ b/examples/basic/debugger.py @@ -0,0 +1,6 @@ +from jinja2 import Environment +from jinja2.loaders import FileSystemLoader + +env = Environment(loader=FileSystemLoader("templates")) +tmpl = env.get_template("broken.html") +print(tmpl.render(seq=[3, 2, 4, 5, 3, 2, 0, 2, 1])) diff --git a/examples/basic/inheritance.py b/examples/basic/inheritance.py new file mode 100644 index 0000000..6d928df --- /dev/null +++ b/examples/basic/inheritance.py @@ -0,0 +1,13 @@ +from jinja2 import Environment +from jinja2.loaders import DictLoader + +env = Environment( + loader=DictLoader( + { + "a": "[A[{% block body %}{% endblock %}]]", + "b": "{% extends 'a' %}{% block body %}[B]{% endblock %}", + "c": "{% extends 'b' %}{% block body %}###{{ super() }}###{% endblock %}", + } + ) +) +print(env.get_template("c").render()) diff --git a/examples/basic/templates/broken.html b/examples/basic/templates/broken.html new file mode 100644 index 0000000..294d5c9 --- /dev/null +++ b/examples/basic/templates/broken.html @@ -0,0 +1,6 @@ +{% from 'subbroken.html' import may_break %} +<ul> +{% for item in seq %} + <li>{{ may_break(item) }}</li> +{% endfor %} +</ul> diff --git a/examples/basic/templates/subbroken.html b/examples/basic/templates/subbroken.html new file mode 100644 index 0000000..245eb7e --- /dev/null +++ b/examples/basic/templates/subbroken.html @@ -0,0 +1,3 @@ +{% macro may_break(item) -%} + [{{ item / 0 }}] +{%- endmacro %} diff --git a/examples/basic/test.py b/examples/basic/test.py new file mode 100644 index 0000000..7a58e1a --- /dev/null +++ b/examples/basic/test.py @@ -0,0 +1,29 @@ +from jinja2 import Environment +from jinja2.loaders import DictLoader + +env = Environment( + loader=DictLoader( + { + "child.html": """\ +{% extends default_layout or 'default.html' %} +{% include helpers = 'helpers.html' %} +{% macro get_the_answer() %}42{% endmacro %} +{% title = 'Hello World' %} +{% block body %} + {{ get_the_answer() }} + {{ helpers.conspirate() }} +{% endblock %} +""", + "default.html": """\ +<!doctype html> +<title>{{ title }}</title> +{% block body %}{% endblock %} +""", + "helpers.html": """\ +{% macro conspirate() %}23{% endmacro %} +""", + } + ) +) +tmpl = env.get_template("child.html") +print(tmpl.render()) diff --git a/examples/basic/test_filter_and_linestatements.py b/examples/basic/test_filter_and_linestatements.py new file mode 100644 index 0000000..9bbcbca --- /dev/null +++ b/examples/basic/test_filter_and_linestatements.py @@ -0,0 +1,27 @@ +from jinja2 import Environment + +env = Environment( + line_statement_prefix="%", variable_start_string="${", variable_end_string="}" +) +tmpl = env.from_string( + """\ +% macro foo() + ${caller(42)} +% endmacro +<ul> +% for item in seq + <li>${item}</li> +% endfor +</ul> +% call(var) foo() + [${var}] +% endcall +% filter escape + <hello world> + % for item in [1, 2, 3] + - ${item} + % endfor +% endfilter +""" +) +print(tmpl.render(seq=range(10))) diff --git a/examples/basic/test_loop_filter.py b/examples/basic/test_loop_filter.py new file mode 100644 index 0000000..6bd89fd --- /dev/null +++ b/examples/basic/test_loop_filter.py @@ -0,0 +1,13 @@ +from jinja2 import Environment + +tmpl = Environment().from_string( + """\ +<ul> +{%- for item in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] if item % 2 == 0 %} + <li>{{ loop.index }} / {{ loop.length }}: {{ item }}</li> +{%- endfor %} +</ul> +if condition: {{ 1 if foo else 0 }} +""" +) +print(tmpl.render(foo=True)) diff --git a/examples/basic/translate.py b/examples/basic/translate.py new file mode 100644 index 0000000..e659681 --- /dev/null +++ b/examples/basic/translate.py @@ -0,0 +1,18 @@ +from jinja2 import Environment + +env = Environment(extensions=["jinja2.ext.i18n"]) +env.globals["gettext"] = {"Hello %(user)s!": "Hallo %(user)s!"}.__getitem__ +env.globals["ngettext"] = lambda s, p, n: { + "%(count)s user": "%(count)d Benutzer", + "%(count)s users": "%(count)d Benutzer", +}[s if n == 1 else p] +print( + env.from_string( + """\ +{% trans %}Hello {{ user }}!{% endtrans %} +{% trans count=users|count -%} +{{ count }} user{% pluralize %}{{ count }} users +{% endtrans %} +""" + ).render(user="someone", users=[1, 2, 3]) +) |