Ejemplo n.º 1
0
def render_template_string(request,
                           ctx,
                           template,
                           minify_html=None,
                           extra_ctx=None):
    with graphite_timer('jinja2.render_template_string'):
        if minify_html is None:
            minify_html = template.endswith('.html')

        if extra_ctx:
            if not is_seq_not_string(extra_ctx):
                extra_ctx = [extra_ctx]

            new_ctx = {}
            page_load_hooks = ctx.get('__js_page_load_hooks', [])

            for ctx_callable in extra_ctx:
                ctx_item = ctx_callable(request)
                if ctx_item:
                    if '__js_page_load_hooks' in ctx_item:
                        page_load_hooks.extend(
                            ctx_item['__js_page_load_hooks'])
                        del ctx_item['__js_page_load_hooks']
                        new_ctx.update(ctx_item)

            new_ctx.update(ctx)
            new_ctx['__js_page_load_hooks'] = page_load_hooks

            ctx = new_ctx

        with graphite_timer('jinja2.render_to_string'):
            output = render_to_string(template,
                                      ctx,
                                      context_instance=RequestContext(request))

        if isinstance(output, unicode):
            output = output.encode('utf-8')

        if ctx and ctx.get('__no_minify'):
            minify_html = False

        if minify_html:
            with graphite_timer('jinja2.minify_html'):
                output = slimmer.html_slimmer(output)

        return output
Ejemplo n.º 2
0
def render_template_string(request, ctx, template, minify_html=None, extra_ctx=None):
    with graphite_timer('jinja2.render_template_string'):
        if minify_html is None:
            minify_html = template.endswith('.html')

        if extra_ctx:
            if not is_seq_not_string(extra_ctx):
                extra_ctx = [extra_ctx]

            new_ctx = {}
            page_load_hooks = ctx.get('__js_page_load_hooks', [])

            for ctx_callable in extra_ctx:
                ctx_item = ctx_callable(request)
                if ctx_item:
                    if '__js_page_load_hooks' in ctx_item:
                        page_load_hooks.extend(ctx_item['__js_page_load_hooks'])
                        del ctx_item['__js_page_load_hooks']
                        new_ctx.update(ctx_item)

            new_ctx.update(ctx)
            new_ctx['__js_page_load_hooks'] = page_load_hooks

            ctx = new_ctx

        with graphite_timer('jinja2.render_to_string'):
            output = render_to_string(template, ctx, context_instance=RequestContext(request))

        if isinstance(output, unicode):
            output = output.encode('utf-8')

        if ctx and ctx.get('__no_minify'):
            minify_html = False

        if minify_html:
            with graphite_timer('jinja2.minify_html'):
                output = slimmer.html_slimmer(output)

        return output
Ejemplo n.º 3
0
def minify(html):
    "Convenience method."
    if isinstance(html, unicode):
        html = html.encode('utf-8')

    return slimmer.html_slimmer(html)
Ejemplo n.º 4
0
def minify(html):
    "Convenience method."
    if isinstance(html, unicode):
        html = html.encode('utf-8')

    return slimmer.html_slimmer(html)