Templates for StreamBlock

535 views
Skip to first unread message

Florian Vogt

unread,
Aug 19, 2015, 6:37:10 PM8/19/15
to Wagtail support
Hey everyone. I hope you can help me with my question.

Is it possible to render a template for StreamBlock?
If yes, how to do it? Which variables should I use the template?

As example a tab feature:
class FeatureTab(blocks.StreamBlock):
    tab
= blocks.StructBlock([
       
('head', blocks.CharBlock(label='Tab Name')),
       
('text', blocks.CharBlock(label='Tab Text'))
   
])

   
class Meta:
        label
= 'A Tab Feature'
       
template = 'home/blocks/features/Tab.html'

With this example a user can create a couple of tabs.
Now I need the tabs with the format:

head1
head2
head3

text1
text2
text3

I have tried a lot but get no result.
Does anyone have an approach?

Thanks in advance.
Flori

Matthew Westcott

unread,
Aug 19, 2015, 7:10:47 PM8/19/15
to wag...@googlegroups.com
Hi Flori,
Your Tab.html template should look like this:

{% for block in self %}
{# each block is an object with properties 'block_type' (always 'tab' here)
and 'value' (the block's value - for a StructBlock, this is a dict-like object) #}
{{ block.value.head }}
{% endfor %}
{% for block in self %}
{{ block.value.text }}
{% endfor %}

Cheers,
- Matt

Florian Vogt

unread,
Aug 19, 2015, 7:22:04 PM8/19/15
to Wagtail support
Hi Matt,

thanks for your fast help.
Now I finally understood it.

Cheers
Flori

Viktor Hunting

unread,
Feb 20, 2018, 12:10:47 PM2/20/18
to Wagtail support
Hi,

Thanks. That was very helpful.

Is this mentioned in documentation? Maybe I missed it

четвер, 20 серпня 2015 р. 02:10:47 UTC+3 користувач Matthew Westcott написав:

Matthew Westcott

unread,
Feb 20, 2018, 12:21:06 PM2/20/18
to wag...@googlegroups.com
Yep, it's all covered here:

http://docs.wagtail.io/en/v1.13.1/topics/streamfield.html#template-rendering

Cheers,
- Matt
> --
> You received this message because you are subscribed to the Google Groups "Wagtail support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to wagtail+u...@googlegroups.com.
> To post to this group, send email to wag...@googlegroups.com.
> Visit this group at https://groups.google.com/group/wagtail.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/wagtail/36bf24fb-7be5-45f3-9ead-6e3c187426d8%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages