英文:
Beego template render partials
问题
在Beego中有渲染部分模板的功能吗?我在RoR中使用过这个功能。
Ruby on Rails部分模板:
部分模板通常被称为"partials",是将渲染过程分解为更可管理的块的另一种方式。通过使用部分模板,你可以将渲染响应的特定部分的代码移动到自己的文件中。
RoR的示例:
<!-- html.erb -->
<h1>New zone</h1>
<%= render partial: "form", locals: {zone: @zone} %>
和
<!-- _form.html.erb -->
<%= form_for(zone) do |f| %>
<p>
<b>Zone name</b><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
英文:
Is there a functionality to render partial templates in the Beego? I used this functionality in RoR.
Ruby on Rails partials:
Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.
Example from RoR:
<!-- html.erb -->
<h1>New zone</h1>
<%= render partial: "form", locals: {zone: @zone} %>
and
<!-- _form.html.erb -->
<%= form_for(zone) do |f| %>
<p>
<b>Zone name</b><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
答案1
得分: 1
是的,尽管在文档中没有明确将它们称为partials,但你可以按照你描述的方式组合模板。
来自视图的文档。
{{template "header.html"}}
逻辑代码
{{template "footer.html"}}
因为Beego是一个MVC框架,它会自动在views目录中查找你的"partials"。
如果你有另一个名为views/base的子目录,那么你的partial将如下所示:
{{template "base/header.html" .}}
一个很好的参考实现,它将在真实应用的上下文中提供更多示例,请查看github上的wetalk项目。
英文:
Yes, though they are not explicitly referenced as partials in the documentation, you can compose templates like you described.
From the documentation for views.
{{template "header.html"}}
Logic code
{{template "footer.html"}}
Because Beego is an MVC framework, it will automatically look for your "partials" in the views directory.
If you had another subdirectory called views/base then your partial would look like this:
{{template "base/header.html" .}}
A good reference implementation that will provide more examples in the context of a real world app, check out the wetalk project on github
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论