Pagination plugin
Testing a simple pagination
This plugin is pretty much a phile adaptation of Pico pagination plugin,. You use it exactly the same way. So read that documentation.
It is just proof of concept. Use it to learn about plugins or find a better way to do it. This works, but may not be efficient for large blogs. This plugin is used on the 5 minute blog tutorial. Read that and see it in action.
Required Core.php modification
Phile does not let a plugin modify a uri. So you will get 404 when you have pages with a name (i.e. somepagename
) but you add some parameters after page name. For example:
somepagename/page/2
The real uri here is only somepagename
but phile won't let your plugin change it back from somepagename/page/2
to just somepagename
, and therefore, you will get a 404 error page from phile. (Note, THIS ISSUE DOES NOT exist in PICO.) Hopefully the phile team will find a solution.
So, my solution involved changeing 1 character in the core.php initCurrentPage()
method From:
Event::triggerEvent('request_uri', array('uri' => $uri));
to:
Event::triggerEvent('request_uri', array('uri' => &$uri));
How it works
It's pretty simple. The plugin creates a second array of pages identical to the default pages
called paged_pages
. This second array is filtered by date (so only files that have a meta "date" value set will be returned) and limited by the number of pages you want per page. (You can use any meta field, it does not have to be date.)
Listing your paged results, therefore, is just a matter of iterating through the paged_pages
variable instead of the phile default pages
variable.
You can add this pagination by simply adding the two twig tags paged_pages
and pagination_links
in your theme templates as shown here:
<h2>Latest Posts</h2>
{% for page in paged_pages %}
{# just show all pages, i did not add meta 'template: post' to all the posts #}
<article>
<h3><a href="{{ page.url }}">{{ page.meta.title }}</a></h3>
<span class="post-date">Date: <em>{{ page.meta.date|date(config.date_format) }}</em></span><br>
{# remove the formatting and limit the string to 300 characters #}
<p>{{ page.content|striptags|slice(0, 300) }}...</p>
<a href="{{ page.url }}" title="{{ page.title }}" class="btn">Read More</a>
</article>
{% endfor %}
{{ pagination_links }}
In my tutorial 5 Minute Blog you can see an example of the pagination on the demo site.