From ac1bee89c992c2952a0f6fb8128490e344eb7245 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 5 Feb 2015 12:31:24 +0000 Subject: [PATCH] Have StreamChild.__str__ return the child's native rendering so that it's usable on templates. (StreamChild isn't just syntactic sugar after all, then...) --- wagtail/wagtailadmin/blocks.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/wagtail/wagtailadmin/blocks.py b/wagtail/wagtailadmin/blocks.py index af2d24e52..e3dd620e5 100644 --- a/wagtail/wagtailadmin/blocks.py +++ b/wagtail/wagtailadmin/blocks.py @@ -735,7 +735,7 @@ class BaseStreamBlock(Block): def render(self, value): return format_html_join('\n', '
{0}
', - [(child.render(), child.block_type) for child in value] + [(child, child.block_type) for child in value] ) @@ -750,15 +750,21 @@ class StreamValue(collections.Sequence): (which keep track of block types in a way that the values alone wouldn't). """ + @python_2_unicode_compatible class StreamChild(BoundBlock): - """ - Syntactic sugar so that we can say child.block_type instead of child.block.name. - (This doesn't belong on BoundBlock itself because the idea of block.name denoting - the child's "type" ('heading', 'paragraph' etc) is unique to StreamBlock, and in the - wider context people are liable to confuse it with the block class (CharBlock etc). - """ + """Provides some extensions to BoundBlock to make it more natural to work with on front-end templates""" + def __str__(self): + """Render the value according to the block's native rendering""" + return self.block.render(self.value) + @property def block_type(self): + """ + Syntactic sugar so that we can say child.block_type instead of child.block.name. + (This doesn't belong on BoundBlock itself because the idea of block.name denoting + the child's "type" ('heading', 'paragraph' etc) is unique to StreamBlock, and in the + wider context people are liable to confuse it with the block class (CharBlock etc). + """ return self.block.name def __init__(self, stream_block, stream_data):