How to generate pages for each tag in nanoc

。_饼干妹妹 提交于 2019-12-10 01:58:27

问题


I am new to nanoc and I am still finding my around it. I am able to get my site ready, it looks good and functions good, too. But I need to have a tags area. I am able to achieve that with

<%= tags_for(post, params = {:base_url => "http://example.com/tag/"}) %>

But how do I generate pages for tag? So for instance there is a tag called "NFL", so every time a user clicks on it, he/she should be directed to http://example.com/tag/nfl with a list of articles that correspond with NFL.

I can setup a layout which will do that. But then what kind of logic should be I using? And also do I need to have a helper for this?


回答1:


You can use a preprocess block in your Rules file in order to generate new items dynamically. Here’s an example of a preprocess block where a single new item is added:

preprocess do
  items << Nanoc::Item.new(
    "some content here",
    { :attributes => 'here', :awesomeness => 5000 },
    "/identifier/of/this/item")
end

If you want pages for each tag, you need to collect all tags first. I’m doing this with a set because I do not want duplicates:

require 'set'
tags = Set.new
items.each do |item|
  item[:tags].each { |t| tags.add(t.downcase) }
end

Lastly, loop over all tags and generate items for them:

tags.each do |tag|
  items << Nanoc::Item.new(
    "",
    { :tag => tag },
    "/tags/#{tag}/")
end

Now, you can create a specific compilation rule for /tags/*/, so that it is rendered using a "tags" layout, which will take the value of the :tag attribute, find all items with this tag and show them in a list. That layout will look somewhat like this:

<h1><%= @item[:tag] %></h1>
<ul>
  <% items_with_tag(@item[:tag]).each do |i| %>
    <li><%= link_to i[:title], i %></li>
  <% end %>
</ul>

And that, in broad strokes, should be what you want!



来源:https://stackoverflow.com/questions/13866141/how-to-generate-pages-for-each-tag-in-nanoc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!