Sha256: 2b3c4b6a5dff66372f03e2dc088a517ee3d3af9121812048c6962230039453d8

Contents?: true

Size: 1.38 KB

Versions: 48

Compression:

Stored size: 1.38 KB

Contents

require 'spiderfw/templates/template_blocks'

module Spider; module TemplateBlocks
    
    # *sp:attr-if*
    # Adds an attribute if a condition holds
    # Example:
    #   <div sp:attr-if="@my_condition,class,coolDiv"></div>
    # The attribute's value can be a scene variable, but not an expression: so
    #  <div sp:attr-if"@my_condition,attr_name,@attr_value" /> is valid, but
    #  <div sp:attr-if"@my_condition,attr_name,@attr_value_start+@attr_value_end" /> is not
    
    class AttrIf < Block
        
        def compile(options={})
            init = ""
            attr_if = @el.get_attribute('sp:attr-if')
            @el.remove_attribute('sp:attr-if')
            @el.set_attribute("tmp-attr-if", '---')
            compiled = Spider::TemplateBlocks.parse_element(@el, @allowed_blocks, @template).compile(options)
            c, init = compiled.run_code, compiled.init_code
            cond, name, val = attr_if.split(',')
            if val && val.strip[0].chr == '@'
                val = "'+#{var_to_scene(val)}+'"
            end
            cond = vars_to_scene(cond)
            full_attr = val ? "#{name}=\"#{val}\"" : "#{name}"
            replace = "'+"+"( (#{cond}) ? '#{full_attr}' : '' )"+"+'"
#            debug("ATTR IF REPLACe")
            c.sub!('tmp-attr-if="---"', replace)
            return CompiledBlock.new(init, c)
        end
        
        
    end
    
    
end; end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
spiderfw-1.0.1 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-1.0.0 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.39 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.38 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.37 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.35 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.34 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.33 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.32 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.31 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.30 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.29 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.28 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.27 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.26 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.26.pre1 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.25 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.24 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.23 lib/spiderfw/templates/blocks/attr_if.rb
spiderfw-0.6.22 lib/spiderfw/templates/blocks/attr_if.rb