From 0db3afced3b01d0eac2f115c5ff179012a86f3f2 Mon Sep 17 00:00:00 2001 From: ngr Date: Mon, 14 May 2018 10:53:36 -0400 Subject: [PATCH] {% raw %} tests and note for documentation --- docs/templates.rst | 5 +++++ tests/test_lexnparse.py | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/docs/templates.rst b/docs/templates.rst index c60234b3..6b123a21 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -271,6 +271,11 @@ include example Jinja syntax in a template, you can use this snippet:: {% endraw %} +.. admonition:: Note + + Minus sign at the end of ``{% raw -%}`` tag cleans all the spaces and newlines + preceding the first character of your raw data. + .. _line-statements: diff --git a/tests/test_lexnparse.py b/tests/test_lexnparse.py index 7da72c0b..28a60765 100644 --- a/tests/test_lexnparse.py +++ b/tests/test_lexnparse.py @@ -67,6 +67,18 @@ class TestLexer(object): tmpl = env.from_string('1 {%- raw -%} 2 {%- endraw -%} 3') assert tmpl.render() == '123' + def test_raw3(self, env): + # The second newline after baz exists because it is AFTER the {% raw %} and is ignored. + env = Environment(lstrip_blocks=True, trim_blocks=True) + tmpl = env.from_string("bar\n{% raw %}\n {{baz}}2 spaces\n{% endraw %}\nfoo") + assert tmpl.render(baz='test') == "bar\n\n {{baz}}2 spaces\nfoo" + + def test_raw4(self, env): + # The trailing dash of the {% raw -%} cleans both the spaces and newlines up to the first character of data. + env = Environment(lstrip_blocks=True, trim_blocks=False) + tmpl = env.from_string("bar\n{%- raw -%}\n\n \n 2 spaces\n space{%- endraw -%}\nfoo") + assert tmpl.render() == "bar2 spaces\n spacefoo" + def test_balancing(self, env): env = Environment('{%', '%}', '${', '}') tmpl = env.from_string('''{% for item in seq -- 2.47.2