1

I have an issue where I have a StrictBlock within a StreamField:

class DetailsTableBlock(StructBlock):
    title = CharBlock(required=False)
    description = RichTextBlock(required=False)
    table = TableBlock(template="home/blocks/table.html")

class MainStreamBlock(StreamBlock):
    ....
    table = DetailsTableBlock()

The problem occurs when I try to render the table using:

{{ child.value.table }}

all I get is:

{u'data': [[u'test', u'test', u'test'], [u'123', u'asd', u'asd'], [u'123', u'asd', u'asd']], u'first_row_is_table_header': True, u'first_col_is_header': False}

So the question is how can I render html using a StructBlock inside a StreamField? I am using Wagtail 1.7

Dimitar Petrov
  • 667
  • 1
  • 5
  • 16

1 Answers1

5

You should use: {{ child.value.bound_blocks.table }}

The full explanation is given at wagtail documentation, but in short: when you're looping over the content of a StreamField to output it, you'll sometimes get the raw data value, and sometimes get a BoundBlock object which knows both the value and how to render it as HTML. When you access the child values of a StructBlock, you get the raw values (since this is usually the more useful thing to access inside a StructBlock template) - to get the BoundBlock object, you need to read it from the StructBlock's bound_blocks dictionary instead.

Bikash kharel
  • 472
  • 7
  • 16
gasman
  • 23,691
  • 1
  • 38
  • 56