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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -