Answers.Body: <p>It is expecting an <a href="http://buildwithcraft.com/docs/templating/paginate#showing-the-elements" rel="nofollow"><code>{% endpaginate %}</code></a>.</p>
  
  <pre>
  {% set categoryName = craft.request.lastSegment %}
  {% if categoryName == 'all' %}
      {% paginate craft.entries.section('photo').limit(6) as entries %}
      ...
      <b><i>{% endpaginate %}</i></b>
  {% else %}
      {% set category = craft.categories.slug(categoryName) %}
      {% paginate craft.entries.section('photo').relatedTo(category).limit(6) as entries %}
      ...
      <b><i>{% endpaginate %}</i></b>
  {% endif %}
  </pre>
  
  <p>Update: if you want to define the paginate block separately to keep your code dry you can assign the element criteria model to a variable first. See carlcs' answer above/below for another way to do the same.</p>
  
  <pre>
  {% set categoryName = craft.request.lastSegment %}
  {% if categoryName == 'all' %}
      {% set ecm = craft.entries.section('photo').limit(6) %}
  {% else %}
      {% set category = craft.categories.slug(categoryName) %}
      {% set ecm = craft.entries.section('photo').relatedTo(category).limit(6) %}
  {% endif %}
  
  {% paginate ecm as entries %}
      ...
  {% endpaginate %}
  </pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-14T17:26:12.567Z
