Skip to content

Instantly share code, notes, and snippets.

@yortz
Created August 28, 2009 17:04
Show Gist options
  • Save yortz/177100 to your computer and use it in GitHub Desktop.
Save yortz/177100 to your computer and use it in GitHub Desktop.
Ok, so I finally fixed the pagination thing, actually this is working for me with mephisto 0.8.2 (using several different themes - skittlish, simpla, metatheme - but that would not make any difference).
Here are the steps I've used to fixed pagination thanks to the blog post you can find at this address (FIRS THING FIRST, you have to follow the guidelines on that blog post to fix the pagination and total pages issue!!!)
http://railstalk.com/2009/8/12/setting-up-mephisto-articles-pagination
Then you can come back here and go on reading through my post.
This way I am achieving pagination on several blog categories on my website: I have setup my sections this way (note that I am using several "single" pages in my website such as Home, about us, contact etc etc...) but of course here we need pagination for blog sections that should be configured this way:
SECTIONS:
- Blog (here I am listing basically all articles belonging to all categories)
Type of section: Section with multiple articles (blog)
Section Template: _shared.liquid
Section layout: blog_layout.liquid
- News
Type of section: Section with multiple articles (blog)
Section Template: _news.liquid
Section layout: blog_layout.liquid
- Projects
Type of section: Section with multiple articles (blog)
Section Template: _projects.liquid
Section layout: blog_layout.liquid
You can easily add more Categories (aka Sections), each one SHOULD USE! its own SECTION TEMPLATE so let's say you are going to add an Events category (section), then you should go under the design tab and add your _events.liquid template.
Each Section Template is basically the same, the only thing that change is the name of the previous and next links, here is an example:
_shared.liquid
{% for article in articles %}
<div class="entry entry-{{ article.id }}">
<div class="entrytitle">
<h2>{{ article | link_to_article }}</h2>
<h3>{{ article.published_at | format_date: 'mdy', true }}</h3>
</div>
<div class="entrybody">
{% if article.excerpt %}
{{ article.excerpt }}
{% unless mode == 'single' %}
<a href="{{ article.url }}">Read the rest of this entry</a>
{% endunless %}
{% else %}
{% unless mode == 'single' %}{{ article.body }}{% endunless %}
{% endif %}
{% if mode == 'single' %}
{{ article.body }}
{% endif %}
</div>
<div class="entrymeta">
<div class="postinfo">
<span class="postedby">Posted by {{ article.author.login }}</span>
<span class="filedto">Filed in {{ article | linked_section_list }}</span>
</div>
{% unless mode == 'single' %}
{% if article.accept_comments %}<a href="{{ article.url }}#comments" class="commentslink">{{ article.comments_count | pluralize: 'comment' }} &#187;</a>{% endif %}
{% endunless %}
</div>
</div>
{% endfor %}
{% if mode == 'single' %}
<div class="commentsblock">
{% include 'comments' %}
</div>
{% endif %}
{{ articles | blog_link_to_prev_page: path_info, 'previous' }}
{{ articles | blog_link_to_next_page: path_info, 'next' }}
Note the blog_link_to_prev_page and blog_link_to_next_page at the bottom of the layout, those are using to create the previous and next link to sections (categories), so all other liquid templates would be exactly the same, the only thing that will be different will be the name of the previous and next links, like so: in _news.liquid I will have
{{ articles | news_link_to_prev_page: path_info, 'previous' }}
{{ articles | news_link_to_next_page: path_info, 'next' }}
in _projects.liquid I will have
{{ articles | projects_link_to_prev_page: path_info, 'previous' }}
{{ articles | projects_link_to_next_page: path_info, 'next' }}
and so on, change the name to something meaningful to you or to the name of your blog categories (sections): this way would be easier to reference those links later
Then, add your articles to the "Blog" categories (the one listing all blog posts - articles -) of course you can have the same article listed into multiple categories (added to different Sections), so I can have my "first article" being added to the "Blog", "News" and to the "Projects" section as well (use your own critheria here).
This instead is the blog_layout.liquid I am using (this is the default simpla theme one):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>{{ site.title }} {% if article %} {{ article.title }} {% else %} {{ site.subtitle }} {% endif %}</title>
<meta name="generator" content="Mephisto" />
{{ 'main' | stylesheet }}
{{ 'lazy' | stylesheet : 'screen, projection' }}
{{ site.home_section | articles_feed }}
</head>
<body>
<div id="wrap">
<div id="header"><h1><a href="/">{{ site.title }}</a></h1>
<p class="description">{{ site.subtitle }}</p>
</div>
<div id="content">
{{ content_for_layout }}
</div>
<div id="sidebar">
<h2>Search</h2>
<form method="get" id="sform" action="/search">
<label for="q">Search:</label>
<input type="text" id="q" value="" name="q" size="15" />
</form>
{% if site.page_sections %}
<h2>Pages</h2>
<ul>
{% for page in site.page_sections %}<li>{{ page | link_to_section }}</li>{% endfor %}
</ul>
{% endif %}
<h2>Categories</h2>
<ul>
{% for category in site.blog_sections %}<li>{{ category | link_to_section }}</li>{% endfor %}
</ul>
<h2>Tags</h2>
{% for tag in site.tags %}
{{ tag | size_tag }}
{% endfor %}
</ul>
</div>
</div>
<div id="footer">{{ site.title }} is proudly using the <a href="http://ifelse.co.uk/simpla">Simpla theme</a> originally designed by <a href="http://ifelse.co.uk">Phu</a>. Powered by <a href="http://mephistoblog.com/">Mephisto</a></div>
</body>
</html>
Basically here you just render {{ content_for_layout }} and list your categories (sections).
Now comes the nastiest part (please don't blame me, I am not a seasoned rails developer, just tried to get things done here, so I am pretty open to any kind of critic or suggestion about the way I have edited the code to let pagination work with mephisto 0.8.2)
1) go into your RAILS_HOME/vendor/plugins/mephisto_paged_article_list/lib/paged_article_list/mephisto_controller.rb and search for the dispatch_list_with_paging method, just edit this part to get rid of the @section.path being passed to the :path like so (you can comment out that line of code)
render_liquid_template_for(@section.show_paged_articles? ? :page : :section,
'section' => @section.to_liquid(true),
'articles' => @articles,
# 'path_info' => {:path => @section.path}
'path_info' => {:path => ''}
)
2) for each category aka section you have added to mephisto add its own link helper like so:
for blog create a new .rb file into RAILS_HOME/vendor/plugins/mephisto_paged_article_list/lib/paged_article_list folder and named it whatever you like (for easy of use I've named each of those after my categories names)
RAILS_HOME/vendor/plugins/mephisto_paged_article_list/lib/paged_article_list/blog_link_helpers.rb
module PagedArticleList
module BlogLinkHelpers
include ActionView::Helpers::TagHelper
def blog_prev_next_page_links(pager, path_info, *options)
return '' if path_info.nil?
options.unshift 'newer posts &raquo;', '&laquo; older posts' if options.size < 2
text, sep = {:prev => options[0], :next => options[1]}, options[2]
links = [:prev, :next].collect do |direction|
eval("link_to_#{direction}_page(pager, path_info, text[direction])")
end.compact
links.insert(1, sep) unless links.size < 2 || sep.nil?
links.reverse.join ' '
end
def blog_link_to_prev_page(pager, path_info, text = 'newer posts &raquo;')
return if pager.current_page == 1
content_tag :a, text, :href => blog_path_for(path_info, pager.current_page - 1), :class => 'prev'
end
def blog_link_to_next_page(pager, path_info, text = '&laquo; older posts')
# return if pager.current_page == pager.page_count
return if pager.current_page == pager.total_pages
content_tag :a, text, :href => blog_path_for(path_info, pager.current_page + 1), :class => 'next'
end
def blog_path_for( path_info, page_number )
return path_info[:path] + "/blog/page/" + page_number.to_s
end
end
end
and for news will be
module PagedArticleList
module NewsLinkHelpers
include ActionView::Helpers::TagHelper
def news_prev_next_page_links(pager, path_info, *options)
return '' if path_info.nil?
options.unshift 'newer posts &raquo;', '&laquo; older posts' if options.size < 2
text, sep = {:prev => options[0], :next => options[1]}, options[2]
links = [:prev, :next].collect do |direction|
eval("link_to_#{direction}_page(pager, path_info, text[direction])")
end.compact
links.insert(1, sep) unless links.size < 2 || sep.nil?
links.reverse.join ' '
end
def news_link_to_prev_page(pager, path_info, text = 'newer posts &raquo;')
return if pager.current_page == 1
content_tag :a, text, :href => news_path_for(path_info, pager.current_page - 1), :class => 'prev'
end
def news_link_to_next_page(pager, path_info, text = '&laquo; older posts')
# return if pager.current_page == pager.page_count
return if pager.current_page == pager.total_pages
content_tag :a, text, :href => news_path_for(path_info, pager.current_page + 1), :class => 'next'
end
def news_path_for( path_info, page_number )
return path_info[:path] + "/news/page/" + page_number.to_s
end
end
end
and so on, this way each liquid shared layout will trigger its own links, of course edit the categoryname_path_for with the correct path to your categories - sections (the default will be /categoryname/page) except you didn't edited the path into mephisto section preferences page.
3) edit RAILS_HOME/vendor/plugins/mephisto_paged_article_list/lib/paged_article_list/init.rb to include your new pagination links for your categories (sections) like so
ActionView::Base.send :include, PagedArticleList::LinkHelpers
ActionView::Base.send :include, PagedArticleList::BlogLinkHelpers
ActionView::Base.send :include, PagedArticleList::NewsLinkHelpers
require 'paged_article_list/mephisto_controller'
config.to_prepare do
# MephistoController.send :include, PagedArticleList::MephistoController
Liquid::Template.register_filter(PagedArticleList::LinkHelpers)
Liquid::Template.register_filter(PagedArticleList::BlogLinkHelpers)
Liquid::Template.register_filter(PagedArticleList::NewsLinkHelpers)
end
4) restart your server
this way you can browse to website/blog and see your prev next links pointing to website/blog/page/pagenumber and moving to website/news you will have your news links pointing you to the right direction website/news/page/pagenumber
The limit here is that if you want to add a new category (anyway, i don't think that anyone will ever create a default category with a bunch -10 or more posts - aka articles - from the very beginning), after you have setup the maximum limit for your articles in the category (section) page, you will have to go back and add another custom LinkHelper to trigger that very same category (section).
This way is working for me, sorry if this post was so long, I just hope to have helped other peeps.
A quick note to anyone interested, originaly I've tried to edit the original LinkHelper module to add custom named previous next links in there, but for some way it wasn't working so I added a different module for each section.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment