ruby - Adding a conditional block in ERB -
i have following in erb layout:
<%# check block -%> <% if content_for?(:layoutblock) %> <%# if so, yield -%> <%= yield :layoutblock %> <%# otherwise... -%> <% else %> <%# stuff -%> <% end %>
which checks see if view has content layoutblock
, , if let run, otherwise execute own content layoutblock
.
is there way define alias simplify to:
<% optionalyield :layoutblock %> <%# stuff -%> <% end %>
i expect helper work
# in helper def optional_block(block_name, &block) concat(content_for(block_name).presence || capture(&block)) end
which used in view this:
<% optional_block(:layoutblock) %> # content rendered when content_for(:layoutblock) blank <% end %>
Comments
Post a Comment