Skip to content

Instantly share code, notes, and snippets.

@rbmrclo
Created October 22, 2013 04:37
Show Gist options
  • Save rbmrclo/7095300 to your computer and use it in GitHub Desktop.
Save rbmrclo/7095300 to your computer and use it in GitHub Desktop.
Liquid
class Liquify
attr_accessor :content, :template
class ::InvalidInputException < Exception; end;
def initialize(content)
@content = content
end
def template
@template ||= Liquid::Template.parse @content
end
def variables
template.root.nodelist.select{|node| node.respond_to? :name}.map{|n| n.name}
end
def substitute(params = {})
unless params.class == Hash || params.class == HashWithIndifferentAccess
raise InvalidInputException
end
template.render hashify params
end
def variables_in?(params = [])
unless params.class == Array
raise InvalidInputException
end
variables & params.map{|m| m.to_s} == variables
end
def variables_not_in?(params = [])
!variables_in? params
end
private
def hashify(params = {})
hash = {}
params.each do |k,v|
hash[k.to_s] = v
end
hash
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment