module Railbars::ViewHelpers

View Helpers

Handlebar Helpers for Rails Views.

Public Instance Methods

hb(name, *params, &block) click to toggle source

Handlebars helper, returns an expression, helper or block handlebar string

@param [String] name

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

@param [Array<Object>] params

The parameters the helper takes

@return [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}}"
# 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) click to toggle source

Handlebars block helper, returns the contents of the block wrapped with the specified block helper

@param [String] name

The name of the block helper

@param [Array<Object>] params

The parameters the helper takes

@return [String]

Example:

irb> hbblock('myBlockHelper', 'param') { '<p>Hello</p>' }
#=> "{{#myBlockHelper param}}<p>Hello</p>{{/myBlockHelper}}"
# 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) click to toggle source

Handlebars each helper, returns the contents of the block wrapped with each handlebars

@param [String] item

The name of item

@return [String]

Example:

irb> hbeach('point') { '<li>Hello</li>' }
#=> "{{#each point}}<li>Hello</li>{{/each}}"
# File lib/railbars/view_helpers.rb, line 127
def hbeach(item, &block)
  hbblock('each', item, &block)
end
hbelse() click to toggle source

Handlebars else helper, returns else wrapped in handlebars

@return [String]

Example:

irb> hbelse
#=> "{{hbelse}}"
# File lib/railbars/view_helpers.rb, line 152
def hbelse
  hbexp('else')
end
hbexp(exp) click to toggle source

Handlebars expression helper, returns the value wrapped in handlebars

@param [String] exp

The value to be wrapped

@return [String]

Example:

irb> hbexp('hello')
#=> "{{hello}}"
# File lib/railbars/view_helpers.rb, line 48
def hbexp(exp)
  "{{#{exp}}}"
end
hbhelper(name, *params) click to toggle source

Handlebars helper helper, returns the helper name with params

@param [String] name

The name of the helper

@param [Array<Object>] params

The parameters the helper takes

@return [String]

Example:

irb> hbhelper('myHelper', 'param1', 'param2')
#=> "{{myHelper param1 param2}}"
# File lib/railbars/view_helpers.rb, line 82
def hbhelper(name, *params)
  "{{#{name} #{hbparams(*params)}}}"
end
hbif(condition, &block) click to toggle source

Handlebars if helper, returns the contents of the block wrapped with if handlebars

@param [String] condition

The condition of this block

@return [String]

Example:

irb> hbif('present') { '<p>Hello</p>' }
#=> "{{#if present}}<p>Hello</p>{{/if}}"
# File lib/railbars/view_helpers.rb, line 141
def hbif(condition, &block)
  hbblock('if', condition, &block)
end
hbparams(*params) click to toggle source

Translates params into handlebars param literals and hash literals

@param [Array<Object>] params

The values to be translated

@return [String]

Example:

# Translate a hash into handlebars hash literals
irb> translate_values('cat', 'dog', food: 'lots')
#=> "cat dog food=\"lots\""
# 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) click to toggle source

Handlebars partial helper, returns the value wrapped in partial handlebars

@param [String] name

The name of the partial

@param [Array<Object>] params

The parameters the partial takes

@return [String]

Example:

irb> hbunescape('partialName', 'param1', 'param2')
#=> "{{> partial param1 param2}}"
# File lib/railbars/view_helpers.rb, line 113
def hbpartial(name, *params)
  "{{> #{name} #{hbparams(*params)} }}"
end
hbunescape(value) click to toggle source

Handlebars escape helper, returns the value wrapped in escaped handlebars

@param [String] value

The value to be wrapped

@return [String]

Example:

irb> hbunescape('hello')
#=> "{{{hello}}}"
# File lib/railbars/view_helpers.rb, line 96
def hbunescape(value)
  "{{{#{value}}}}"
end
translate_hash(hash) click to toggle source

Translates a hash into handlebars hash literals

@param [Hash] hash

The hash to be translated

@return [String]

Example:

# Translate a hash into handlebars hash literals
irb> translate_hash({miku: "senbonzakura", luka: "double lariat"})
#=> "miku=\"senbonzakura\" luka=\"double lariat\""
# 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) click to toggle source

Translates params into handlebars param literals

@param [Array] values

The values to be translated

@return [String]

Example:

# Translate a hash into handlebars hash literals
irb> translate_values('rin', 'len')
#=> "rin len"
# File lib/railbars/view_helpers.rb, line 189
def translate_values(values)
  if values.empty?
    nil
  else
    values.join(' ')
  end
end