Yield and default case || do not output default case

前端 未结 7 1236
刺人心
刺人心 2021-02-14 15:49

I have a simple yield use case and for some unknown reason the default case is never shown:

In my super_admin layout I have:

<%= yield :body_id || \'s         


        
7条回答
  •  误落风尘
    2021-02-14 16:19

    I know this is an old question but I have a solution for Rails 2.3.

    I've extended shingara's yield_or helper method above so it can now accept a block:

    module ApplicationHelper
      def yield_or(name, content = nil, &block)
        ivar = "@content_for_#{name}"
    
        if instance_variable_defined?(ivar)
          content = instance_variable_get(ivar)
        else
          content = block_given? ? capture(&block) : content
        end
    
        block_given? ? concat(content) : content
      end
    end
    

    and this can be used in your templates:

    <% yield_or :something do %>
        

    something else

    <% end %>

    or

    <%= yield_or :something, 'something else' %>
    

提交回复
热议问题