class Handlebars::Helpers::Template

Register handlebars helpers

Public Class Methods

render(template, data = {}, &block) click to toggle source

Render template will take the template, compile it and merge it with the data

@param [String] template The handlebars template to render against @param [Hash, Array] data The data to merge with the template NOTE: I'm considering where to put the configuration block, &block)

# File lib/handlebars/helpers/template.rb, line 15
def self.render(template, data = {}, &block)
  register = if block_given?
               Handlebars::Helpers::RegisterHelpers.new(&block)
             else
               Handlebars::Helpers::RegisterHelpers.new
             end

  handlebars = register.handlebars
  compiled_template = handlebars.compile(template)

  begin
    obj = case data
          when String
            data
          when Array
            process_array(data)
          else
            process_hash(data)
          end

    compiled_template.call(obj)
  rescue StandardError => e
    puts 'Failed to process template'
    puts e.message
    # L.block 'Failed to process template', e.message
    # L.exception e
  end
end

Private Class Methods

process_array(data) click to toggle source
# File lib/handlebars/helpers/template.rb, line 44
def self.process_array(data)
  data.map do |item|
    if item.is_a?(String)
      item # Handle array of strings
    else
      process_hash(item)
    end
  end
end
process_hash(data) click to toggle source
# File lib/handlebars/helpers/template.rb, line 54
def self.process_hash(data)
  obj = data.to_h

  obj.each_key do |key|
    obj[key] = obj[key].to_h if obj[key].instance_of?(OpenStruct)
  end

  obj
end