Class: Spider::TemplateBlocks::AttrIf
- Defined in:
- lib/spiderfw/templates/blocks/attr_if.rb
Overview
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
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
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/spiderfw/templates/blocks/attr_if.rb', line 15 def compile(={}) 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() 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 |