Merge pull request #942 from mojombo/pagination-paths
Add paginator.previous_page_path and paginator.next_page_path
This commit is contained in:
commit
b7646cc9ca
|
@ -37,7 +37,7 @@ module Jekyll
|
|||
if num_page > 1
|
||||
newpage = Page.new(site, site.source, page.dir, page.name)
|
||||
newpage.pager = pager
|
||||
newpage.dir = File.join(page.dir, paginate_path(site, num_page))
|
||||
newpage.dir = File.join(page.dir, Pager.paginate_path(site.config, num_page))
|
||||
site.pages << newpage
|
||||
else
|
||||
page.pager = pager
|
||||
|
@ -45,16 +45,12 @@ module Jekyll
|
|||
end
|
||||
end
|
||||
|
||||
private
|
||||
def paginate_path(site, num_page)
|
||||
format = site.config['paginate_path']
|
||||
format.sub(':num', num_page.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Pager
|
||||
attr_reader :page, :per_page, :posts, :total_posts, :total_pages, :previous_page, :next_page
|
||||
attr_reader :page, :per_page, :posts, :total_posts, :total_pages,
|
||||
:previous_page, :previous_page_path, :next_page, :next_page_path
|
||||
|
||||
# Calculate the number of pages.
|
||||
#
|
||||
|
@ -76,6 +72,18 @@ module Jekyll
|
|||
file == 'index.html' && !config['paginate'].nil?
|
||||
end
|
||||
|
||||
# Static: Return the pagination path of the page
|
||||
#
|
||||
# site_config - the site config
|
||||
# num_page - the pagination page number
|
||||
#
|
||||
# Returns the pagination path as a string
|
||||
def self.paginate_path(site_config, num_page)
|
||||
return nil if num_page.nil? || num_page <= 1
|
||||
format = site_config['paginate_path']
|
||||
format.sub(':num', num_page.to_s)
|
||||
end
|
||||
|
||||
# Initialize a new Pager.
|
||||
#
|
||||
# config - The Hash configuration of the site.
|
||||
|
@ -98,7 +106,9 @@ module Jekyll
|
|||
@total_posts = all_posts.size
|
||||
@posts = all_posts[init..offset]
|
||||
@previous_page = @page != 1 ? @page - 1 : nil
|
||||
@previous_page_path = Pager.paginate_path(config, @previous_page)
|
||||
@next_page = @page != @total_pages ? @page + 1 : nil
|
||||
@next_page_path = Pager.paginate_path(config, @next_page)
|
||||
end
|
||||
|
||||
# Convert this Pager's data to a Hash suitable for use by Liquid.
|
||||
|
@ -112,7 +122,9 @@ module Jekyll
|
|||
'total_posts' => total_posts,
|
||||
'total_pages' => total_pages,
|
||||
'previous_page' => previous_page,
|
||||
'next_page' => next_page
|
||||
'previous_page_path' => previous_page_path,
|
||||
'next_page' => next_page,
|
||||
'next_page_path' => next_page_path
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -31,6 +31,78 @@ paginate: 5
|
|||
The number should be the maximum number of Posts you’d like to be displayed per-
|
||||
page in the generated site.
|
||||
|
||||
## Liquid Attributes Available
|
||||
|
||||
The pagination plugin exposes the `paginator` liquid object with the following
|
||||
attributes:
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Attribute</th>
|
||||
<th>Description</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><p><code>page</code></p></td>
|
||||
<td><p>current page number</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><p><code>per_page</code></p></td>
|
||||
<td><p>number of posts per page</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><p><code>posts</code></p></td>
|
||||
<td><p>a list of posts for the current page</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><p><code>total_posts</code></p></td>
|
||||
<td><p>total number of posts in the site</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><p><code>total_pages</code></p></td>
|
||||
<td><p>number of pagination pages</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><p><code>previous_page</code></p></td>
|
||||
<td>
|
||||
<p>
|
||||
page number of the previous pagination page,
|
||||
or <code>nil</code> if no previous page exists
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><p><code>previous_page_path</code></p></td>
|
||||
<td>
|
||||
<p>
|
||||
path of previous pagination page,
|
||||
or <code>nil</code> if no previous page exists
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><p><code>next_page</code></p></td>
|
||||
<td>
|
||||
<p>
|
||||
page number of the next pagination page,
|
||||
or <code>nil</code> if no subsequent page exists
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><p><code>next_page_path</code></p></td>
|
||||
<td>
|
||||
<p>
|
||||
path of next pagination page,
|
||||
or <code>nil</code> if no subsequent page exists
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<div class="note info">
|
||||
<h5>Pagination does not support tags or categories</h5>
|
||||
<p>Pagination pages through every post in the <code>posts</code> variable regardless of variables defined in the YAML Front Matter of each. It does not currently allow paging over groups of posts linked by a common tag or category.</p>
|
||||
|
@ -98,7 +170,7 @@ page with links to all but the current page.
|
|||
{% if paginator.previous_page == 1 %}
|
||||
<a href="/">Previous</a>
|
||||
{% else %}
|
||||
<a href="/page{{ paginator.previous_page }}">Previous</a>
|
||||
<a href="{{ paginator.previous_page_path }}">Previous</a>
|
||||
{% endif %}
|
||||
</p>
|
||||
{% else %}
|
||||
|
@ -129,7 +201,7 @@ page with links to all but the current page.
|
|||
|
||||
{% if paginator.next_page %}
|
||||
<p class="next">
|
||||
<a href="/page{{ paginator.next_page }}">Next</a>
|
||||
<a href="{{ paginator.next_page_path }}">Next</a>
|
||||
</p>
|
||||
{% else %}
|
||||
<p class="next disabled">
|
||||
|
|
Loading…
Reference in New Issue