Is there any way to have a template inherit another template? I'm not using Rails.
有没有办法让模板继承另一个模板?我没有使用Rails。
4 个解决方案
#1
3
No, you can't inherit an erb template - it isn't a class. It is just a file.
不,你不能继承一个erb模板 - 它不是一个类。它只是一个文件。
EDIT:
According to @Dan's comment, I took a look on Django template inheritance and it looks very similar to Rails content_for
. However I don't know how to use it outside of Rails. Take a look here at points 3.2 and 3.3, and here.
根据@ Dan的评论,我看了一下Django模板继承,它看起来与Rails content_for非常相似。但是我不知道如何在Rails之外使用它。请看这里的第3.2和3.3点。
#2
5
What you're looking for is partials and layouts. You can define a layout as a container for your page which has global design stuff (your CSS and javascript includes, headers and footers, etc. Inside this view, you can yield
to another template, and that template can render partials
.
您正在寻找的是局部和布局。您可以将布局定义为具有全局设计内容的页面容器(您的CSS和javascript包括,页眉和页脚等。在此视图中,您可以屈服于另一个模板,该模板可以渲染部分。
#3
5
I also really like the django template inheritance, but it's not available at least with sinatra.
我也非常喜欢django模板继承,但它至少不能与sinatra一起使用。
The sinatra book explains how you can use a layout.erb
to define a standard page layout:
sinatra书解释了如何使用layout.erb定义标准页面布局:
You just need to define a views/layout.erb
file with something like:
您只需要使用以下内容定义views / layout.erbfile:
<html>
<head>..</head>
<body>
<%= yield %>
</body>
</html>
And then call your erb template with erb :index
for example. Sinatra will render both templates and include the contents of your index template inside the result of the layout.erb rendering.
然后用erb:index调用你的erb模板。 Sinatra将渲染两个模板,并在layout.erb渲染的结果中包含索引模板的内容。
#4
0
Now there are the framework Rango, the first Ruby framework with template inheritance, and a framework-agnostic template inheritance gem, extracted from it.
现在有框架Rango,第一个带有模板继承的Ruby框架,以及从中提取的框架无关的模板继承gem。
#1
3
No, you can't inherit an erb template - it isn't a class. It is just a file.
不,你不能继承一个erb模板 - 它不是一个类。它只是一个文件。
EDIT:
According to @Dan's comment, I took a look on Django template inheritance and it looks very similar to Rails content_for
. However I don't know how to use it outside of Rails. Take a look here at points 3.2 and 3.3, and here.
根据@ Dan的评论,我看了一下Django模板继承,它看起来与Rails content_for非常相似。但是我不知道如何在Rails之外使用它。请看这里的第3.2和3.3点。
#2
5
What you're looking for is partials and layouts. You can define a layout as a container for your page which has global design stuff (your CSS and javascript includes, headers and footers, etc. Inside this view, you can yield
to another template, and that template can render partials
.
您正在寻找的是局部和布局。您可以将布局定义为具有全局设计内容的页面容器(您的CSS和javascript包括,页眉和页脚等。在此视图中,您可以屈服于另一个模板,该模板可以渲染部分。
#3
5
I also really like the django template inheritance, but it's not available at least with sinatra.
我也非常喜欢django模板继承,但它至少不能与sinatra一起使用。
The sinatra book explains how you can use a layout.erb
to define a standard page layout:
sinatra书解释了如何使用layout.erb定义标准页面布局:
You just need to define a views/layout.erb
file with something like:
您只需要使用以下内容定义views / layout.erbfile:
<html>
<head>..</head>
<body>
<%= yield %>
</body>
</html>
And then call your erb template with erb :index
for example. Sinatra will render both templates and include the contents of your index template inside the result of the layout.erb rendering.
然后用erb:index调用你的erb模板。 Sinatra将渲染两个模板,并在layout.erb渲染的结果中包含索引模板的内容。
#4
0
Now there are the framework Rango, the first Ruby framework with template inheritance, and a framework-agnostic template inheritance gem, extracted from it.
现在有框架Rango,第一个带有模板继承的Ruby框架,以及从中提取的框架无关的模板继承gem。