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
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/arbre/context.rb', line 38
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
49
50
51
|
# File 'lib/arbre/context.rb', line 49
def arbre_context
self
end
|
#assigns ⇒ Object
53
54
55
|
# File 'lib/arbre/context.rb', line 53
def assigns
@_assigns
end
|
#bytesize ⇒ Object
Also known as:
length
66
67
68
|
# File 'lib/arbre/context.rb', line 66
def bytesize
cached_html.bytesize
end
|
#current_arbre_element ⇒ Object
86
87
88
|
# File 'lib/arbre/context.rb', line 86
def current_arbre_element
@_current_arbre_element_buffer.last
end
|
#helpers ⇒ Object
57
58
59
|
# File 'lib/arbre/context.rb', line 57
def helpers
@_helpers
end
|
#indent_level ⇒ Object
61
62
63
64
|
# File 'lib/arbre/context.rb', line 61
def indent_level
super - 1
end
|
#respond_to_missing?(method, include_all) ⇒ Boolean
71
72
73
|
# File 'lib/arbre/context.rb', line 71
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
90
91
92
93
94
95
|
# File 'lib/arbre/context.rb', line 90
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
|