Ruby 在MiddleMan中定义日历页的自定义标题

Ruby 在MiddleMan中定义日历页的自定义标题,ruby,middleman,Ruby,Middleman,我刚刚用middleman3和middleman bloggems建立了一个小型静态博客,目前正在完善一切。我从SEO的角度看我的页面,注意到我所有的日历页面都显示相同的标题(这对SEO非常不利,afaik) 目前,我使用它在布局(Slim template engine)中生成标题标记: 对于文章来说,这很容易,因为我只需要在frontmatter中定义标题,但对于动态生成的日历页面,我显然不能这样做。除非我能在calendar.slim的前端使用变量,但到目前为止,我什么都做不到。可能通过c

我刚刚用
middleman
3和
middleman blog
gems建立了一个小型静态博客,目前正在完善一切。我从SEO的角度看我的页面,注意到我所有的日历页面都显示相同的标题(这对SEO非常不利,afaik)

目前,我使用它在布局(Slim template engine)中生成标题标记:

对于文章来说,这很容易,因为我只需要在frontmatter中定义标题,但对于动态生成的日历页面,我显然不能这样做。除非我能在calendar.slim的前端使用变量,但到目前为止,我什么都做不到。可能通过
config.rb
文件


谢谢你的帮助

您可以使用
变量集。您还可以在日历模板中使用frontmatter,方法与在其他任何地方使用的方法相同(
current_resource.data
),并且您可以在
文章中访问该日历页面的文章,并循环浏览它们以从每个页面中提取数据。很多很多选择


默认生成的
calendar.html.erb
有使用这些的示例。

最后,我选择了继续操作的方式:在我的calendar.slim和tag.slim模板中,我只定义了一个
@title
变量,我在默认布局中使用它。在
calendar.slim
中,我使用内置变量
year
month
day
来构建标题字符串,在
tag.slim
中,我使用内置的
标记名
变量

[calendar.slim]

- case page_type
- when 'month'
  - date = date_to_fr Date.new(year, month, 1).strftime('%B %Y')
- when 'year'
  - date = year

- @title = "#{date} - Archives"

(...)
然后在布局中,我使用以下代码显示我的
@title
变量,后跟我的博客名称,除非
@title
不存在(然后我只使用我的博客名称作为页面标题):

只是在这个基础上发展

不能动态设置frontmatter变量

current_page.data.foo = true
current_page.data.too #=> nil
而且frontmatter不是模板化的

---
title: <%= "foo" %>
---
<%= current_page.data.title %> == <%%= "foo" %>
---
标题:
---
== 
所以我所做的是在我的布局中,首先检查实例变量,然后回到frontmatter

<title><%= @title || current_page.data.title %></title>


通过这种方式,我可以在frontmatter中设置大多数变量,但当我需要动态变量时,我可以使用实例变量。

是的,但实际上我想在布局中使用一些来自实际页面的数据(例如日历模板)。请参阅我使用的(不那么优雅)解决方案的答案。
[calendar.slim]

- case page_type
- when 'month'
  - date = date_to_fr Date.new(year, month, 1).strftime('%B %Y')
- when 'year'
  - date = year

- @title = "#{date} - Archives"

(...)
<title><%= @title || current_page.data.title %></title>