Module: Liquify

Defined in:
lib/liquify.rb,
lib/liquify/tag.rb,
lib/liquify/drop.rb,
lib/liquify/block.rb,
lib/liquify/methods.rb,
lib/liquify/version.rb,
lib/liquify/parameter.rb,
lib/generators/liquify/install_generator.rb

Defined Under Namespace

Modules: ClassMethods, Generators, InstanceMethods, Methods Classes: Block, Drop, Parameter, Tag

Constant Summary collapse

VERSION =
"0.2.3"
@@tags =
{}
@@filters =
[]
@@drops =
{}

Class Method Summary collapse

Class Method Details

.invoke(template, extra_context = {}) ⇒ Object

invoke = This method handles to rendering of the Liquid template with all of the registered drops, tags and filters. It accepts the Liquid template as a string.

template = '{{ drop_name.method }}'
Liquify.invoke(template) # => Rendered Liquid template


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/liquify.rb', line 69

def invoke(template, extra_context={})
  context = {}
  @@drops.each { |name, klass| context[name.to_s] = klass.respond_to?(:call) ? klass.call : klass.new }
  @@filters.each { |filter| Liquid::Template.register_filter(filter) }
  @@tags.each { |tag, klass| Liquid::Template.register_tag(tag, klass) }

  # ensuring any extra context values that come in as symbols are
  # converted to strings
  extra_keys = extra_context.keys.map!(&:to_s)
  extra_context = Hash[extra_keys.zip(extra_context.values)]

  context.merge!(extra_context)

  Liquid::Template.parse(template).render(context)
end

.register_drop(name, klass) ⇒ Object

register_drop - This method is used to make drops available to your Liquid templates. It accepts a symbol for the drop name and your drop class or a lambda if processing has to be done at time time of rendering the Liquid template.

Liquify.setup do |config|
  config.register_drop :drop_name, NameDrop
  # with a lamda
  config.register_drop :special_name, lambda { Foo.first }
end


59
60
61
# File 'lib/liquify.rb', line 59

def register_drop(name, klass)
  @@drops[name] = klass
end

.register_filters(mod) ⇒ Object

register_filters - This method is used to make filters available to your Liquid templates. It accepts a module of methods. Each method becomes a Liquid filter.

Liquify.setup do |config|
  config.register_filters CustomFilters
end


45
46
47
# File 'lib/liquify.rb', line 45

def register_filters(mod)
  @@filters << mod
end

.register_tag(name, klass) ⇒ Object

register_tag - This method is used to make tags available to your Liquid templates. It accepts a symbol for the tag name and the tag class.

Liquify.setup do |config|
  config.register_tag :tag_name, NameTag
end


34
35
36
# File 'lib/liquify.rb', line 34

def register_tag(name, klass)
  @@tags[name] = klass
end

.render(template) ⇒ Object



85
86
87
88
# File 'lib/liquify.rb', line 85

def render(template)
  warn 'DEPRECATED: render has been replaced with invoke'
  invoke(template)
end

.setup {|_self| ... } ⇒ Object

setup - This method allows you to register tags, drops and filters. to Liquify in one place.

Liquify.setup do |config|
  ...
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Liquify)

    the object that the method was called on



24
25
26
# File 'lib/liquify.rb', line 24

def setup
  yield self
end