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

CC BY-SA 3.0
2014-12-14T17:26:12.567Z
There is a {% endpaginate %} further down the template after the pagination html though, i should have mentioned that. I'll amend the question.
John
CC BY-SA 3.0
2014-12-14T20:25:03.383Z
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.
nicael
CC BY-SA 3.0
2014-12-14T21:40:53.830Z
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.
Douglas McDonald
CC BY-SA 3.0
2014-12-14T16:33:07.217Z
2014-12-14T21:55:51.107Z
2014-12-14T21:55:51.107Z
Douglas McDonald
<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>

CC BY-SA 3.0
2014-12-14T18:04:28.023Z
2014-12-14T18:04:28.023Z
carlcs
<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>

<templating><categories>
Error when using if statement in template
# 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 %}
        Previous Page
        {% endif %}

        {% if paginate.nextUrl %}
        Next Page
        {% endif %}
    

{% endpaginate %}

**Tags:** templating, categories


---

## 2 answers

### Answer by username_1 on 2014-12-14. Score: 3 | Accepted answer

It is expecting an {% endpaginate %}.

{% 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 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:
{% 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 %}


---

