Class: Pakman::LiquidTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/pakman/liquid/template.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, opts = {}) ⇒ LiquidTemplate

Returns a new instance of LiquidTemplate.



18
19
20
# File 'lib/pakman/liquid/template.rb', line 18

def initialize( text, opts={} )
  @template = Liquid::Template.parse( text )   # parses and compiles the template
end

Class Method Details

.from_file(path) ⇒ Object



8
9
10
11
12
# File 'lib/pakman/liquid/template.rb', line 8

def self.from_file( path )
  puts "  Loading template (from file) >#{path}<..."
  text = File.open( path, 'r:bom|utf-8' ).read     ## note: assume utf8
  self.new( text, path: path )   ## note: pass along path as an option
end

.from_string(text) ⇒ Object

use parse as alias - why?? why not??



14
15
16
# File 'lib/pakman/liquid/template.rb', line 14

def self.from_string( text )  ### use parse as alias - why?? why not??
  self.new( text )
end

Instance Method Details

#render(hash) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pakman/liquid/template.rb', line 22

def render( hash )
  ## note: hash keys MUST be strings (not symbols) e.g. 'name' => 'Toby'
  ## pp hash
  res = @template.render( hash,  { strict_variables: true, strict_filters: true } )

  ###
  ##  issue warnings/errors if present
  errors = @template.errors
  if errors.size > 0
    puts "!! WARN - #{errors.size} liquid error(s) when rendering template:"
    pp errors
  end

  res
end