1

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 %}
3

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 carlcs' 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 %}
  • There is a {% endpaginate %} further down the template after the pagination html though, i should have mentioned that. I'll amend the question. – John Dec 14 '14 at 17:26
  • 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 Dec 14 '14 at 20:25
  • 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 Dec 14 '14 at 21:40
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 %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.