In my main template I can call:
{% load static wagtailuserbar wagtailcore_tags %}
{% load navigation_tags %}
{% if request.user.is_authenticated %}
You're logged in
{% endif %}
but if I call this in my StreamBlock sub-template, it doesn't work.
{% load wagtailcore_tags wagtailimages_tags %}
{% if request.user.is_authenticated %}
<div class="container">
...
</div>
{% endif %}
Any ideas?
When outputting the StreamField onto the page, be sure to use the include_block tag rather than just outputting the value within a {{ ... }}
tag. This ensures that any variables defined on the outer template, including request
, are passed on to the sub-template. If you use {{ ... }}
, the sub-template will still be rendered, but the only available variables are the ones supplied by the block itself.
{% include_block %}
can be used on an individual block, or the stream as a whole:
{% load wagtailcore_tags %}
{% include_block page.body %}
or
{% for block in page.body %}
{% include_block block %}
{% endfor %}
You'll also need to do this within any sub-templates that render sub-sub-templates (for example, if you have a nested StreamBlock within the StreamField, the template for that StreamBlock needs to use include_block
too).