【问题标题】:How to access the article layout data property in middleman 4?如何访问中间人 4 中的文章布局数据属性?
【发布时间】:2017-04-21 16:30:03
【问题描述】:

我正在尝试从 Middleman 4 迁移我的博客。在我的 index.html.erb 文件中,我有这样的代码:

<% page_articles.each_with_index do |article, i| %>
    <% if article.data.layout == 'post' %>
        <%= partial "post", :locals => { :article => article} %>
    <% elsif article.data.layout == 'micro' %>
        <%= partial "micro", :locals => { :article => article} %>
    <% end %>
<% end %>

这在 Middleman 3 中运行良好,我可以根据每个帖子前面定义的布局属性使用不同的部分。

但是,在 Middleman 4 中,layout 属性在 article.data 中不再可见。

所有其他变量都在那里。到目前为止,完成这项工作的唯一方法是像这样声明 frontmatter:

---
layout: post
l: post
title:  "Foo"
date:   2012-10-22 15:14:01
categories: Bar
tags:
---

并使用article.data.l 而不是article.data.layout。但是,这需要我重写所有文章,似乎是多余的。

根据布局属性在索引页面上呈现不同部分的文章的正确方法是什么?

【问题讨论】:

    标签: erb middleman yaml-front-matter


    【解决方案1】:

    在 Middleman 4 中,layout 属性被视为 option 而不是 data

    您应该能够使用article.options.layoutarticle.options[:layout] 访问页面的layout 属性的值。

    【讨论】:

    • 太棒了!谢谢你。第二个选项适用于article.options[:layout]
    猜你喜欢
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多