【问题标题】:Custom Tag in MiddlemanappMiddlemanapp 中的自定义标签
【发布时间】:2013-09-03 12:37:55
【问题描述】:

我正在使用当前版本的 Middleman 创建博客,并且我希望能够创建博客上所有作者的列表以显示在侧边栏上(就像我创建标签一样,然后链接到列出所有作者帖子的页面(有点像作者档案?)

到目前为止,我在每一页的顶部都有一个“作者”frontmatter 块:

---
author: Joe Bloggs
---

我曾考虑过使用frontmatter,但frontmatter似乎只允许页面特定的变量,例如:

 ---
  layout: "blog"
  authors:
    - author 1
    - author 2
    - author 3
  ---

  <ul>
    <% current_page.data.authors.each do |f| %>
    <li><%= f %></li>
    <% end %>
  </ul>

而不是创建存档页面。

我想我可以像显示标签列表一样做到这一点:

<ul>
<% blog.tags.each do |tag, articles| %>
<li><%= link_to tag, tag_path(tag) %></a></li>
<% end %>
</ul>

但到目前为止还没有运气。我做了谷歌搜索,但没有找到具体的。

谁能提出一个可能的代码解决方案?

【问题讨论】:

  • 目前还不清楚您的代码中究竟有什么不工作。是创建对您不起作用的存档页面,还是在示例 2 中发布作者姓名?您希望存档页面自动生成吗?
  • 这是存档页面的实际创建,以显示每个作者发布的列表(示例 2),这对我不起作用。我正在考虑为每个作者动态生成“存档”页面,然后简单地使用静态链接从侧边栏链接到每个作者页面
  • 这对你有用吗?我很好奇构建索引页面的模式是否有效。我知道articles.select 表示法效率很低,但考虑到它是一个静态站点,看起来还不错。
  • 不,不幸的是,我没有运气,但从我能读到的内容来看,如果我有更多时间坚持下去的话,这些想法是正确的......

标签: ruby sinatra middleman


【解决方案1】:

首先,您需要在 config.rb 中添加代理:

Middleman: Dynamic Pages

# Assumes the file source/author/template.html.erb exists
["tom", "dick", "harry"].each do |name|
  proxy "/author/#{name}.html", "/author/template.html", :locals => { :person_name => name }, :ignore => true
end

不过,问题在于中间人博客引擎似乎并未正式支持每篇文章的不同作者。通读本教程以获得自制解决方案的完整说明:Building a Middleman Blog

基本上,您会希望在存档页面模板上执行以下操作:

# Note the presence of the person_name local variable, created in the above example
<% author_articles = articles.select {|x| x.data.author == person_name } %>

<ul>
<% author_articles.each do |article|
  # Add your rendering code here %>
  <li><%= link_to article.title, article.url %></li>
<% # (for better practices, put this in a helper method)
end %>
</ul>

【讨论】:

    猜你喜欢
    • 2012-07-13
    • 2015-01-03
    • 2021-05-11
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    相关资源
    最近更新 更多