mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
A Jinja2 version of {% include_block %} tag
This commit is contained in:
parent
ad1f6716c5
commit
a6d021d146
@ -1,12 +1,15 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import jinja2
|
||||
import jinja2.nodes
|
||||
from jinja2.ext import Extension
|
||||
|
||||
from .templatetags.wagtailcore_tags import pageurl, richtext, slugurl, wagtail_version
|
||||
|
||||
|
||||
class WagtailCoreExtension(Extension):
|
||||
tags = {'include_block'}
|
||||
|
||||
def __init__(self, environment):
|
||||
super(WagtailCoreExtension, self).__init__(environment)
|
||||
|
||||
@ -19,6 +22,40 @@ class WagtailCoreExtension(Extension):
|
||||
'richtext': richtext,
|
||||
})
|
||||
|
||||
def parse(self, parser):
|
||||
parse_method = getattr(self, 'parse_' + parser.stream.current.value)
|
||||
|
||||
return parse_method(parser)
|
||||
|
||||
def parse_include_block(self, parser):
|
||||
lineno = next(parser.stream).lineno
|
||||
|
||||
args = [parser.parse_expression()]
|
||||
|
||||
with_context = True
|
||||
if parser.stream.current.test_any('name:with', 'name:without') and parser.stream.look().test('name:context'):
|
||||
with_context = next(parser.stream).value == 'with'
|
||||
parser.stream.skip()
|
||||
|
||||
if with_context:
|
||||
args.append(jinja2.nodes.ContextReference())
|
||||
else:
|
||||
# Actually we can just skip else branch because context arg default to None
|
||||
args.append(jinja2.nodes.Const(None))
|
||||
|
||||
node = self.call_method('_include_block', args, lineno=lineno)
|
||||
return jinja2.nodes.Output([node], lineno=lineno)
|
||||
|
||||
def _include_block(self, value, context=None):
|
||||
if hasattr(value, 'render_as_block'):
|
||||
if context:
|
||||
new_context = context.get_all()
|
||||
else:
|
||||
new_context = {}
|
||||
|
||||
return jinja2.Markup(value.render_as_block(context=new_context))
|
||||
|
||||
return jinja2.Markup(value)
|
||||
|
||||
# Nicer import names
|
||||
core = WagtailCoreExtension
|
||||
|
Loading…
Reference in New Issue
Block a user