module Antex::LiquidHelpers

Exposes helper methods to simplify Liquid templates rendering.

Public Instance Methods

liquid_render(object, context_hash = {}) click to toggle source

Recursively renders Liquid template strings, possibly organized in nested arrays and hashes, using the given hash of contextual variables.

@param object [String, Array, Hash] the object to render @param context_hash [Hash]

the context hash accessible from the object strings

@return [String] the rendered object @raise [UnknownClass] when given anything that's not renderable

# File lib/antex/liquid_helpers.rb, line 18
def liquid_render(object, context_hash = {})
  case object
  when String
    liquid_render_string object, context_hash
  when Array
    liquid_render_array object, context_hash
  when Hash
    liquid_render_hash object, context_hash
  else
    raise UnknownClass, "I don't know how to render a #{object.class}."
  end
end

Private Instance Methods

liquid_render_array(array, context_hash = {}) click to toggle source
# File lib/antex/liquid_helpers.rb, line 37
def liquid_render_array(array, context_hash = {})
  array.map do |element|
    liquid_render element, context_hash
  end
end
liquid_render_hash(hash, context_hash = {}) click to toggle source
# File lib/antex/liquid_helpers.rb, line 43
def liquid_render_hash(hash, context_hash = {})
  hash.map do |key, value|
    [key, liquid_render(value, context_hash)]
  end.to_h
end
liquid_render_string(string, context_hash = {}) click to toggle source
# File lib/antex/liquid_helpers.rb, line 33
def liquid_render_string(string, context_hash = {})
  Liquid::Template.parse(string).render(context_hash)
end