arbre
arbre copied to clipboard
can i use helper methods that use arbre in *.arb templates ?
example that does not work:
app/helpers/application_helper.rb
module ApplicationHelper
def arbre(&block)
Arbre::Context.new({}, self, &block)
end
def static_pie_chart(data)
arbre do
div class: 'd3-pie-chart', 'data-series': data
end
end
end
app/views/dashboard/index/html.arb
h1 'Dashboard#index'
static_pie_chart [{label: 'first', value: 23}, {label: 'second', value: 46}]
and it's same as:
h1 'Dashboard#index'
Arbre::Context.new({}, self) do
div class: 'd3-pie-chart', 'data-series': [{label: 'first', value: 23}, {label: 'second', value: 46}]
end
and this is doesn't work. div with class 'd3-pie-chart' doesn't appear in rendered html because context that executes *.arb file just ignore new Arbre::Context
example that works but not like i want №1:
app/helpers/application_helper.rb
module ApplicationHelper
def arbre(&block)
Arbre::Context.new({}, self, &block)
end
def static_pie_chart(data)
arbre do
div class: 'd3-pie-chart', 'data-series': data
end
end
end
app/views/dashboard/index/html.arb
h1 'Dashboard#index'
div do
text_node static_pie_chart([{label: 'first', value: 23}, {label: 'second', value: 46}])
end
it works because text_node convert first parameter into string but i don't want to wrap each method into text_node
example that works but not like i want №2:
app/helpers/application_helper.rb
module ApplicationHelper
def arbre(&block)
Arbre::Context.new({}, self, &block).to_s
end
def static_pie_chart(data)
arbre do
div class: 'd3-pie-chart', 'data-series': data
end
end
end
app/views/dashboard/index/html.arb
h1 'Dashboard#index'
div do
static_pie_chart [{label: 'first', value: 23}, {label: 'second', value: 46}]
end
it works because static_pie_chart returns html_safe string but i don't want to wrap every method into div or span
question
how can i write helper methods if i want them to return child elements of current context?
arbre know about helpers but helpers doesn't know anything about context only solution i have right now is:
module ApplicationHelper
def arbre(&block)
Arbre::Context.new({}, self, &block).to_s
end
def static_pie_chart(data)
arbre do
div class: 'd3-pie-chart', 'data-series': data
end
end
end
h1 'Dashboard#index'
pie_chart = static_pie_chart [{label: 'first', value: 23}, {label: 'second', value: 46}]
text_node pie_chart
have you tried this?
module ApplicationHelper
def arbre(&block)
text_node Arbre::Context.new({}, self, &block).to_s
end
def static_pie_chart(data)
arbre do
div class: 'd3-pie-chart', 'data-series': data
end
end
end
# or
module ApplicationHelper
def arbre(&block)
Arbre::Context.new({}, self, &block).to_s
end
def static_pie_chart(data)
arbre do
text_node div(class: 'd3-pie-chart', 'data-series': data)
end
end
end
h1 'Dashboard#index'
static_pie_chart [{label: 'first', value: 23}, {label: 'second', value: 46}]
@timoschilling How can we use text_node in a helper?
@senid231 Don't do that. :scream: Try the Arbre way:
class StaticPieChart < Arbre::Component
builder_method :static_pie_chart
def default_class_name
'd3-pie-chart'
end
def buid(data, attributes = {})
super attributes.merge('data-series': data)
end
end
@knubie Don't do that. If you are trying to use text_node in a helper you are lost and confused. Post more details on StackOverflow tagged arbre and I'll respond.
thanks @varyonic I think this will help
Components are the way.