代码之家  ›  专栏  ›  技术社区  ›  brad

带有块的Rails呈现部分

  •  102
  • brad  · 技术社区  · 15 年前

    我正在尝试重新使用我编写的提供面板样式的HTML组件。类似:

      <div class="v-panel">
        <div class="v-panel-tr"></div>
        <h3>Some Title</h3>
        <div class="v-panel-c">
          .. content goes here
        </div>
        <div class="v-panel-b"><div class="v-panel-br"></div><div class="v-panel-bl"></div></div>
      </div>
    

    所以我看到渲染需要一个块。我想我可以这样做:

    # /shared/_panel.html.erb
    <div class="v-panel">
      <div class="v-panel-tr"></div>
      <h3><%= title %></h3>
      <div class="v-panel-c">
        <%= yield %>
      </div>
      <div class="v-panel-b"><div class="v-panel-br"></div><div class="v-panel-bl"></div></div>
    </div>
    

    我想做如下的事情:

    #some html view
    <%= render :partial => '/shared/panel', :locals =>{:title => "Some Title"} do %>
      <p>Here is some content to be rendered inside the panel</p>
    <% end %>
    

    很遗憾,此错误不适用:

    ActionView::TemplateError (/Users/bradrobertson/Repos/VeloUltralite/source/trunk/app/views/sessions/new.html.erb:1: , unexpected tRPAREN
    
    old_output_buffer = output_buffer;;@output_buffer = '';  __in_erb_template=true ; @output_buffer.concat(( render :partial => '/shared/panel', :locals => {:title => "Welcome"} do ).to_s)
    on line #1 of app/views/sessions/new.html.erb:
    1: <%= render :partial => '/shared/panel', :locals => {:title => "Welcome"} do -%>
    ...
    

    所以它不喜欢 = 显然,如果移除一个块,它就不会输出任何内容。

    有人知道怎么做我想在这里实现的吗?我想在我的网站上的许多地方重复使用这个面板HTML。

    5 回复  |  直到 8 年前
        1
  •  182
  •   Pez Cuckow    10 年前

    虽然上面的两个答案都有效(托尼链接到的例子),但我最终在上面的帖子中找到了最简洁的答案(Kornelis Sietsma的评论)

    我猜 render :layout 确切地 我要找的是:

    # Some View
    <%= render :layout => '/shared/panel', :locals => {:title => 'some title'} do %>
      <p>Here is some content</p>
    <% end %>
    

    结合:

    # /shared/_panel.html.erb
    <div class="v-panel">
      <div class="v-panel-tr"></div>
      <h3><%= title -%></h3>
      <div class="v-panel-c">
        <%= yield %>
      </div>
    </div>
    
        2
  •  20
  •   rebagliatte    10 年前

    这里有一个基于先前答案的备选方案。

    创建您的部分 shared/_modal.html.erb :

    <div class="ui modal form">
      <i class="close icon"></i>
      <div class="header">
        <%= heading %>
      </div>
      <div class="content">
        <%= capture(&block) %>
      </div>
      <div class="actions">
        <div class="ui negative button">Cancel</div>
        <div class="ui positive button">Ok</div>
      </div>
    </div>
    

    定义方法 application_helper.rb :

    def modal_for(heading, &block)
      render(
        partial: 'shared/modal',
        locals: { heading: heading, block: block }
      )
    end
    

    从任何视图调用它:

    <%= modal_for('My Title') do |t| %>
      <p>Here is some content to be rendered inside the partial</p>
    <% end %>
    
        3
  •  8
  •   Yetanotherjosh    12 年前

    您可以使用捕获助手,甚至在渲染调用中使用内联:

    <%= render 'my_partial',
               :locals => { :title => "Some Title" },
               :captured => capture { %>
        <p>Here is some content to be rendered inside the partial</p>
    <% } %>
    

    在共享/面板中:

    <h3><%= title %></h3>
    <div class="my-outer-wrapper">
      <%= captured %>
    </div>
    

    将产生:

    <h3>Some Title</h3>
    <div class="my-outer-wrapper">
      <p>Here is some content to be rendered inside the partial</p>
    </div>
    

    http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html

        4
  •  2
  •   Kris    8 年前

    根据公认的答案,这是使用Rails 4对我很有效的方法。

    我们可以这样呈现一个面板:

    =render_panel('non-compliance reports',type:'primary')do
    %P你的内容在这里!
    < /代码> 
    
    

    这需要一个助手方法和一个共享视图:

    helper方法(ui_helper.rb)

    def render_panel(heading,options=,&block)
    选项。反向合并!(type:'默认')
    选项[:panel_classes]=[“panel-选项[:type]”]
    
    呈现布局:“/ui/panel”,局部变量:标题:标题,选项:选项do
    捕获(阻止)(&B)
    结束
    结束
    < /代码> 
    
    

    视图(/ui/panel.html.haml)

    .panel_class:options[:panel_classes]
    .panel heading=标题
    面板主体
    =产量
    < /代码> 
    

    enter image description here

    这需要一个助手方法和一个共享视图:

    helper方法(ui_helper.rb)

    def render_panel(heading, options = {}, &block)
      options.reverse_merge!(type: 'default')
      options[:panel_classes] = ["panel-#{options[:type]}"]
    
      render layout: '/ui/panel', locals: { heading: heading, options: options } do
        capture(&block)
      end
    end
    

    视图(/ui/panel.html.haml)

    .panel{ class: options[:panel_classes] }
      .panel-heading= heading
      .panel-body
        = yield
    
        5
  •  1
  •   Tesserex    15 年前

    我认为如果您先将它赋给一个变量,然后输出它,它就会工作(只是做了快速脏测试)。

    <% foo = render :partial => '/shared/panel', :locals =>{:title => "Some Title"} do %>
    <p>Here is some content to be rendered inside the panel</p>
    <% end %>
    <%= foo %>