def render_jinja2_block(template_name, extra_vars=None, block=None, cache_key=None, cache_type=None, cache_expire=None): """Render a template or a block from template with Jinja2 Accepts the cache options ``cache_key``, ``cache_type``, and ``cache_expire``. Allows rendering of single block from the template: # In controller return render('/controller/index.html', block='sidebar') """ from pylons.templating import pylons_globals, cached_template from webhelpers.html import literal from jinja2.utils import concat # Create a render callable for the cache function def render_template(): # Pull in extra vars if needed globs = extra_vars or {} # Second, get the globals globs.update(pylons_globals()) # Grab a template reference template = \ globs['app_globals'].jinja2_env.get_template(template_name) # Determine render function depending on whether block # is defined or not and fallback to full template if # block is defined but not found in template # TBD: maybe we should throw when block is defined but not found? render_func = template.blocks.get(block, template.root_render_func) if block else template.root_render_func try: return literal(concat(render_func(template.new_context(globs)))) except: from jinja2.debug import translate_exception exc_type, exc_value, tb = translate_exception(sys.exc_info()) raise exc_type, exc_value, tb return cached_template(template_name, render_template, cache_key=cache_key, cache_type=cache_type, cache_expire=cache_expire)
Powered by Pylons - Contact Administrators
Comments (0)
You must login before you can comment.