Published: Wed 10 Apr 2019

Printing Regions in Views in Different Places Using the Same Template

We had a case where on some views we needed to print the header above the exposed filters and on others we needed to print the header after the exposed filters. Here's my simple solution.

This is rather simple, in my views template (list.twig I call it, as I am using PatternLab), I created the following:

  1. {% if not header_after_exposed and header %}
  2. <div class="view-header">
  3. {{ header }}
  4. </div>
  5. {% endif %}
  6.  
  7. {% if exposed %}
  8. <div class="view-filters">
  9. {{ exposed }}
  10. </div>
  11. {% endif %}
  12.  
  13. {% if header_after_exposed and header %}
  14. <div class="view-header">
  15. {{ header }}
  16. </div>
  17. {% endif %}

Now, in my corresponding views template in Drupal - views-view--search.html.twig - I just use that template and set the header_after_exposed variable to true.

  1. {% set header_after_exposed = true %}
  2. {% include '@content/list/list.twig' %}

If you like what I write, you might like to follow me on Twitter @markconroy

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.