1

I'm trying to create a Twig view that renders:

  • Intro block
  • View content
  • outro block (yes, made up word for the opposite of intro)

The reason I'm putting these three things in one view is that I have a repeating background pattern; if each item is rendered alone, the background does not display smoothly across/through each element.

I've been trying to put this line - {% include 'block--intro.html.twig' %}, based upon block.html.twig - into the appropriate place of the view twig file, but when the page containing this view is rendered the {{ contents }} of the block is empty.

How should I be trying to do this?

I am using Drupal 8.2.4. The base theme is Bootstrap; we are using a subtheme of that.

4

You can't mix different templates types like views and block templates. Every template has other variables and so the block template doesn't find the content variable in the views template context.

In your case for a View you could place the block in the header or footer in Views UI. For example a Rendered entity - Block.

In general you can use the module Twig Tweak instead of {% include %} to render a block template or any other entity template:

{{ drupal_entity('block_content', 1) }}

This works, because it renders the entity in its own variable scope and so you can place it everywhere in twig.


While entities normally render in their own template, block content is special, because it doesn't have a template on its own. So the command above only shows the fields. To get the block wrapper you need to use drupal_block().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.