ruby 将一个 erb 文件包含到另一个文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10236049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Including one erb file into another
提问by Chris Allen Lane
I'm writing a command-line tool that will ultimately output an HTML report. The tool is written in Ruby. (I am not using Rails). I'm trying to keep the logic of the application in one set of files, and the HTML templates (the .erb files) in another set.
我正在编写一个命令行工具,最终会输出一个 HTML 报告。该工具是用 Ruby 编写的。(我没有使用 Rails)。我试图将应用程序的逻辑保留在一组文件中,并将 HTML 模板(.erb 文件)保留在另一组文件中。
I'm having a really annoying problem though: I can't successfully include one .erb file into another.
不过,我遇到了一个非常烦人的问题:我无法将一个 .erb 文件成功包含到另一个文件中。
To be specific, I'm trying to do something like this (in pseudo-code):
具体来说,我正在尝试做这样的事情(在伪代码中):
<html>
<head>
<style type='text/css'>
[include a stylesheet here]
[and another one here]
</style>
</head>
<body>
<p>The rest of my document follows...
That example snippet is itselfan erb file, which is being invoked from within the application logic.
该示例片段本身就是一个 erb 文件,它是从应用程序逻辑中调用的。
I'm doing things this way so I can keep my stylesheets out of the main template to make it easier/cleaner to maintain the application. The end product (the report), though, needs to be a single, stand-alone HTML file which has no dependencies, and thus, I want to inline those stylesheets into the document head when the report is generated.
我这样做是为了让我的样式表远离主模板,以便更容易/更清洁地维护应用程序。但是,最终产品(报告)需要是一个没有依赖关系的独立 HTML 文件,因此,我想在生成报告时将这些样式表内联到文档头中。
This seems like this should be easy, but I've been banging my head against a wall (and Googling, and RTMF'ing) for the last hour, and I'm not having any luck at all.
这看起来应该很容易,但过去一个小时我一直在撞墙(和谷歌搜索和 RTMF),我一点运气都没有。
How is this supposed to be done? Thanks.
这应该怎么做?谢谢。
回答by cydparser
ERB templates can be nested by evaluating the sub-template from within <%= %> of the main template.
ERB 模板可以通过从主模板的 <%= %> 内评估子模板来嵌套。
<%= ERB.new(sub_template_content).result(binding) %>
For example:
例如:
require "erb"
class Page
def initialize title, color
@title = title
@color = color
end
def render path
content = File.read(File.expand_path(path))
t = ERB.new(content)
t.result(binding)
end
end
page = Page.new("Home", "#CCCCCC")
puts page.render("home.html.erb")
home.html.erb:
home.html.erb:
<title><%= @title %></title>
<head>
<style type="text/css">
<%= render "home.css.erb" %>
</style>
</head>
home.css.erb:
home.css.erb:
body {
background-color: <%= @color %>;
}
produces:
产生:
<title>Home</title>
<head>
<style type="text/css">
body {
background-color: #CCCCCC;
}
</style>
</head>
回答by Jon Carter
I'm needing this in a Sinatra app, and I find that I can just call it the same way I called the original:
我在 Sinatra 应用程序中需要这个,我发现我可以像调用原始应用程序一样调用它:
In the sinatra app, I call the index:
在 sinatra 应用程序中,我调用索引:
erb :index
Then, in the index template, I can do the same for any sub-template:
然后,在索引模板中,我可以对任何子模板执行相同的操作:
<div id="controls">
<%= erb :controls %>
..which shows the 'controls.erb' template.
..其中显示了“controls.erb”模板。
回答by Joseph Larson
From within my .erb file, I had to do this:
在我的 .erb 文件中,我必须这样做:
<%= ERB.new(File.read('pathToFile/myFile.erb'), nil, nil, '_sub01').result(binding) %>
The other answers in this thread assumed you had a variable with your content in it. This version retrieves the content.
该线程中的其他答案假设您有一个包含内容的变量。此版本检索内容。
回答by mighq
<%= ERB.new(sub_template_content).result(binding) %>
does not work, when you are using erbcli utility, multiple _erboutvariables are overriden and only last one is used.
不起作用,当您使用erbcli 实用程序时,多个_erbout变量被覆盖并且只使用最后一个。
use it like this:
像这样使用它:
<%= ERB.new(sub_template_content, eoutvar='_sub01').result(binding) %>

