Sha256: 1644fc3652e728929fdac3bcac9dba2908dbbe70ab64040d0093452d8ca402b8

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
masterview-0.3.2 lib/masterview/directives/omit_tag.rb
masterview-0.3.4 lib/masterview/directives/omit_tag.rb
masterview-0.3.0 lib/masterview/directives/omit_tag.rb
masterview-0.3.1 lib/masterview/directives/omit_tag.rb
masterview-0.3.3 lib/masterview/directives/omit_tag.rb