Module: Railbars::ViewHelpers
- Defined in:
- lib/railbars/view_helpers.rb
Overview
View Helpers
Handlebar Helpers for Rails Views.
Instance Method Summary collapse
-
#hb(name, *params, &block) ⇒ String
Handlebars helper, returns an expression, helper or block handlebar string.
-
#hbblock(name, *params, &block) ⇒ String
Handlebars block helper, returns the contents of the block wrapped with the specified block helper.
-
#hbeach(item, &block) ⇒ String
Handlebars each helper, returns the contents of the block wrapped with each handlebars.
-
#hbelse ⇒ String
Handlebars else helper, returns else wrapped in handlebars.
-
#hbexp(exp) ⇒ String
Handlebars expression helper, returns the value wrapped in handlebars.
-
#hbhelper(name, *params) ⇒ String
Handlebars helper helper, returns the helper name with params.
-
#hbif(condition, &block) ⇒ String
Handlebars if helper, returns the contents of the block wrapped with if handlebars.
-
#hbparams(*params) ⇒ String
Translates params into handlebars param literals and hash literals.
-
#hbpartial(name, *params) ⇒ String
Handlebars partial helper, returns the value wrapped in partial handlebars.
-
#hbunescape(value) ⇒ String
Handlebars escape helper, returns the value wrapped in escaped handlebars.
-
#translate_hash(hash) ⇒ String
Translates a hash into handlebars hash literals.
-
#translate_values(values) ⇒ String
Translates params into handlebars param literals.
Instance Method Details
#hb(name, *params, &block) ⇒ String
Handlebars helper, returns an expression, helper or block handlebar string
Example:
irb> hb('hello')
#=> "{{hello}}"
irb> hb('myHelper', 'param1', 'param2')
#=> "{{myHelper param1 param2}}"
irb> hb('myBlockHelper', 'param') { '<p>Hello</p>' }
#=> "{{#myBlockHelper param}}<p>Hello</p>{{/myBlockHelper}}"
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/railbars/view_helpers.rb', line 26 def hb(name, *params, &block) if block hbblock(name, *params, &block) else if params.any? hbhelper(name, *params) else hbexp(name) end end end |
#hbblock(name, *params, &block) ⇒ String
Handlebars block helper, returns the contents of the block wrapped with the specified block helper
Example:
irb> hbblock('myBlockHelper', 'param') { '<p>Hello</p>' }
#=> "{{#myBlockHelper param}}<p>Hello</p>{{/myBlockHelper}}"
65 66 67 |
# File 'lib/railbars/view_helpers.rb', line 65 def hbblock(name, *params, &block) "{{##{name} #{hbparams(*params)}}}#{capture(&block)}{{/#{name}}}".html_safe end |
#hbeach(item, &block) ⇒ String
Handlebars each helper, returns the contents of the block wrapped with each handlebars
Example:
irb> hbeach('point') { '<li>Hello</li>' }
#=> "{{#each point}}<li>Hello</li>{{/each}}"
127 128 129 |
# File 'lib/railbars/view_helpers.rb', line 127 def hbeach(item, &block) hbblock('each', item, &block) end |
#hbelse ⇒ String
Handlebars else helper, returns else wrapped in handlebars
Example:
irb> hbelse
#=> "{{hbelse}}"
152 153 154 |
# File 'lib/railbars/view_helpers.rb', line 152 def hbelse hbexp('else') end |
#hbexp(exp) ⇒ String
Handlebars expression helper, returns the value wrapped in handlebars
Example:
irb> hbexp('hello')
#=> "{{hello}}"
48 49 50 |
# File 'lib/railbars/view_helpers.rb', line 48 def hbexp(exp) "{{#{exp}}}" end |
#hbhelper(name, *params) ⇒ String
Handlebars helper helper, returns the helper name with params
Example:
irb> hbhelper('myHelper', 'param1', 'param2')
#=> "{{myHelper param1 param2}}"
82 83 84 |
# File 'lib/railbars/view_helpers.rb', line 82 def hbhelper(name, *params) "{{#{name} #{hbparams(*params)}}}" end |
#hbif(condition, &block) ⇒ String
Handlebars if helper, returns the contents of the block wrapped with if handlebars
Example:
irb> hbif('present') { '<p>Hello</p>' }
#=> "{{#if present}}<p>Hello</p>{{/if}}"
141 142 143 |
# File 'lib/railbars/view_helpers.rb', line 141 def hbif(condition, &block) hbblock('if', condition, &block) end |
#hbparams(*params) ⇒ String
Translates params into handlebars param literals and hash literals
Example:
# Translate a hash into handlebars hash literals
irb> translate_values('cat', 'dog', food: 'lots')
#=> "cat dog food=\"lots\""
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/railbars/view_helpers.rb', line 167 def hbparams(*params) if params.empty? nil else values = translate_values(params.select { |p| p.is_a?(String) }) hash = translate_hash(params.last.is_a?(Hash) ? params.last : {}) [values, hash].compact.join(' ') end end |
#hbpartial(name, *params) ⇒ String
Handlebars partial helper, returns the value wrapped in partial handlebars
Example:
irb> hbunescape('partialName', 'param1', 'param2')
#=> "{{> partial param1 param2}}"
113 114 115 |
# File 'lib/railbars/view_helpers.rb', line 113 def hbpartial(name, *params) "{{> #{name} #{hbparams(*params)} }}" end |
#hbunescape(value) ⇒ String
Handlebars escape helper, returns the value wrapped in escaped handlebars
Example:
irb> hbunescape('hello')
#=> "{{{hello}}}"
96 97 98 |
# File 'lib/railbars/view_helpers.rb', line 96 def hbunescape(value) "{{{#{value}}}}" end |
#translate_hash(hash) ⇒ String
Translates a hash into handlebars hash literals
Example:
# Translate a hash into handlebars hash literals
irb> translate_hash({miku: "senbonzakura", luka: "double lariat"})
#=> "miku=\"senbonzakura\" luka=\"double lariat\""
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/railbars/view_helpers.rb', line 208 def translate_hash(hash) if hash.empty? nil else hash.map do |k, v| value = v.is_a?(String) ? "\"#{v}\"" : v "#{k}=#{value}" end.join(' ') end end |
#translate_values(values) ⇒ String
Translates params into handlebars param literals
Example:
# Translate a hash into handlebars hash literals
irb> translate_values('rin', 'len')
#=> "rin len"
189 190 191 192 193 194 195 |
# File 'lib/railbars/view_helpers.rb', line 189 def translate_values(values) if values.empty? nil else values.join(' ') end end |