Answers.Body: <p>You could pass the content to the sidebar include, as follows:</p>
  
  <pre><code>{% include 'sidebar-template' with {
      content: entry.sidebarContent
  } only %}
  </code></pre>
  
  <p>The sidebar template would then use <code>content</code>, rather than <code>entry.sidebarContent</code>.</p>
  
  <p>It's also worth taking a look at <a href="http://twig.sensiolabs.org/doc/1.x/tags/embed.html" rel="nofollow noreferrer">Twig embeds</a>, which can be very useful in situations such as this.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-30T16:42:44.960Z
Answers.LastActivityDate: 2017-03-30T16:42:44.960Z
Answers.UserDisplayName: Stephen Lewis
Body: <p>I'm struggling to combine two almost identical sidebar templates.    </p>
  
  <p>I’m working on a site that has:</p>
  
  <ol>
  <li>a blog channel section with an index template that ‘includes’ a ‘sidebar-main.html’ template</li>
  <li>a structure section with a page template that ‘includes’ the same ‘sidebar-main.html’ template</li>
  <li>a category template that lists blog posts for a single category, but which ‘includes’ a different ‘sidebar-category.html’ template</li>
  </ol>
  
  <p>The ‘sidebar-main.html’ and ‘sidebar-category.html’ templates are almost identical. Their main function is to offer the user a choice of an 'All categories' menu sorted by category, or a 'Top categories' menu sorted by number of posts, selectable in the entry control panel.</p>
  
  <p>The 'sidebar-main.html template starts:</p>
  
  <pre><code>&lt;div class="sidebar column"&gt;
  
      &lt;div  class="sticky-container" data-sticky-container&gt;
          &lt;div class="sticky" data-sticky data-anchor="stickyAnchor" data-sticky-on="large" style="width:100%"&gt;
  
      {% if entry.sidebarContent is defined %}
  
          {% for block in entry.sidebarContent %}
  
              {% if block.type == "sidebarWidget" %}
                  {% if block.widget.contains('topCategories') %}
  
                  &lt;h4 class="section-title"&gt;Top categories&lt;/h4&gt;
                  &lt;div class="card"&gt;
                      &lt;div class="card-section"&gt;
  
                      {% set entryIds = craft.entries.section('blog').ids() %}
                      {% set allCats = craft.categories.relatedTo(entryIds).level(1).find() %}
                      {% set catArray = {} %} 
  
                      {% for category in allCats %}
                        {% set catArray = catArray | merge({(category.slug):craft.entries.relatedTo(category).total() }) %}
                      {% endfor %}
  
                      {% set catArray = catArray|sort|reverse %}
  
                      {% for catSlug, entryCount in catArray | slice(0,3) %}
                      {% set category = craft.categories.slug(catSlug).first() %}
  
                      {% if loop.first %}
                          &lt;div class="row"&gt;
                              &lt;div class="first-column small-6 large-12 columns"&gt;
                                  &lt;ul&gt;
                      {% endif %} 
  
                                      &lt;li class="clearfix"&gt;
                                          &lt;a href="{{ category.url }}"&gt;{{ category.title }}&amp;nbsp;&lt;/a&gt;
                                          &lt;div class="count"&gt;{{ entryCount }}{% if entryCount == 1 %}&amp;nbsp;post{% else %}&amp;nbsp;posts{% endif %}&lt;/div&gt;
                                      &lt;/li&gt;
  </code></pre>
  
  <p>And the 'sidebar-category.html' template is identical to this, except that <code>entry.sidebarContent</code> is replaced by <code>category.sidebarContent</code>. This works fine, with no errors or problems (except it's not DRY). However, if I can eliminate the use of 'category.sidebarContent', then I can split up the combined template to enable its use in 404 and search etc pages.</p>
  
  <p>I hope all that makes some sort of sense, and thanks for any suggestions!</p>
  
Tags: <templating><categories>
Title: Combine two almost identical sidebar templates, used for blog index and category index pages
ThreadText: # Combine two almost identical sidebar templates, used for blog index and category index pages
  
  (Asked by: username_0 on 2017-03-30)
  
  I'm struggling to combine two almost identical sidebar templates.
  
  I’m working on a site that has:
  
  1. a blog channel section with an index template that ‘includes’ a ‘sidebar-main.html’ template
  2. a structure section with a page template that ‘includes’ the same ‘sidebar-main.html’ template
  3. a category template that lists blog posts for a single category, but which ‘includes’ a different ‘sidebar-category.html’ template
  
  The ‘sidebar-main.html’ and ‘sidebar-category.html’ templates are almost identical. Their main function is to offer the user a choice of an 'All categories' menu sorted by category, or a 'Top categories' menu sorted by number of posts, selectable in the entry control panel.
  
  The 'sidebar-main.html template starts:
  
  ```
  <div class="sidebar column">
  
      <div  class="sticky-container" data-sticky-container>
          <div class="sticky" data-sticky data-anchor="stickyAnchor" data-sticky-on="large" style="width:100%">
  
      {% if entry.sidebarContent is defined %}
  
          {% for block in entry.sidebarContent %}
  
              {% if block.type == "sidebarWidget" %}
                  {% if block.widget.contains('topCategories') %}
  
                  <h4 class="section-title">Top categories</h4>
                  <div class="card">
                      <div class="card-section">
  
                      {% set entryIds = craft.entries.section('blog').ids() %}
                      {% set allCats = craft.categories.relatedTo(entryIds).level(1).find() %}
                      {% set catArray = {} %} 
  
                      {% for category in allCats %}
                        {% set catArray = catArray | merge({(category.slug):craft.entries.relatedTo(category).total() }) %}
                      {% endfor %}
  
                      {% set catArray = catArray|sort|reverse %}
  
                      {% for catSlug, entryCount in catArray | slice(0,3) %}
                      {% set category = craft.categories.slug(catSlug).first() %}
  
                      {% if loop.first %}
                          <div class="row">
                              <div class="first-column small-6 large-12 columns">
                                  <ul>
                      {% endif %} 
  
                                      <li class="clearfix">
                                          <a href="{{ category.url }}">{{ category.title }}&nbsp;</a>
                                          <div class="count">{{ entryCount }}{% if entryCount == 1 %}&nbsp;post{% else %}&nbsp;posts{% endif %}</div>
                                      </li>
  
  ```
  
  And the 'sidebar-category.html' template is identical to this, except that `entry.sidebarContent` is replaced by `category.sidebarContent`. This works fine, with no errors or problems (except it's not DRY). However, if I can eliminate the use of 'category.sidebarContent', then I can split up the combined template to enable its use in 404 and search etc pages.
  
  I hope all that makes some sort of sense, and thanks for any suggestions!
  
  **Tags:** templating, categories
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-03-30. Score: 2 | Accepted answer
  
  You could pass the content to the sidebar include, as follows:
  
  ```
  {% include 'sidebar-template' with {
      content: entry.sidebarContent
  } only %}
  
  ```
  
  The sidebar template would then use `content`, rather than `entry.sidebarContent`.
  
  It's also worth taking a look at [Twig embeds](http://twig.sensiolabs.org/doc/1.x/tags/embed.html), which can be very useful in situations such as this.
  
  
  ---
  
  