使用 Slim 将变量从内容传递到 Nanoc 中的布局
Pass variable from content to layout in Nanoc using Slim
我基本上想知道使用 Nanoc 和 Slim 将 ruby 变量从内容页面传递到其布局的最简单方法。我在想这样的事情:
content/content.苗条:
---
title: Writeups
layout: /layout.slim
---
- age = get_age
layout/layout.苗条:
doctype html
html
head
== yield
p I am #{@item[:title]} and am #{@item[:age]} years old
我知道如何通过 frontmatter 访问值,但 frontmatter 值是固定的,我想要的是一个 ruby 函数来为我找到该值。
Nanoc 提供了一个capturing helper,可以在一个地方“捕获”内容并在其他地方使用。
content/content.苗条:
---
title: Mister Tree
---
p Hello there!
- content_for :age
| hundreds of years
layout/layout.苗条:
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>
我基本上想知道使用 Nanoc 和 Slim 将 ruby 变量从内容页面传递到其布局的最简单方法。我在想这样的事情:
content/content.苗条:
---
title: Writeups
layout: /layout.slim
---
- age = get_age
layout/layout.苗条:
doctype html
html
head
== yield
p I am #{@item[:title]} and am #{@item[:age]} years old
我知道如何通过 frontmatter 访问值,但 frontmatter 值是固定的,我想要的是一个 ruby 函数来为我找到该值。
Nanoc 提供了一个capturing helper,可以在一个地方“捕获”内容并在其他地方使用。
content/content.苗条:
---
title: Mister Tree
---
p Hello there!
- content_for :age
| hundreds of years
layout/layout.苗条:
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>