Answers.Body: <p>Yup, you can totally re-use the same template. When it is a category page, you will have access to a <code>category</code> variable which is a <a href="https://craftcms.com/docs/templating/categorymodel" rel="nofollow">CategoryModel</a>, and you can use that to filter your entries by category.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-14T13:58:25.300Z
Answers.Text: Trevor, how would I use the CategoryModel to limit the results to the category if on a category page and list all results if on a regular listing page. i.e. /blog/ or /blog/p2 vs /blog/category or /blog/category/p2 I looked at the docs for that page and it wasn't immediately clear how to do this.
Answers.UserDisplayName: CreateSean
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-17T14:25:46.917Z
Answers.Text: `{% set params = {
      section: 'blog',
      limit: 5
  } %}
  
  {% if category is defined %}
      {% set params = params | merge({ relatedTo: category }) %}
  {% endif %}
  
  {% set blogEntries = craft.entries(params) %}`
Answers.UserDisplayName: Trevor Davis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-18T14:29:17.520Z
Answers.Text: Thanks Travis, got this working and so simple, but I wouldn't never have figured it out on my own.
Answers.UserDisplayName: CreateSean
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-13T20:25:18.807Z
Answers.LastActivityDate: 2016-10-13T20:25:18.807Z
Answers.UserDisplayName: Trevor Davis
Answers.Body: <p>I'm confused: if the supplied code is what you're using for both the index page and category page then it won't filter by category because there's no code to tell it to do that.</p>
  
  <p>Category would need something like:</p>
  
  <pre><code>{% set entries = craft.entries.relatedTo(category) %}
  {% for entry in entries %}
      &lt;li&gt;
          &lt;a href="{{ entry.url }}"&gt;
              &lt;h2&gt;{{ entry.title }}&lt;/h2&gt;
              &lt;p class="meta"&gt;Posted: {{ entry.postDate|date("d.m.Y") }}&lt;/p&gt;
          &lt;/a&gt;
      &lt;/li&gt;
  {% endfor %}
  </code></pre>
  
  <p>Rather than trying to use the same template - given the logic is actually slightly different - I'd perhaps suggest setting up a shared template to extend from (i.e., another _layout.twig).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-13T14:08:23.820Z
Answers.LastActivityDate: 2016-10-13T14:08:23.820Z
Answers.UserDisplayName: Matt Wilcox
Body: <p>I have a blog listing page which works correctly. I also have categories that I would like to use the same template rather than creating a whole new template since the design is exactly the same.</p>
  
  <p>However when I set the category template to <code>blog/index</code> the page loads correctly but doesn't limit by categories.</p>
  
  <p>Looking at my simplified template code, I'm not sure how to get it to display only the category being viewed or all if on the blog index page.</p>
  
  <p>My urls are </p>
  
  <pre><code>/blog/
  /blog/p2 (pagination)
  /blog/{{ slug }}
  </code></pre>
  
  <p>I would like the following url's to use the same template code:</p>
  
  <pre><code>/blog/category/{{ category_url }}
  /blog/category/{{ category_url }}/p2
  </code></pre>
  
  <p>My template code:</p>
  
  <pre><code>{% paginate craft.entries.section('blog').limit(6) as pageInfo, pageEntries %}
  {% for entry in pageEntries %}
    &lt;article class="col-sm-6 block" data-mh="blog-rows"&gt;
  
      some code
  
      &lt;aside&gt;
        more code
      &lt;/aside&gt;
  
      &lt;a href="{{ entry.url }}" class="btn btn-primary"&gt;Keep Reading&lt;/a&gt;
  
    &lt;/article&gt;&lt;!--block--&gt;
  {% endfor %}
  </code></pre>
  
  <p>If necessary I can create a <code>category.html</code> template, but then I'd essentially have duplicate code. Is there a way to do what I want to?</p>
  
Tags: <templating><categories>
Title: Use blog index template as category template
ThreadText: # Use blog index template as category template
  
  (Asked by: username_0 on 2016-10-12)
  
  I have a blog listing page which works correctly. I also have categories that I would like to use the same template rather than creating a whole new template since the design is exactly the same.
  
  However when I set the category template to `blog/index` the page loads correctly but doesn't limit by categories.
  
  Looking at my simplified template code, I'm not sure how to get it to display only the category being viewed or all if on the blog index page.
  
  My urls are
  
  ```
  /blog/
  /blog/p2 (pagination)
  /blog/{{ slug }}
  
  ```
  
  I would like the following url's to use the same template code:
  
  ```
  /blog/category/{{ category_url }}
  /blog/category/{{ category_url }}/p2
  
  ```
  
  My template code:
  
  ```
  {% paginate craft.entries.section('blog').limit(6) as pageInfo, pageEntries %}
  {% for entry in pageEntries %}
    <article class="col-sm-6 block" data-mh="blog-rows">
  
      some code
  
      <aside>
        more code
      </aside>
  
      <a href="{{ entry.url }}" class="btn btn-primary">Keep Reading</a>
  
    </article><!--block-->
  {% endfor %}
  
  ```
  
  If necessary I can create a `category.html` template, but then I'd essentially have duplicate code. Is there a way to do what I want to?
  
  **Tags:** templating, categories
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-10-13. Score: 4 | Accepted answer
  
  Yup, you can totally re-use the same template. When it is a category page, you will have access to a `category` variable which is a [CategoryModel](https://craftcms.com/docs/templating/categorymodel), and you can use that to filter your entries by category.
  
  > **Comments:**
  > **username_0**: username_1, how would I use the CategoryModel to limit the results to the category if on a category page and list all results if on a regular listing page. i.e. /blog/ or /blog/p2 vs /blog/category or /blog/category/p2 I looked at the docs for that page and it wasn't immediately clear how to do this.  
  > **username_1**: `{% set params = {     section: 'blog',     limit: 5 } %}  {% if category is defined %}     {% set params = params | merge({ relatedTo: category }) %} {% endif %}  {% set blogEntries = craft.entries(params) %}`  
  > **username_0**: Thanks Travis, got this working and so simple, but I wouldn't never have figured it out on my own.  
  
  ---
  
  ### Answer by username_2 on 2016-10-13. Score: 2
  
  I'm confused: if the supplied code is what you're using for both the index page and category page then it won't filter by category because there's no code to tell it to do that.
  
  Category would need something like:
  
  ```
  {% set entries = craft.entries.relatedTo(category) %}
  {% for entry in entries %}
      <li>
          <a href="{{ entry.url }}">
              <h2>{{ entry.title }}</h2>
              <p class="meta">Posted: {{ entry.postDate|date("d.m.Y") }}</p>
          </a>
      </li>
  {% endfor %}
  
  ```
  
  Rather than trying to use the same template - given the logic is actually slightly different - I'd perhaps suggest setting up a shared template to extend from (i.e., another \_layout.twig).
  
  
  ---
  
  