summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/build/filtering.rst18
-rw-r--r--doc/build/syntax.rst13
-rw-r--r--doc/build/unreleased/n_page.rst13
-rw-r--r--mako/codegen.py2
-rw-r--r--test/test_filters.py11
5 files changed, 54 insertions, 3 deletions
diff --git a/doc/build/filtering.rst b/doc/build/filtering.rst
index 3bcb25a..c657b01 100644
--- a/doc/build/filtering.rst
+++ b/doc/build/filtering.rst
@@ -4,6 +4,8 @@
Filtering and Buffering
=======================
+.. _expression_filtering:
+
Expression Filtering
====================
@@ -150,6 +152,8 @@ The above will generate templates something like this:
def render_body(context):
context.write(myfilter(unicode("some text")))
+.. _expression_filtering_nfilter:
+
Turning off Filtering with the ``n`` Filter
-------------------------------------------
@@ -169,6 +173,20 @@ will render ``myexpression`` with no filtering of any kind, and:
will render ``myexpression`` using the ``trim`` filter only.
+Including the ``n`` filter in a ``<%page>`` tag will only disable
+``default_filters``. In effect this makes the filters from the tag replace
+default filters instead of adding to them. For example:
+
+.. sourcecode:: mako
+
+ <%page expression_filter="n, json.dumps"/>
+ data = {a: ${123}, b: ${"123"}};
+
+will suppress turning the values into strings using the default filter, so that
+``json.dumps`` (which requires ``imports=["import json"]`` or something
+equivalent) can take the value type into account, formatting numbers as numeric
+literals and strings as string literals.
+
Filtering Defs and Blocks
=========================
diff --git a/doc/build/syntax.rst b/doc/build/syntax.rst
index 4d2814d..2873584 100644
--- a/doc/build/syntax.rst
+++ b/doc/build/syntax.rst
@@ -271,8 +271,17 @@ Currently, only one ``<%page>`` tag gets used per template, the
rest get ignored. While this will be improved in a future
release, for now make sure you have only one ``<%page>`` tag
defined in your template, else you may not get the results you
-want. The details of what ``<%page>`` is used for are described
-further in :ref:`namespaces_body` as well as :ref:`caching_toplevel`.
+want. Further details on what ``<%page>`` is used for are described
+in the following sections:
+
+* :ref:`namespaces_body` - ``<%page>`` is used to define template-level
+ arguments and defaults
+
+* :ref:`expression_filtering` - expression filters can be applied to all
+ expressions throughout a template using the ``<%page>`` tag
+
+* :ref:`caching_toplevel` - options to control template-level caching
+ may be applied in the ``<%page>`` tag.
``<%include>``
--------------
diff --git a/doc/build/unreleased/n_page.rst b/doc/build/unreleased/n_page.rst
new file mode 100644
index 0000000..958a5ba
--- /dev/null
+++ b/doc/build/unreleased/n_page.rst
@@ -0,0 +1,13 @@
+.. change::
+ :tags: feature, template
+
+ The ``n`` filter is now supported in the ``<%page>`` tag. This allows a
+ template to omit the default expression filters throughout a whole
+ template, for those cases where a template-wide filter needs to have
+ default filtering disabled. Pull request courtesy Martin von Gagern.
+
+ .. seealso::
+
+ :ref:`expression_filtering_nfilter`
+
+
diff --git a/mako/codegen.py b/mako/codegen.py
index 1acc5e6..5ca7b04 100644
--- a/mako/codegen.py
+++ b/mako/codegen.py
@@ -802,7 +802,7 @@ class _GenerateRenderMethod(object):
if is_expression:
if self.compiler.pagetag:
args = self.compiler.pagetag.filter_args.args + args
- if self.compiler.default_filters:
+ if self.compiler.default_filters and "n" not in args:
args = self.compiler.default_filters + args
for e in args:
# if filter given as a function, get just the identifier portion
diff --git a/test/test_filters.py b/test/test_filters.py
index 598cb45..a58a01f 100644
--- a/test/test_filters.py
+++ b/test/test_filters.py
@@ -294,6 +294,17 @@ class FilterTest(TemplateTest):
)
assert t.render().strip() == "&lt;tag&gt;this is html&lt;/tag&gt;"
+ def test_global_json(self):
+ t = Template(
+ """
+<%!
+import json
+%><%page expression_filter="n, json.dumps"/>
+data = {a: ${123}, b: ${"123"}};
+ """
+ )
+ assert t.render().strip() == """data = {a: 123, b: "123"};"""
+
def test_non_expression(self):
t = Template(
"""