局部,布局,模板渲染问题 [英] Partial, Layout, Template rendering problems

查看:81
本文介绍了局部,布局,模板渲染问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当我访问页面时,我希望能够将布局应用于部分(我希望三个具有相同布局的部分).现在,我正在尝试使用以下命令:

So when I visit a page, I want to be able to apply a layout to a partial (I have three partials that I want with the same layout). Right now, I am trying to do it with this command:

<%= render :partial => "shared/services/essay", :layout => "layouts/services/tab_pane", :locals => { :service => "essay" } %>

其中shared/services/essay类似于:

<% content_for :intro do %>
<p>
    blah.
</p>
<% end %>

<% content_for :workflow do %>
<div>
    blah.
</div>
<% end %>

<% content_for :value_prop do %>
<p>
    blah.
</p>
<% end %>

和`layouts/services/tab_pane'像这样:

and `layouts/services/tab_pane' goes like:

<div class="tab-pane fade in" id=<%= service %> >
    <%= yield :intro %>

    <div class="span7 workflow">
        <h3>Workflow</h3>
        <%= yield :workflow %>
    </div>

    <div class="span5 value-proposition">
        <h3>Our Value Proposition for You</h3>
        <%= yield :value_prop %>
        <div class="call-to-action">
            <%= link_to "Action!", contact_path, class: "btn btn-large btn-warning" %>
        </div>
    </div>
</div>

问题

当我加载它时,我只获得布局HTML.没有内容通过.因此,从本质上讲,收益并没有吸引到任何内容.

The Problem

When I load it up, I just get the layout HTML. None of the content is put through. So essentially, the yields aren't pulling in content or something.

有人可以帮忙吗?谢谢!

Could someone help? Thanks!

如果我尝试使用Ruby Guides上提到的嵌套布局(添加...

If I try nested layouts as mentioned on Ruby Guides (adding...

<%= render :template => "layouts/services/tab_pane", :locals  => { :service => "essay" } %>

更改我的部分,并更改所有正确的名称),第二部分包含第一部分的内容,第三部分包含第一部分和第二部分的内容.因此,我的第二部分将每个部分包含2个blah,而我的第三部分将每个部分包含3个blah.谢谢!

to my partials and making all the proper name changes), my second partial contains content from the first, and my third from both the first and second. So my second partial would contain 2 blah's for each section and my third partial would contain 3 blah's for each section. Thanks!

这个问题现在已经解决了(感谢Michael),但是现在我想我想知道在处理类似问题时的最佳实践是什么.我将来是否应该避免这样的事情,而只担心为整个页面而不是页面中的局部页面提供模板/布局?谢谢!

The problem is now solved (thanks Michael), but now I guess I was wondering what best practices were when dealing with something like this. Should I avoid something like this in the future and only worry about giving templates/layout to whole pages as opposed to partials within the page? Thanks!

推荐答案

我想知道您是否对Partial的ViewFlow处理感到不满.您多次打这个电话吗?问题是Rails将content_for数据存储在散列中,并且是非破坏性的.

I wonder if you are getting hit with the ViewFlow handling of partials. Are you calling this partial several times? The problem is rails stores the content_for data in a hash and its non-destructive.

您可以尝试添加:

def yield_content!(content_key) view_flow.content.delete(content_key) end

def yield_content!(content_key) view_flow.content.delete(content_key) end

在ApplicationHelper中,然后调用yield_content而不是布局文件中的yield.

in your ApplicationHelper and call yield_content instead of yield in your layout file.

这篇关于局部,布局,模板渲染问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