Class: Arbre::Context
Overview
The Arbre::Context class is the frontend for using Arbre.
The simplest example possible:
html = Arbre::Context.new do
h1 "Hello World"
end
html.to_s
The contents of the block are instance eval’d within the Context object. This means that you lose context to the outside world from within the block. To pass local variables into the Context, use the assigns param.
html = Arbre::Context.new({one: 1}) do
h1 "Your number #{one}"
end
html.to_s
Instance Attribute Summary
Attributes inherited from Element
#children, #parent
Instance Method Summary
collapse
Methods inherited from Element
#+, #<<, #add_child, #ancestors, #build, #children?, #content, #content=, #each, #find_first_ancestor, #get_elements_by_class_name, #get_elements_by_tag_name, #html_safe, #inspect, #parent?, #remove_child, #tag_name, #to_ary, #to_s, #to_str
#render
#build_tag, included, #insert_tag
Constructor Details
#initialize(assigns = {}, helpers = nil) { ... } ⇒ Context
Initialize a new Arbre::Context
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/arbre/context.rb', line 37
def initialize(assigns = {}, helpers = nil, &block)
assigns = assigns || {}
@_assigns = assigns.symbolize_keys
@_helpers = helpers
@_current_arbre_element_buffer = [self]
super(self)
instance_eval(&block) if block
end
|
Instance Method Details
#arbre_context ⇒ Object
48
49
50
|
# File 'lib/arbre/context.rb', line 48
def arbre_context
self
end
|
#assigns ⇒ Object
52
53
54
|
# File 'lib/arbre/context.rb', line 52
def assigns
@_assigns
end
|
#bytesize ⇒ Object
Also known as:
length
65
66
67
|
# File 'lib/arbre/context.rb', line 65
def bytesize
cached_html.bytesize
end
|
#current_arbre_element ⇒ Object
85
86
87
|
# File 'lib/arbre/context.rb', line 85
def current_arbre_element
@_current_arbre_element_buffer.last
end
|
#helpers ⇒ Object
56
57
58
|
# File 'lib/arbre/context.rb', line 56
def helpers
@_helpers
end
|
#indent_level ⇒ Object
60
61
62
63
|
# File 'lib/arbre/context.rb', line 60
def indent_level
super - 1
end
|
#respond_to_missing?(method, include_all) ⇒ Boolean
70
71
72
|
# File 'lib/arbre/context.rb', line 70
def respond_to_missing?(method, include_all)
super || cached_html.respond_to?(method, include_all)
end
|
#with_current_arbre_element(tag) ⇒ Object
Also known as:
within
89
90
91
92
93
94
|
# File 'lib/arbre/context.rb', line 89
def with_current_arbre_element(tag)
raise ArgumentError, "Can't be in the context of nil. #{@_current_arbre_element_buffer.inspect}" unless tag
@_current_arbre_element_buffer.push tag
yield
@_current_arbre_element_buffer.pop
end
|