Hi Timo,
Any time you've retrieved a StreamField value (e.g. by accessing value.specific.body within a template), you have the option of iterating over it rather than rendering it directly - this will give you a sequence of objects with 'block_type' and 'value' properties:
{% for block in value.specific.body %}
{% if block.block_type == 'person' %}
<li>{{ block.value.first_name }}</li>
{% endif %}
{% endfor %}
As Brett mentions, it's often cleaner to do this sort of logic within Python code rather than in the template. There are a couple of approaches that would work here, the simplest being to add a method to the TestPage model:
class TestPage(CorePage):
...
def contributor_names(self):
return [block.value.first_name for block in self.body if block.block_type == 'person']
- and then accessing {{ value.specific.contributor_names }} in the template. Alternatively, as of Wagtail 1.2, all block types support a 'get_context' method for passing additional context variables to the block's template - you'd define this on your servicepage block by subclassing PageChooserBlock (rather than using PageChooserBlock directly):
class ServicePageBlock(blocks.PageChooserBlock):
def get_context(self, value):
context = super(EventBlock, self).get_context(value)
context['contributor_names'] = [block.value.first_name for block in self.body if block.block_type == 'person']
return context
class Meta:
template = 'mysite/blocks/page.html'
'contributor_names' will then be available as a variable on mysite/blocks/page.html.
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
http://groups.google.com/group/wagtail.
> To view this discussion on the web, visit
https://groups.google.com/d/msgid/wagtail/020b542c-c075-494e-83b7-c76df73b5f5f%40googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.