我有一个Jekyll的自定义页面:我有一个news.markdown
文件,如下所示:
{% assign posts_per_page = 5 %}
{% for post in site.categories.ro offset:pagination_skip limit:posts_per_page %}
...
{{ post.content }}
...
{% endfor %}
{% include nav.html %}
nav.html
位于_includes
目录中,如下所示:
{% if pagination_skip %}
{% capture pagination_skip %}
{{pagination_skip | plus: posts_per_page}}
{% endcapture %}
{% else %}
{% assign pagination_skip = posts_per_page %}
{% endif %}
<div class="next">
<a rel="prev" href="{{site.basepath}}ro/news/{{ pagination_skip }}">Next</a>
</div>
我想要的是将网址.../ro/news/5
映射到news.markdown
的内容,使pagination_skip
有5
。同样适用于10,15,无论如何。此外,如果可能,.../ro/news/0
应与.../ro/news/
相同。
我能这样做吗?怎么样?
我想尽可能少地使用扩展程序。
答案 0 :(得分:3)
你试图做错了。您希望动态处理URL参数。 Jekyll是一个静态 Web生成器。因此,您需要生成将要访问的所有页面。你可以为它做一个发电机。请查看第https://github.com/mojombo/jekyll/wiki/Plugins页
上的生成器部分我在一台发电机上工作,你想做什么,它工作得非常好,我还创建了'上一步'和'下一步'按钮。请看看并尝试一下。它适用于所有类别(不仅仅是 ro ),但您可以根据需要自定义它。
文件夹结构
| -- _plugins
` news.rb
| -- _layouts
` news.html
| -- _includes
` nav.html
| -- _config.yml
| -- ro
` -- _posts
| -- 2012-04-10-test.textile
| -- 2012-04-10-test2.textile
| -- 2012-04-10-test4.textile
| -- 2012-04-10-test6.textile
| -- 2012-04-10-test15.textile
| -- 2012-04-10-test3.textile
` -- 2012-04-10-test5.textile
` _site
发布者news.rb
红宝石代码
module Jekyll
class NewsPage < Page
def initialize(site, base, dir, category, posts_number, posts_per_page, pagination_skip)
@site = site
@base = base
@dir = dir
@name = "news_#{pagination_skip}.html"
self.process(@name)
self.read_yaml(File.join(base, '_layouts'), 'news.html')
self.data['category'] = category
self.data['posts_per_page'] = posts_per_page
self.data['pagination_skip'] = pagination_skip
if pagination_skip != 0
self.data['prev_pagination_skip'] = pagination_skip - posts_per_page
end
if pagination_skip + posts_per_page < posts_number
self.data['next_pagination_skip'] = pagination_skip + posts_per_page
end
end
end
class NewsGenerator < Generator
safe true
def generate(site)
if site.layouts.key? 'news'
dir = site.config['category_dir'] || 'categories'
site.categories.keys.each do |category|
posts_number = site.categories[category].length
pagination_skip = 0;
posts_per_page = 5;
begin
write_news_page(site, File.join(dir, category), category, posts_number, posts_per_page, pagination_skip)
pagination_skip += posts_per_page;
end while pagination_skip < posts_number
end
end
end
def write_news_page(site, dir, category, posts_number, posts_per_page, pagination_skip)
index = NewsPage.new(site, site.source, dir, category, posts_number, posts_per_page, pagination_skip)
index.render(site.layouts, site.site_payload)
index.write(site.dest)
site.pages << index
end
end
end
news.html
文件的布局
---
---
{% for post in site.categories[page.category] offset:page.pagination_skip limit:page.posts_per_page %}
{{ post.content }}
{% endfor %}
{% include nav.html %}
包含导航nav.html
<div class="nav">
{% if page.prev_pagination_skip %}
<a rel="prev" href="{{site.basepath}}categories/{{page.category}}/news_{{page.prev_pagination_skip}}.html">Prev</a>
{% endif %}
{% if page.next_pagination_skip %}
<a rel="next" href="{{site.basepath}}categories/{{page.category}}/news_{{page.next_pagination_skip}}.html">Prev</a>
{% endif %}
</div>
尝试一下,如果你喜欢,请告诉我。