Exemplo n.º 1
0
    def test_get_def_base(self):
        theme = base.BaseTheme(self.settings)
        templ = theme.lookup.get_template('common/blocks.html')
        buf = FastEncodingBuffer(as_unicode=True)
        ctx = Context(buf)
        templ.get_def('hello').render_context(ctx)

        s = buf.getvalue()
        self.assertIn('base theme blocks hello', s)
    def test_get_def_base(self):
        theme = base.BaseTheme(self.settings)
        templ = theme.lookup.get_template('common/blocks.html')
        buf = FastEncodingBuffer(as_unicode=True)
        ctx = Context(buf)
        templ.get_def('hello').render_context(ctx)

        s = buf.getvalue()
        self.assertIn('base theme blocks hello', s)
Exemplo n.º 3
0
    def test_get_def_by_file(self):
        theme = quux.QuuxTheme(self.settings)

        filename = os.path.join(theme.template_dirs[1], 'common/blocks.html')
        uri = theme.lookup.filename_to_uri(filename)

        templ = theme.lookup.get_template(uri)

        buf = FastEncodingBuffer(as_unicode=True)
        ctx = Context(buf)
        templ.get_def('hello').render_context(ctx)

        s = buf.getvalue()
        self.assertIn('foo theme blocks hello', s)
    def test_get_def_by_file(self):
        theme = quux.QuuxTheme(self.settings)

        filename = os.path.join(theme.template_dirs[1], 'common/blocks.html')
        uri = theme.lookup.filename_to_uri(filename)

        templ = theme.lookup.get_template(uri)

        buf = FastEncodingBuffer(as_unicode=True)
        ctx = Context(buf)
        templ.get_def('hello').render_context(ctx)

        s = buf.getvalue()
        self.assertIn('foo theme blocks hello', s)
Exemplo n.º 5
0
    def test_dont_accept_relative_outside_of_root(self):
        """test the mechanics of an include where
        the include goes outside of the path"""
        tl = lookup.TemplateLookup(
            directories=[os.path.join(config.template_base, "subdir")])
        index = tl.get_template("index.html")

        ctx = runtime.Context(FastEncodingBuffer())
        ctx._with_template = index

        assert_raises_message(
            exceptions.TemplateLookupException,
            'Template uri "../index.html" is invalid - it '
            "cannot be relative outside of the root path",
            runtime._lookup_template,
            ctx,
            "../index.html",
            index.uri,
        )

        assert_raises_message(
            exceptions.TemplateLookupException,
            'Template uri "../othersubdir/foo.html" is invalid - it '
            "cannot be relative outside of the root path",
            runtime._lookup_template,
            ctx,
            "../othersubdir/foo.html",
            index.uri,
        )

        # this is OK since the .. cancels out
        runtime._lookup_template(ctx, "foo/../index.html", index.uri)
Exemplo n.º 6
0
def _render(template, callable_, args, data, as_unicode=False):
    """create a Context and return the string
    output of the given template and template callable."""

    if as_unicode:
        buf = FastEncodingBuffer(as_unicode=True)
    elif template.bytestring_passthrough:
        buf = compat.StringIO()
    else:
        buf = FastEncodingBuffer(as_unicode=as_unicode,
                                 encoding=template.output_encoding,
                                 errors=template.encoding_errors)
    context = MakoInputEnvContext(buf, data)
    context._outputting_as_unicode = as_unicode
    context._set_with_template(template)
    _render_context(template, callable_, context, args,
                    mako_runtime._kwargs_for_callable(callable_, data))
    return context._pop_buffer().getvalue()