Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Slim在Nanoc中将变量从内容传递到布局_Ruby On Rails_Ruby_Model View Controller_Slim Lang_Nanoc - Fatal编程技术网

Ruby on rails 使用Slim在Nanoc中将变量从内容传递到布局

Ruby on rails 使用Slim在Nanoc中将变量从内容传递到布局,ruby-on-rails,ruby,model-view-controller,slim-lang,nanoc,Ruby On Rails,Ruby,Model View Controller,Slim Lang,Nanoc,我主要想知道使用Nanoc和Slim将ruby变量从内容页传递到布局的最简单方法。我在想这样的事情: content/content.slim: --- title: Writeups layout: /layout.slim --- - age = get_age --- title: Mister Tree --- p Hello there! - content_for :age | hundreds of years layout/layout.slim: doctype h

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

content/content.slim:

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

p Hello there!

- content_for :age
  | hundreds of years
layout/layout.slim:

doctype html
  html
    head
      == yield
      p I am #{@item[:title]} and am #{@item[:age]} years old
doctype html
html
  body
    == yield
    p I am #{@item[:title]} and am #{content_for(@item, :age)} years old
我知道如何通过frontmatter访问值,但frontmatter的值是固定的,我想要的是一个ruby函数来为我查找该值。

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

content/content.slim:

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

p Hello there!

- content_for :age
  | hundreds of years
layout/layout.slim:

doctype html
  html
    head
      == yield
      p I am #{@item[:title]} and am #{@item[:age]} years old
doctype html
html
  body
    == yield
    p I am #{@item[:title]} and am #{content_for(@item, :age)} years old
lib/default.rb(或您选择的lib/中的任何文件):

这将生成以下输出:

<!DOCTYPE html>
<html>
  <body>
    <p>Hello there!</p>
    <p>I am Mister Tree and am hundreds of years years old</p>
  </body>
</html>

你好

我是树先生,已经几百岁了


nanoc是否有可用的方法的
content\u?我必须使用
include
而不是
content\u。它与您正在使用的nanoc版本相关。