From 2ad0366fa0a87c43f68d4d96c784611195ff7d7f Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 5 Feb 2015 15:00:56 +0000 Subject: [PATCH] make the base Block.render template-aware, so that any subclass can define a 'template' property to override the default rendering (which is now provided by block.render_basic()) --- wagtail/wagtailadmin/blocks.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/wagtail/wagtailadmin/blocks.py b/wagtail/wagtailadmin/blocks.py index e3dd620e5..ea008f1c4 100644 --- a/wagtail/wagtailadmin/blocks.py +++ b/wagtail/wagtailadmin/blocks.py @@ -184,7 +184,20 @@ class Block(object): def render(self, value): """ - Return a text rendering of 'value', suitable for display on templates. + Return a text rendering of 'value', suitable for display on templates. By default, this will + use a template if a 'template' property is specified on the block, and fall back on render_basic + otherwise. + """ + template = getattr(self, 'template', None) + if template: + return render_to_string(template, {'self': value}) + else: + return self.render_basic(value) + + def render_basic(self, value): + """ + Return a text rendering of 'value', suitable for display on templates. render() will fall back on + this if the block does not define a 'template' property. """ return force_text(value) @@ -403,9 +416,6 @@ class BaseStructBlock(Block): for name, val in value.items() ]) - def render(self, value): - return render_to_string(self.template, {'self': value}) - @python_2_unicode_compatible # provide equivalent __unicode__ and __str__ methods on Py2 class StructValue(collections.OrderedDict): def __init__(self, block, *args): @@ -579,6 +589,12 @@ class ListBlock(Block): for item in value ] + def render_basic(self, value): + children = format_html_join('\n', '
  • {0}
  • ', + [(self.child_block.render(child_value),) for child_value in value] + ) + return format_html("", children) + # =========== # StreamBlock @@ -733,7 +749,7 @@ class BaseStreamBlock(Block): for child in value # child is a BoundBlock instance ] - def render(self, value): + def render_basic(self, value): return format_html_join('\n', '
    {0}
    ', [(child, child.block_type) for child in value] )