运维 erb 可以继承吗?能制定位置吗?

tonysh · 2014年08月08日 · 最后由 ericguo 回复于 2014年08月09日 · 2447 次阅读

重要可以发帖了。。。 是否可以写一个 erb,名字是 base。内容是我制定为的头部 和尾部。 再写一个文件名字是 extend,其内容是在 base 继承上扩展,并且运行此文件后,看到 base 的头,extend,base 尾部。

你直接将 头部 和 尾部 各自放到一个 erb 文件里,如 views/shared/_header.html.erb

然后你在你当前的文件里 render 'shared/header' 就可以。

#2 楼 @hz_qiuyuanxin 我的中间部分如何告诉_header.html.erb,我需要他的内容做头部和尾部?需要什么关键字吗?

类似这样?用 content_for 和 yield,甚至 render 也行。

app/views/lots/dp.html.erb

<% if @lot.lotattributes.stdmrgebatch.present? -%>
<% title = "#{@lot.lotattributes.stdmrgebatch} #{@product.packagecategory.packagecategoryname} #{@product.densitycode} DP"
   content_for :title, title %>
<% else -%>
<% title = "#{@product.packagecategory.packagecategoryname} #{@product.densitycode} #{@lot.containername} DP"
   content_for :title, title %>
<% end -%>
<%= render :partial => 'navbar', :locals => {:title => title, :print_template => 'dp' } %>

app/views/layouts/application.html.erb

<!doctype html>
<html>
<head>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <title><%= (yield(:title) + " - " unless yield(:title).blank?).to_s + "MES View" %></title>
  <%= stylesheet_link_tag    "application", media: "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body data-spy="scroll" data-target=".navbar">
<% if notice.present? -%>
    <div class="alert alert-info"><%= notice %></div>
<% end %>
<%= yield %>
</body>
</html>
需要 登录 后方可回复, 如果你还没有账号请 注册新账号