使用Slim将变量从内容传递到Nanoc中的布局

问题描述 投票:1回答:1

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

内容/ content.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函数来为我找到该值。

ruby-on-rails ruby model-view-controller slim-lang nanoc
1个回答
1
投票

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

内容/ content.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>
© www.soinside.com 2019 - 2024. All rights reserved.