module MasterView module Directives # omit tag if attr_value is empty or if at runtime the condition evaluates to true # mv:omit_tag="" will not render the tag, # mv:omit_tag="true" will not render the tag, # mv:omit_tag="false" will render the tag unconditionally # mv:omit_tag="expression" will conditionally render the tag subject to the expression class OmitTag < MasterView::DirectiveBase metadata :priority => 'UltraLow', :category => 'general', :description => 'Omits the element\'s tag from the generated output if the value is empty or evaluates to true' event :before_stag do # determine whether tag is omitted or included and if latter whether conditional spec = attr_value.strip() @omit_tag = spec.empty? || spec == 'true' @tag_condition = (@omit_tag || spec == 'false' ) ? nil : spec end event :stag do if @omit_tag render :nothing elsif @tag_condition.nil? render render_result else # had something we need to evaluate so wrap tag in condition render erb_eval( 'if', @tag_condition ) render render_result render erb_eval( 'end' ) end end event :etag do if @omit_tag render :nothing elsif @tag_condition.nil? render render_result else # had something we need to evaluate so wrap tag in condition render erb_eval( 'if', @tag_condition ) render render_result render erb_eval( 'end' ) end end end end end