[英]How can I DRY up conditional content_for in Rails view?
contests/show.html.haml: 比赛/ show.html.haml:
- if modal
- content_for(:modal_header) do
= render 'contests/contest_header'
- content_for(:modal_body) do
= render 'contests/contest_body'
- else
= render 'contests/contest_header'
= render 'contests/contest_body'
modal
is a boolean local which indicates whether the view is being rendered in a modal template, vs the regular application template. modal
是一个布尔型局部变量,它指示视图是在模式模板中呈现的,还是在常规应用程序模板中呈现的。 The modal has content_for blocks while the main application template does not: 模态具有content_for块,而主应用程序模板则没有:
layouts/modal.html.haml: 布局/ modal.html.haml:
-modal = true
.modal-header
.row
.col-xs-11
= yield(:modal_header)
.col-xs-1
%button{'type': 'button', 'class': 'close', 'data-dismiss': 'modal', 'aria-label': 'close'}
%span{'aria-hidden': 'true'} ×
.modal-body
= render 'layouts/messages'
- if content_for?(:modal_body)
= yield(:modal_body)
- else
=yield
What I'm looking for is something that can conditionally make the content_for blocks disable when modal
is false. 我正在寻找的是可以在modal
为false时有条件地禁用content_for块的东西。 Rather than repeating my render statements. 而不是重复我的渲染语句。
Found a solution from another SO answer! 从另一个SO答案中找到了解决方案! https://stackoverflow.com/a/27398070/378622 https://stackoverflow.com/a/27398070/378622
Here's my implementation 这是我的实现
# app/helpers/application_helper.rb
def modal_content_for(name, &block)
if params[:modal] == '1'
content_for 'modal_' + name.to_s, nil, &block
else
capture_haml(&block)
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.