Beego模板渲染部分视图

huangapple go评论101阅读模式
英文:

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:

&lt;!-- html.erb --&gt;
&lt;h1&gt;New zone&lt;/h1&gt;
&lt;%= render partial: &quot;form&quot;, locals: {zone: @zone} %&gt;

and

&lt;!-- _form.html.erb --&gt;
&lt;%= form_for(zone) do |f| %&gt;
  &lt;p&gt;
    &lt;b&gt;Zone name&lt;/b&gt;&lt;br&gt;
    &lt;%= f.text_field :name %&gt;
  &lt;/p&gt;
  &lt;p&gt;
    &lt;%= f.submit %&gt;
  &lt;/p&gt;
&lt;% end %&gt;

答案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 &quot;header.html&quot;}}
  Logic code
{{template &quot;footer.html&quot;}}

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 &quot;base/header.html&quot; .}}

A good reference implementation that will provide more examples in the context of a real world app, check out the wetalk project on github

huangapple
  • 本文由 发表于 2015年10月28日 04:08:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/33377528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定