<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>

CC BY-SA 3.0
2017-10-06T15:33:00.583Z
Yeah, that is what I'm doing pretty much. Thanks!
fodney
CC BY-SA 3.0
2017-10-02T02:10:11.097Z
2017-10-02T02:10:11.097Z
Marion Newlevant
<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>

CC BY-SA 3.0
2017-09-30T00:27:17.647Z
Seems pretty legit to me.  I assume you've been through https://craftcms.com/support/entry-archive ?
Brad Bell
<archives>
Is it possible to use same template for index and archive template?
# 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')") %}
    Entries from {{ date }}

    {% for entry in entries %}
        
            {% for asset in entry.newsthumb %}
                
                     
                
            {% endfor %}

            
                {{ entry.title }}

                {{ entry.postDate.format('F d, Y') }} 

                {{ entry.newssummary }}

                {% if entry.newscategory | length %}{% endif %}
                {% nav category in entry.newscategory %}
                    {{ category.title }}{{ not loop.last ? ', ' }}
                {% endnav %}

                {% if entry.newscategory | length %}{% endif %}
            
        
    {% 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!  

---

