Module: Railbars::ViewHelpers

Defined in:
lib/railbars/view_helpers.rb

Overview

View Helpers

Handlebar Helpers for Rails Views.

Instance Method Summary collapse

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}}"

Parameters:

  • name (String)

    The name of the value for the expression or the name of the block.

  • params (Array<Object>)

    The parameters the helper takes

Returns:

  • (String)


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}}"

Parameters:

  • name (String)

    The name of the block helper

  • params (Array<Object>)

    The parameters the helper takes

Returns:

  • (String)


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}}"

Parameters:

  • item (String)

    The name of item

Returns:

  • (String)


127
128
129
# File 'lib/railbars/view_helpers.rb', line 127

def hbeach(item, &block)
  hbblock('each', item, &block)
end

#hbelseString

Handlebars else helper, returns else wrapped in handlebars

Example:

irb> hbelse
#=> "{{hbelse}}"

Returns:

  • (String)


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}}"

Parameters:

  • exp (String)

    The value to be wrapped

Returns:

  • (String)


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}}"

Parameters:

  • name (String)

    The name of the helper

  • params (Array<Object>)

    The parameters the helper takes

Returns:

  • (String)


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}}"

Parameters:

  • condition (String)

    The condition of this block

Returns:

  • (String)


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\""

Parameters:

  • params (Array<Object>)

    The values to be translated

Returns:

  • (String)


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}}"

Parameters:

  • name (String)

    The name of the partial

  • params (Array<Object>)

    The parameters the partial takes

Returns:

  • (String)


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}}}"

Parameters:

  • value (String)

    The value to be wrapped

Returns:

  • (String)


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\""

Parameters:

  • hash (Hash)

    The hash to be translated

Returns:

  • (String)


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"

Parameters:

  • values (Array)

    The values to be translated

Returns:

  • (String)


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