summaryrefslogtreecommitdiffstats
path: root/third_party/python/Jinja2/examples
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/python/Jinja2/examples')
-rw-r--r--third_party/python/Jinja2/examples/basic/cycle.py18
-rw-r--r--third_party/python/Jinja2/examples/basic/debugger.py8
-rw-r--r--third_party/python/Jinja2/examples/basic/inheritance.py15
-rw-r--r--third_party/python/Jinja2/examples/basic/templates/broken.html6
-rw-r--r--third_party/python/Jinja2/examples/basic/templates/subbroken.html3
-rw-r--r--third_party/python/Jinja2/examples/basic/test.py31
-rw-r--r--third_party/python/Jinja2/examples/basic/test_filter_and_linestatements.py29
-rw-r--r--third_party/python/Jinja2/examples/basic/test_loop_filter.py15
-rw-r--r--third_party/python/Jinja2/examples/basic/translate.py20
9 files changed, 145 insertions, 0 deletions
diff --git a/third_party/python/Jinja2/examples/basic/cycle.py b/third_party/python/Jinja2/examples/basic/cycle.py
new file mode 100644
index 0000000000..25dcb0b090
--- /dev/null
+++ b/third_party/python/Jinja2/examples/basic/cycle.py
@@ -0,0 +1,18 @@
+from __future__ import print_function
+
+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/third_party/python/Jinja2/examples/basic/debugger.py b/third_party/python/Jinja2/examples/basic/debugger.py
new file mode 100644
index 0000000000..d3c1a60a7a
--- /dev/null
+++ b/third_party/python/Jinja2/examples/basic/debugger.py
@@ -0,0 +1,8 @@
+from __future__ import print_function
+
+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/third_party/python/Jinja2/examples/basic/inheritance.py b/third_party/python/Jinja2/examples/basic/inheritance.py
new file mode 100644
index 0000000000..4a881bf8a8
--- /dev/null
+++ b/third_party/python/Jinja2/examples/basic/inheritance.py
@@ -0,0 +1,15 @@
+from __future__ import print_function
+
+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/third_party/python/Jinja2/examples/basic/templates/broken.html b/third_party/python/Jinja2/examples/basic/templates/broken.html
new file mode 100644
index 0000000000..294d5c9989
--- /dev/null
+++ b/third_party/python/Jinja2/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/third_party/python/Jinja2/examples/basic/templates/subbroken.html b/third_party/python/Jinja2/examples/basic/templates/subbroken.html
new file mode 100644
index 0000000000..245eb7e6e6
--- /dev/null
+++ b/third_party/python/Jinja2/examples/basic/templates/subbroken.html
@@ -0,0 +1,3 @@
+{% macro may_break(item) -%}
+ [{{ item / 0 }}]
+{%- endmacro %}
diff --git a/third_party/python/Jinja2/examples/basic/test.py b/third_party/python/Jinja2/examples/basic/test.py
new file mode 100644
index 0000000000..80b9d1f052
--- /dev/null
+++ b/third_party/python/Jinja2/examples/basic/test.py
@@ -0,0 +1,31 @@
+from __future__ import print_function
+
+from jinja2 import Environment
+from jinja2.loaders import DictLoader
+
+env = Environment(
+ loader=DictLoader(
+ {
+ "child.html": u"""\
+{% extends master_layout or 'master.html' %}
+{% include helpers = 'helpers.html' %}
+{% macro get_the_answer() %}42{% endmacro %}
+{% title = 'Hello World' %}
+{% block body %}
+ {{ get_the_answer() }}
+ {{ helpers.conspirate() }}
+{% endblock %}
+""",
+ "master.html": u"""\
+<!doctype html>
+<title>{{ title }}</title>
+{% block body %}{% endblock %}
+""",
+ "helpers.html": u"""\
+{% macro conspirate() %}23{% endmacro %}
+""",
+ }
+ )
+)
+tmpl = env.get_template("child.html")
+print(tmpl.render())
diff --git a/third_party/python/Jinja2/examples/basic/test_filter_and_linestatements.py b/third_party/python/Jinja2/examples/basic/test_filter_and_linestatements.py
new file mode 100644
index 0000000000..673b67ed76
--- /dev/null
+++ b/third_party/python/Jinja2/examples/basic/test_filter_and_linestatements.py
@@ -0,0 +1,29 @@
+from __future__ import print_function
+
+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/third_party/python/Jinja2/examples/basic/test_loop_filter.py b/third_party/python/Jinja2/examples/basic/test_loop_filter.py
new file mode 100644
index 0000000000..39be08d61c
--- /dev/null
+++ b/third_party/python/Jinja2/examples/basic/test_loop_filter.py
@@ -0,0 +1,15 @@
+from __future__ import print_function
+
+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/third_party/python/Jinja2/examples/basic/translate.py b/third_party/python/Jinja2/examples/basic/translate.py
new file mode 100644
index 0000000000..71547f4649
--- /dev/null
+++ b/third_party/python/Jinja2/examples/basic/translate.py
@@ -0,0 +1,20 @@
+from __future__ import print_function
+
+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",
+}[n == 1 and s or 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])
+)