Answers.Body: <p>The way I do this is to use a different template for each set of entries. So there would be an archive one for all the articles from a given month: <code>_news/index-archive</code>, and a category one for all the articles in a given category: <code>_news/index-category</code>, and one for all of the entries: <code>_news/index-all</code>, maybe one for articles by tag, etc.</p>
  
  <p>Each of these templates sets <code>entries</code> to the relevant entries, and then does</p>
  
  <pre><code>{% include '_news/index-generic' with {
    entries: entries
  } only %}
  </code></pre>
  
  <p>And <code>index-generic</code> has all the shared display code.</p>
  
  <p>Probably there will be a couple of other things that vary as well, for example a heading:</p>
  
  <pre><code>{% include '_news/index-generic' with {
    entries: entries,
    heading: 'Category: '~category.title
  } only %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-06T15:33:00.583Z
Answers.Text: Yeah, that is what I'm doing pretty much. Thanks!
Answers.UserDisplayName: fodney
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-02T02:10:11.097Z
Answers.LastActivityDate: 2017-10-02T02:10:11.097Z
Answers.UserDisplayName: Marion Newlevant
Body: <p>I'm new to Craft from ExpressionEngine and wondered how people handle Month/Year archive pages.  I have figured out how to get the archives to display but the code seems overly complex.  I wondered if there is a more straightforward approach or if it is possible to display your archive using the same template as the news/index  which displays all. In EE this was possible and just wondered if it was in Craft.</p>
  
  <p>Thanks in advance.  My archive template code below.</p>
  
  <pre><code>{% if month is not defined %}
      {% set before = year + 1 %}
      {% set after  = year %}
  {% else %}
      {% if month == 12 %}
          {% set yearBefore = year + 1 %}
          {% set monthBefore = '01' %}
  
          {% set before = yearBefore~'-'~monthBefore %}
          {% set after = year~'-'~month %}
      {% else %}
          {% set before = year ~ '-' ~ (month + 1) %}
          {% set after  = year ~ '-' ~ month %}
      {% endif %}
  {% endif %}
  
  {% set allEntries = craft.entries.section('news').limit(null).after(after).before(before) %}
  
  {% for date, entries in allEntries | group("postDate|date('F Y')") %}
      &lt;h3&gt;Entries from {{ date }}&lt;/h3&gt;
  
      {% for entry in entries %}
          &lt;article class="post"&gt;
              {% for asset in entry.newsthumb %}
                  &lt;div class="image-wrapp"&gt;
                      &lt;a href="{{ entry.url }}"&gt;&lt;img src="{{ asset.url('newsthumb') }}" alt="{{ asset.title }}"&gt; &lt;/a&gt;
                  &lt;/div&gt;
              {% endfor %}
  
              &lt;div class="text-wrapp"&gt;
                  &lt;h3&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/h3&gt;
  
                  &lt;time datetime="2017-07-27" class="post-time"&gt;{{ entry.postDate.format('F d, Y') }} &lt;/time&gt;
  
                  {{ entry.newssummary }}
  
                  {% if entry.newscategory | length %}&lt;ul class="category-list"&gt;{% endif %}
                  {% nav category in entry.newscategory %}
                      &lt;li&gt;&lt;a href="{{ category.url }}"&gt;{{ category.title }}{{ not loop.last ? ', ' }}&lt;/a&gt;&lt;/li&gt;
                  {% endnav %}
  
                  {% if entry.newscategory | length %}&lt;/ul&gt;{% endif %}
              &lt;/div&gt;
          &lt;/article&gt;
      {% endfor %}
  {% endfor %}
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-30T00:27:17.647Z
Comments.Text: Seems pretty legit to me.  I assume you've been through https://craftcms.com/support/entry-archive ?
Comments.UserDisplayName: Brad Bell
Tags: <archives>
Title: Is it possible to use same template for index and archive template?
ThreadText: # Is it possible to use same template for index and archive template?
  
  (Asked by: username_0 on 2017-09-29)
  
  I'm new to Craft from ExpressionEngine and wondered how people handle Month/Year archive pages. I have figured out how to get the archives to display but the code seems overly complex. I wondered if there is a more straightforward approach or if it is possible to display your archive using the same template as the news/index which displays all. In EE this was possible and just wondered if it was in Craft.
  
  Thanks in advance. My archive template code below.
  
  ```
  {% if month is not defined %}
      {% set before = year + 1 %}
      {% set after  = year %}
  {% else %}
      {% if month == 12 %}
          {% set yearBefore = year + 1 %}
          {% set monthBefore = '01' %}
  
          {% set before = yearBefore~'-'~monthBefore %}
          {% set after = year~'-'~month %}
      {% else %}
          {% set before = year ~ '-' ~ (month + 1) %}
          {% set after  = year ~ '-' ~ month %}
      {% endif %}
  {% endif %}
  
  {% set allEntries = craft.entries.section('news').limit(null).after(after).before(before) %}
  
  {% for date, entries in allEntries | group("postDate|date('F Y')") %}
      <h3>Entries from {{ date }}</h3>
  
      {% for entry in entries %}
          <article class="post">
              {% for asset in entry.newsthumb %}
                  <div class="image-wrapp">
                      <a href="{{ entry.url }}"><img src="{{ asset.url('newsthumb') }}" alt="{{ asset.title }}"> </a>
                  </div>
              {% endfor %}
  
              <div class="text-wrapp">
                  <h3><a href="{{ entry.url }}">{{ entry.title }}</a></h3>
  
                  <time datetime="2017-07-27" class="post-time">{{ entry.postDate.format('F d, Y') }} </time>
  
                  {{ entry.newssummary }}
  
                  {% if entry.newscategory | length %}<ul class="category-list">{% endif %}
                  {% nav category in entry.newscategory %}
                      <li><a href="{{ category.url }}">{{ category.title }}{{ not loop.last ? ', ' }}</a></li>
                  {% endnav %}
  
                  {% if entry.newscategory | length %}</ul>{% endif %}
              </div>
          </article>
      {% endfor %}
  {% endfor %}
  
  ```
  
  **Tags:** archives
  
  > **Comments:**
  > **username_1**: Seems pretty legit to me.  I assume you've been through https://craftcms.com/support/entry-archive ?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2017-10-02. Score: 1
  
  The way I do this is to use a different template for each set of entries. So there would be an archive one for all the articles from a given month: `_news/index-archive`, and a category one for all the articles in a given category: `_news/index-category`, and one for all of the entries: `_news/index-all`, maybe one for articles by tag, etc.
  
  Each of these templates sets `entries` to the relevant entries, and then does
  
  ```
  {% include '_news/index-generic' with {
    entries: entries
  } only %}
  
  ```
  
  And `index-generic` has all the shared display code.
  
  Probably there will be a couple of other things that vary as well, for example a heading:
  
  ```
  {% include '_news/index-generic' with {
    entries: entries,
    heading: 'Category: '~category.title
  } only %}
  
  ```
  
  > **Comments:**
  > **username_0**: Yeah, that is what I'm doing pretty much. Thanks!  
  
  ---
  
  