Rails 视图:如何将变量传递给部分布局?

发布于 2024-09-15 04:18:30 字数 973 浏览 7 评论 0原文

在我的一个视图中,我将布局应用于代码块:

# In app/views/sessions/new.html.erb
<% render :layout => 'home/shadow_box' do %>
  #... code for sign in form here
<% end %>

布局是一个 div,所有四个侧面都有 png 阴影。

由于我在整个网站上都使用此布局,因此我想将一个变量传递给指定阴影 div 宽度的布局。我尝试在代码块中使用内容:

# In app/views/sessions/new.html.erb
<% render :layout => 'home/shadow_box' do %>
  <% content_for :box_width %>640<% end %>
  #... code for sign in form here
<% end %>

# In app/views/home/_shadow_box.html.erb
<div class="shadow-one" style="width:<%= yield :box_width %>;">
  <div class="corner-a"></div>
  <div class="corner-b"></div>
  <div class="shadow-two">
    <div class="shadow-three">
      <div class="shadow-four">
        <%= yield %>
      </div>
    </div>
  </div>
</div>

这不起作用,反而导致整个代码块的双重渲染。

解决这个问题的最佳方法是什么?

In one of my views I apply a layout to a block of code:

# In app/views/sessions/new.html.erb
<% render :layout => 'home/shadow_box' do %>
  #... code for sign in form here
<% end %>

The layout is a div that has png shadows on all four sides.

Since I use this layout all over my site, I want to pass a variable to the layout that specifies the width of the shadowed div. I tried using content for in the code block:

# In app/views/sessions/new.html.erb
<% render :layout => 'home/shadow_box' do %>
  <% content_for :box_width %>640<% end %>
  #... code for sign in form here
<% end %>

# In app/views/home/_shadow_box.html.erb
<div class="shadow-one" style="width:<%= yield :box_width %>;">
  <div class="corner-a"></div>
  <div class="corner-b"></div>
  <div class="shadow-two">
    <div class="shadow-three">
      <div class="shadow-four">
        <%= yield %>
      </div>
    </div>
  </div>
</div>

This didn't work and instead resulted in a double render of the entire code block.

What's the best way to tackle this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看轻我的陪伴 2024-09-22 04:18:30

想通了。

来自 API:“您还可以在一个布局中多次生成并使用块参数来区分各个部分。”

解决方案:

# In app/views/sessions/new.html.erb
<% render :layout => 'home/shadow_box' do | section | %>
  <%- case section when :box_width -%>
    #width goes here. I.e., 640px
  <%- when :content -%>
    #code block goes here
  <% end -%>
<% end %>

#In app/views/home/_shadow_box.html.erb
<div class="shadow-one" style="width:<%= yield :box_width %>;">
  <div class="corner-a"></div>
  <div class="corner-b"></div>
  <div class="shadow-two">
    <div class="shadow-three">
      <div class="shadow-four">
        <%= yield :content %>
      </div>
    </div>
  </div>
</div>

Figured it out.

From the API: "You can also yield multiple times in one layout and use block arguments to differentiate the sections."

Solution:

# In app/views/sessions/new.html.erb
<% render :layout => 'home/shadow_box' do | section | %>
  <%- case section when :box_width -%>
    #width goes here. I.e., 640px
  <%- when :content -%>
    #code block goes here
  <% end -%>
<% end %>

#In app/views/home/_shadow_box.html.erb
<div class="shadow-one" style="width:<%= yield :box_width %>;">
  <div class="corner-a"></div>
  <div class="corner-b"></div>
  <div class="shadow-two">
    <div class="shadow-three">
      <div class="shadow-four">
        <%= yield :content %>
      </div>
    </div>
  </div>
</div>
忆伤 2024-09-22 04:18:30

首先,您需要了解布局部分之间的区别。部分通常来自视图,但如果您使用 ajax,也可以从控制器使用。布局几乎总是在控制器中使用。

首先在共享文件夹(例如 application/)中创建一个文件,然后在此文件夹中放置一个文件,无论您想要什么名称,都可以将其命名为任何名称,但它将包含您想要在整个站点中包含的材料。然后,当您将变量传递给分部时,它会在分部中作为局部变量进行调用。另外,对于部分,您不需要说 render :partial => 您只需输入 render 'application/some_file'

所以从您想要的视图来看:

<%= render 'application/your_file', :div_size => '600' %>

然后从文件夹中的部分内容(例如 application/your_file.html.erb)执行以下操作:

<div style="width:<%= div_width %>px;">
   content
</div>

First you need to know the difference between layouts and partials. Partials are generally from the view but can also be used from the controller if you are using ajax. Layouts are almost always used in the controller.

First create a file in a shared folder such as application/ and in this folder put a file call it whatever you want but it will contain the material that you want to include all over your site. Then when you pass a variable to a partial it's called in the partial as a local variable. Also with partials you don't need to say render :partial => you just put render 'application/some_file'

So from the view you want this:

<%= render 'application/your_file', :div_size => '600' %>

And then from the partial in the folder such as application/your_file.html.erb do this:

<div style="width:<%= div_width %>px;">
   content
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文