File: jinja2.py

package info (click to toggle)
django-render-block 0.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 152 kB
  • sloc: python: 301; makefile: 8
file content (17 lines) | stat: -rw-r--r-- 592 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from render_block.exceptions import BlockNotFound

def jinja2_render_block(template, block_name, context):
    # Get the underlying jinja2.environment.Template object.
    template = template.template

    # Create a new Context instance.
    context = template.new_context(context)

    # Try to find the wanted block.
    try:
        gen = template.blocks[block_name](context)
    except KeyError:
        raise BlockNotFound("block with name '%s' does not exist" % block_name)

    # The result from above is a generator which yields unicode strings.
    return ''.join([s for s in gen])