Answers.Text: There is a {% endpaginate %} further down the template after the pagination html though, i should have mentioned that. I'll amend the question.
Answers.UserDisplayName: John
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-14T20:25:03.383Z
Answers.Text: Wrapped your code in `<pre>` tags just to be able to emphasize `{% endpaginate %}` with `<b><i>`; if you don't want to emphasize anything in your code, no need in `<pre>`, you can use standard code blocks.
Answers.UserDisplayName: nicael
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-14T21:40:53.830Z
Answers.Text: Twig is pretty strict with regards start and end statement blocks. If you need to conditionally define a start block then you will need to assign it to a variable as carlcs suggested.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-14T16:33:07.217Z
Answers.LastActivityDate: 2014-12-14T21:55:51.107Z
Answers.LastEditDate: 2014-12-14T21:55:51.107Z
Answers.UserDisplayName: Douglas McDonald
Answers.Body: <p>Including an <code>{% endpaginate %}</code> within your if / else statements, like Douglas mentioned should work. If you don't want to repeat the whole code, you could only set the parameters with the conditionals like so:</p>
  
  <pre><code>{% set params = {
      section: photo,
      limit: 6
  } %}
  
  {% set categoryName = craft.request.lastSegment %}
  
  {% if categoryName != 'all' %}
      {% set category = craft.categories.slug(categoryName) %}
      {% set params = params|merge({ relatedTo: category }) %}
  {% endif %}
  
  {% paginate craft.entries(params) as entries %}
      ...
  {% endpaginate %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-14T18:04:28.023Z
Answers.LastActivityDate: 2014-12-14T18:04:28.023Z
Answers.UserDisplayName: carlcs
Body: <p>I have a page that shows all photos in a category, which is set by the last url segment  and then the results re paginated. What i'm trying to do is say that if the last segment is 'all' then show all photos from all categories. So i'm trying the following code, but getting an error and i cannot see why!</p>
  
  <p>Template Code:</p>
  
  <pre><code>{% set categoryName = craft.request.lastSegment %}
  {% if categoryName == 'all' %}
      {% paginate craft.entries.section('photo').limit(6) as entries %}
  {% else %}
  {% set category = craft.categories.slug(categoryName) %}
      {% paginate craft.entries.section('photo').relatedTo(category).limit(6) as entries %}
  {% endif %}
  </code></pre>
  
  <p>Craft error:</p>
  
  <pre><code>Internal Server Error
  
  Unexpected tag name "else" (expecting closing tag for the "paginate" tag defined near line 12)
  </code></pre>
  
  <p>Line 12 is {% else %}, btw.</p>
  
  <p>I have the endPaginate code further down the page after the pagination html, like so:</p>
  
  <pre><code>{% if paginate.prevUrl %}
          &lt;a href="{{ paginate.prevUrl }}" class="button small expand radius"&gt;Previous Page&lt;/a&gt;
          {% endif %}
  
          {% if paginate.nextUrl %}
          &lt;a href="{{ paginate.nextUrl }}" class="button small expand radius"&gt;Next Page&lt;/a&gt;
          {% endif %}
      &lt;/div&gt;
  &lt;/div&gt;
  {% endpaginate %}
  </code></pre>
  
Tags: <templating><categories>
Title: Error when using if statement in template
ThreadText: # Error when using if statement in template
  
  (Asked by: username_0 on 2014-12-14)
  
  I have a page that shows all photos in a category, which is set by the last url segment and then the results re paginated. What i'm trying to do is say that if the last segment is 'all' then show all photos from all categories. So i'm trying the following code, but getting an error and i cannot see why!
  
  Template Code:
  
  ```
  {% set categoryName = craft.request.lastSegment %}
  {% if categoryName == 'all' %}
      {% paginate craft.entries.section('photo').limit(6) as entries %}
  {% else %}
  {% set category = craft.categories.slug(categoryName) %}
      {% paginate craft.entries.section('photo').relatedTo(category).limit(6) as entries %}
  {% endif %}
  
  ```
  
  Craft error:
  
  ```
  Internal Server Error
  
  Unexpected tag name "else" (expecting closing tag for the "paginate" tag defined near line 12)
  
  ```
  
  Line 12 is {% else %}, btw.
  
  I have the endPaginate code further down the page after the pagination html, like so:
  
  ```
  {% if paginate.prevUrl %}
          <a href="{{ paginate.prevUrl }}" class="button small expand radius">Previous Page</a>
          {% endif %}
  
          {% if paginate.nextUrl %}
          <a href="{{ paginate.nextUrl }}" class="button small expand radius">Next Page</a>
          {% endif %}
      </div>
  </div>
  {% endpaginate %}
  
  ```
  
  **Tags:** templating, categories
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2014-12-14. Score: 3 | Accepted answer
  
  It is expecting an [`{% endpaginate %}`](http://buildwithcraft.com/docs/templating/paginate#showing-the-elements).
  
  ```
  
  {% set categoryName = craft.request.lastSegment %}
  {% if categoryName == 'all' %}
      {% paginate craft.entries.section('photo').limit(6) as entries %}
      ...
      {% endpaginate %}
  {% else %}
      {% set category = craft.categories.slug(categoryName) %}
      {% paginate craft.entries.section('photo').relatedTo(category).limit(6) as entries %}
      ...
      {% endpaginate %}
  {% endif %}
  
  ```
  
  Update: if you want to define the paginate block separately to keep your code dry you can assign the element criteria model to a variable first. See username_3' answer above/below for another way to do the same.
  
  ```
  
  {% set categoryName = craft.request.lastSegment %}
  {% if categoryName == 'all' %}
      {% set ecm = craft.entries.section('photo').limit(6) %}
  {% else %}
      {% set category = craft.categories.slug(categoryName) %}
      {% set ecm = craft.entries.section('photo').relatedTo(category).limit(6) %}
  {% endif %}
  
  {% paginate ecm as entries %}
      ...
  {% endpaginate %}
  
  ```
  
  > **Comments:**
  > **username_0**: There is a {% endpaginate %} further down the template after the pagination html though, i should have mentioned that. I'll amend the question.  
  > **username_2**: Wrapped your code in `<pre>` tags just to be able to emphasize `{% endpaginate %}` with `<b><i>`; if you don't want to emphasize anything in your code, no need in `<pre>`, you can use standard code blocks.  
  > **username_1**: Twig is pretty strict with regards start and end statement blocks. If you need to conditionally define a start block then you will need to assign it to a variable as username_3 suggested.  
  
  ---
  
  ### Answer by username_3 on 2014-12-14. Score: 2
  
  Including an `{% endpaginate %}` within your if / else statements, like username_1 mentioned should work. If you don't want to repeat the whole code, you could only set the parameters with the conditionals like so:
  
  ```
  {% set params = {
      section: photo,
      limit: 6
  } %}
  
  {% set categoryName = craft.request.lastSegment %}
  
  {% if categoryName != 'all' %}
      {% set category = craft.categories.slug(categoryName) %}
      {% set params = params|merge({ relatedTo: category }) %}
  {% endif %}
  
  {% paginate craft.entries(params) as entries %}
      ...
  {% endpaginate %}
  
  ```
  
  
  ---
  
  