我可以继承 erb 模板吗?

发布于 2024-08-23 14:47:28 字数 37 浏览 1 评论 0原文

有没有办法让一个模板继承另一个模板?我没有使用 Rails。

Is there any way to have a template inherit another template? I'm not using Rails.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烙印 2024-08-30 14:47:28

我也非常喜欢 django 模板继承,但至少在 sinatra 中不可用。

sinatra 书解释了如何使用 layout.erb定义标准页面布局:

您只需定义一个 views/layout.erb 文件,如下所示:

<html>
  <head>..</head>
  <body>
    <%= yield %>
  </body>
</html>

然后使用 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.erbfile with something like:

<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.

ゃ懵逼小萝莉 2024-08-30 14:47:28

您要寻找的是 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 render partials.

谁对谁错谁最难过 2024-08-30 14:47:28

不,您不能继承 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.

乖乖公主 2024-08-30 14:47:28

现在有框架 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文