How to use Mako - 10 common examples

To help you get started, we’ve selected a few Mako examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github laanwj / mesa / src / intel / isl / gen_format_layout.py View on Github external
# SOFTWARE.

"""Generates isl_format_layout.c."""

from __future__ import absolute_import, division, print_function
import argparse
import csv
import re
import textwrap

from mako import template

# Load the template, ensure that __future__.division is imported, and set the
# bytes encoding to be utf-8. This last bit is important to getting simple
# consistent behavior for python 3 when we get there.
TEMPLATE = template.Template(future_imports=['division'],
                             output_encoding='utf-8',
                             text="""\
/* This file is autogenerated by gen_format_layout.py. DO NOT EDIT! */

/*
 * Copyright 2015 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
github zzzeek / mako / test / test_template.py View on Github external
def test_raw_strings(self):
        """test that raw strings go straight thru with default_filters
        turned off, bytestring_passthrough enabled.

        """

        self._do_memory_test(
            u("## -*- coding: utf-8 -*-\nhello ${x}"),
            "hello śląsk",
            default_filters=[],
            template_args={"x": "śląsk"},
            unicode_=False,
            bytestring_passthrough=True,
            output_encoding=None,  # 'ascii'
        )

        # now, the way you *should* be doing it....
        self._do_memory_test(
            u("## -*- coding: utf-8 -*-\nhello ${x}"),
            u("hello śląsk"),
            template_args={"x": u("śląsk")},
        )
github zzzeek / mako / test / test_template.py View on Github external
def test_unicode_arg(self):
        val = u(
            "Alors vous imaginez ma surprise, au lever du jour, quand "
            "une drôle de petite voix m’a réveillé. Elle disait: "
            "« S’il vous plaît… dessine-moi un mouton! »"
        )
        self._do_memory_test(
            "${val}",
            u(
                "Alors vous imaginez ma surprise, au lever du jour, quand "
                "une drôle de petite voix m’a réveillé. Elle disait: "
                "« S’il vous plaît… dessine-moi un mouton! »"
            ),
            template_args={"val": val},
        )
github zzzeek / mako / test / test_template.py View on Github external
def test_encoding_errors(self):
        self._do_memory_test(
            u(
                """KGB (transliteration of "КГБ") is the Russian-language """
                """abbreviation for Committee for State Security, """
                """(Russian: Комит́ет Госуд́арственной Безоп́асности """
                """(help·info); Komitet Gosudarstvennoy Bezopasnosti)"""
            ),
            u(
                """KGB (transliteration of "КГБ") is the Russian-language """
                """abbreviation for Committee for State Security, """
                """(Russian: Комит́ет Госуд́арственной Безоп́асности """
                """(help·info); Komitet Gosudarstvennoy Bezopasnosti)"""
            ).encode("iso-8859-1", "replace"),
            output_encoding="iso-8859-1",
            encoding_errors="replace",
            unicode_=False,
        )
github zzzeek / mako / test / test_template.py View on Github external
"Alors vous imaginez ma surprise, au lever du jour, "
                    "quand une drôle de petite voix m’a réveillé. "
                    "Elle disait: « S’il vous plaît… dessine-moi un mouton! »"
                ),
                filters=lambda s: s.strip(),
            )
        else:
            self._do_memory_test(
                u(
                    "## -*- coding: utf-8 -*-\n"
                    '${u"Alors vous imaginez ma surprise, au lever du jour, '
                    "quand une drôle de petite voix m’a réveillé. "
                    "Elle disait: « S’il vous plaît… dessine-moi un "
                    'mouton! »"}'
                ).encode("utf-8"),
                u(
                    "Alors vous imaginez ma surprise, au lever du jour, "
                    "quand une drôle de petite voix m’a réveillé. "
                    "Elle disait: « S’il vous plaît… dessine-moi un mouton! »"
                ),
                filters=lambda s: s.strip(),
            )
github zzzeek / mako / test / test_template.py View on Github external
def test_encoding_errors(self):
        self._do_memory_test(
            u(
                """KGB (transliteration of "КГБ") is the Russian-language """
                """abbreviation for Committee for State Security, """
                """(Russian: Комит́ет Госуд́арственной Безоп́асности """
                """(help·info); Komitet Gosudarstvennoy Bezopasnosti)"""
            ),
            u(
                """KGB (transliteration of "КГБ") is the Russian-language """
                """abbreviation for Committee for State Security, """
                """(Russian: Комит́ет Госуд́арственной Безоп́асности """
                """(help·info); Komitet Gosudarstvennoy Bezopasnosti)"""
            ).encode("iso-8859-1", "replace"),
            output_encoding="iso-8859-1",
            encoding_errors="replace",
            unicode_=False,
        )
github zzzeek / mako / test / test_template.py View on Github external
def test_unicode_literal_in_controlline(self):
        if compat.py3k:
            self._do_memory_test(
                u(
                    """## -*- coding: utf-8 -*-
                <%
                    x = "drôle de petite voix m’a réveillé."
                %>
                % if x=="drôle de petite voix m’a réveillé.":
                    hi, ${x}
                % endif
                """
                ).encode("utf-8"),
                u("""hi, drôle de petite voix m’a réveillé."""),
                filters=lambda s: s.strip(),
            )
        else:
            self._do_memory_test(
                u(
                    """## -*- coding: utf-8 -*-
github sqlalchemy / mako / test / test_filters.py View on Github external
def test_url_escaping(self):
        t = Template(
            """
            http://example.com/?bar=${bar | u}&v=1
        """
        )

        eq_(
            flatten_result(t.render(bar=u"酒吧bar")),
            "http://example.com/?bar=%E9%85%92%E5%90%A7bar&v=1",
        )
github sqlalchemy / mako / test / __init__.py View on Github external
def _file_template(self, filename, **kw):
        filepath = self._file_path(filename)
        return Template(
            uri=filename, filename=filepath, module_directory=module_base, **kw
        )
github zzzeek / mako / test / test_namespace.py View on Github external
def test_custom_tag_3(self):
        collection = lookup.TemplateLookup()
        collection.put_string(
            "base.html",
            """
            <%namespace name="foo" file="ns.html" inheritable="True"/>

            ${next.body()}
    """,
        )
        collection.put_string(
            "ns.html",
            """
            <%def name="bar()">
                this is ns.html->bar
                caller body: ${caller.body()}
            
        """,