【问题标题】:Pass variable from content to layout in Nanoc using Slim使用 Slim 在 Nanoc 中将变量从内容传递到布局
【发布时间】:2018-12-11 22:11:31
【问题描述】:

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

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

【问题讨论】:

  • nanoc 有可用的content_for 方法吗?
  • 我不得不使用include 而不是content_for。它与您使用的 nanoc 版本有关。

标签: ruby-on-rails ruby model-view-controller slim-lang nanoc


【解决方案1】:

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

content/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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2011-06-19
    • 2022-11-17
    相关资源
    最近更新 更多