Skip to content

Commit

Permalink
fix broken links in pagination bar (#24)
Browse files Browse the repository at this point in the history
* adding twig arbitrary collection support

* complete version

Oops, some code missing from 1st version.

* set ignore_url_params to false

* twig pagination latest version

* specify which url params to ignore. others will be unaffected.
* bugfix: default argument now working.
* removed unnecessary property from yaml

* copy error corrected

* copy error corrected

* typecast parameter to array before iterating

Typecasting the parameter to array prevents two types of errors with
the foreach construct:

- unset variable is converted to empty array
- non array types are converted to array

* check isset() necessary to prevent exception

This check - which was unjustly removed in a previous version - is
still necessary to prevent index error. $params[‘ignore_url_params’] is
not set when paginating via the onCollectionProcessed event.

* twig function usage added to README

* added code blocks to README

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* fix broken pagination links

Replacing the double slash from the scheme creates a malformed url and
breaks functionality in some browsers. Fix: put base_url outside
brackets, it presumably doesn’t need ‘fixing’.
  • Loading branch information
gigago authored and rhukster committed Mar 27, 2017
1 parent bb9c8dc commit cfb8064
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ This creates a paginated collection with `limit` items per page. As usual, any u

### Extended usage

```twig
{% set collection = page.find( '/other/_events' ).children %}
{% set limit = 5 %}
{% set ignore_url_param_array = [ 'event' ] %}
{% do paginate( collection, limit, ignore_url_param_array ) %}
```

The above example is taken from http://ami-web.nl/events. This code creates a paginated collection with 5 items per page (the event summary list) which is presented together with an active event. The active event appears in only one of the summary pages. Consequently, the url parameter 'event' should be filtered out so it does not show up in the pagination bar's links, preventing inconsistencies with different page parameters. Any non listed url parameters (except the page parameter) are passed through unaffected. The requested page contains logic to pick a sensible default event.

### Rendering the paginated collection

The rest is identical to the standard procedure.

```twig
{% set collection = page.find('/other/_events').children %}
{% set limit = 5 %}
Expand All @@ -134,7 +147,7 @@ This creates a paginated collection with `limit` items per page. As usual, any u

The above example is taken from http://ami-web.nl/events. This code creates a paginated collection with 5 items per page (the event summary list) which is presented together with an active event. The active event appears in only one of the summary pages. Consequently, the url parameter `event` should be filtered out so it does not show up in the pagination bar's links, preventing inconsistencies with different page parameters. Any non listed url parameters (except the page parameter) are passed through unaffected. The requested page contains logic to pick a sensible default event.

### Rendering the paginated collection
## Rendering the paginated collection

The rest is identical to the standard procedure:

Expand Down
6 changes: 3 additions & 3 deletions templates/partials/pagination.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<ul class="pagination">
{% if pagination.hasPrev %}
{% set url = (base_url ~ pagination.params ~ pagination.prevUrl)|replace({'//':'/'}) %}
{% set url = base_url ~ (pagination.params ~ pagination.prevUrl)|replace({'//':'/'}) %}
<li><a rel="prev" href="{{ url }}">&laquo;</a></li>
{% else %}
<li><span>&laquo;</span></li>
Expand All @@ -16,15 +16,15 @@
{% if paginate.isCurrent %}
<li><span>{{ paginate.number }}</span></li>
{% elseif paginate.isInDelta %}
{% set url = (base_url ~ pagination.params ~ paginate.url)|replace({'//':'/'}) %}
{% set url = base_url ~ (pagination.params ~ paginate.url)|replace({'//':'/'}) %}
<li><a href="{{ url }}">{{ paginate.number }}</a></li>
{% elseif paginate.isDeltaBorder %}
<li class="gap"><span>&hellip;</span></li>
{% endif %}

{% endfor %}
{% if pagination.hasNext %}
{% set url = (base_url ~ pagination.params ~ pagination.nextUrl)|replace({'//':'/'}) %}
{% set url = base_url ~ (pagination.params ~ pagination.nextUrl)|replace({'//':'/'}) %}
<li><a rel="next" href="{{ url }}">&raquo;</a></li>
{% else %}
<li><span>&raquo;</span></li>
Expand Down

0 comments on commit cfb8064

Please sign in to comment.