我可以继承 erb 模板吗?
有没有办法让一个模板继承另一个模板?我没有使用 Rails。
Is there any way to have a template inherit another template? I'm not using Rails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有没有办法让一个模板继承另一个模板?我没有使用 Rails。
Is there any way to have a template inherit another template? I'm not using Rails.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
我也非常喜欢 django 模板继承,但至少在 sinatra 中不可用。
sinatra 书解释了如何使用
layout.erb
定义标准页面布局:您只需定义一个
views/layout.erb
文件,如下所示:然后使用
erb :index
调用您的 erb 模板。 Sinatra 将渲染这两个模板,并将索引模板的内容包含在layout.erb 渲染的结果中。I also really like the django template inheritance, but it's not available at least with sinatra.
The sinatra book explains how you can use a
layout.erb
to define a standard page layout:You just need to define a
views/layout.erb
file with something like: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.您要寻找的是 partials 和 布局。您可以将布局定义为页面的容器,其中包含全局设计内容(CSS 和 javascript、页眉和页脚等)。在此视图中,您可以
yield
到另一个模板,然后该模板可以渲染部分
。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 renderpartials
.不,您不能继承 erb 模板 - 它不是一个类。它只是一个文件。
编辑:
根据 @Dan 的评论,我查看了 Django 模板继承,它看起来与 Rails
content_for
非常相似。但是我不知道如何在 Rails 之外使用它。请查看此处第 3.2 和 3.3 点,以及此处。No, you can't inherit an erb template - it isn't a class. It is just a file.
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.现在有框架 Rango,第一个具有模板继承的 Ruby 框架,以及一个与框架无关的 模板继承 gem,从中提取。
Now there are the framework Rango, the first Ruby framework with template inheritance, and a framework-agnostic template inheritance gem, extracted from it.