Skip to content

Instantly share code, notes, and snippets.

@mgd020
Last active October 25, 2016 05:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgd020/d841f8016024707b11b490b82ec648f0 to your computer and use it in GitHub Desktop.
Save mgd020/d841f8016024707b11b490b82ec648f0 to your computer and use it in GitHub Desktop.
Registers render_streamfield tag to add current context to streamfield block templates.
'''
Registers render_streamfield tag to add current page context to all block templates.
Usage: {% render_streamfield a_streamfield %}
Author: github.com/mgd020
'''
from django import template
from wagtail.wagtailcore.blocks.base import Block
register = template.Library()
@register.simple_tag(takes_context=True)
def render_streamfield(context, value):
def get_context(self, value):
return dict(context.flatten(), **{
'self': value,
self.TEMPLATE_VAR: value,
})
Block.get_context = get_context
return str(value)
@SalahAdDin
Copy link

Interesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment