1

我基本上想知道使用 Nanoc 和 Slim 将 ruby​​ 变量从内容页面传递到其布局的最简单方法。我在想这样的事情:

内容/内容.slim:

---
title: Writeups
layout: /layout.slim
---
- age = get_age

布局/layout.slim:

doctype html
  html
    head
      == yield
      p I am #{@item[:title]} and am #{@item[:age]} years old

我知道如何通过 frontmatter 访问值,但 frontmatter 值是固定的,我想要的是一个 ruby​​ 函数来为我找到该值。

4

1 回答 1

2

Nanoc 提供了一个捕获助手,它可以在一个地方“捕获”内容并在其他地方使用它。

内容/内容.slim:

---
title: Mister Tree
---

p Hello there!

- content_for :age
  | hundreds of years

布局/layout.slim:

doctype html
html
  body
    == yield
    p I am #{@item[:title]} and am #{content_for(@item, :age)} years old

lib/default.rb (或您选择的 lib/ 中的任何文件):

use_helper Nanoc::Helpers::Capturing

这会生成以下输出:

<!DOCTYPE html>
<html>
  <body>
    <p>Hello there!</p>
    <p>I am Mister Tree and am hundreds of years years old</p>
  </body>
</html>
于 2018-12-12T07:24:55.500 回答