Class: Spider::TemplateBlocks::If

Inherits:
Block show all
Defined in:
lib/spiderfw/templates/blocks/if.rb

Instance Attribute Summary

Attributes inherited from Block

#allowed_blocks, #doctype, #el, #template

Instance Method Summary collapse

Methods inherited from Block

#compile_content, #compile_text, #escape_text, #get_following, #initialize, #inspect, #parse_content, var_to_scene, #var_to_scene, #vars_to_scene, vars_to_scene

Constructor Details

This class inherits a constructor from Spider::TemplateBlocks::Block

Instance Method Details

#compile(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/spiderfw/templates/blocks/if.rb', line 7

def compile(options={})
    init = ""
    init_cond = nil
    if_attr = @el.attributes['sp:if'] || @el.attributes['sp:run-if']
    init_cond = vars_to_scene(@el.attributes['sp:if'], 'scene') if @el.attributes['sp:if']
    run_cond = vars_to_scene(if_attr)
    c = "if (#{run_cond})\n"
    @el.remove_attribute('sp:if')
    @el.remove_attribute('sp:run-if')
    content = Spider::TemplateBlocks.parse_element(@el, @allowed_blocks, @template).compile(options)
    content.run_code.each_line do |line|
        c += '  '+line
    end
    c += "end\n"
    unless (!content.init_code || content.init_code.strip.empty?)
        init += "if (#{init_cond})\n" if init_cond
        content.init_code.each_line do |line|
            init += "  #{line}"
        end
        init += "end\n" if init_cond
    end
    return CompiledBlock.new(init, c)
end