Class: Yass::LiquidTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/yass/liquid_template.rb

Defined Under Namespace

Classes: SiteDrop, SourceDrop

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, template) ⇒ LiquidTemplate

Returns a new instance of LiquidTemplate.



10
11
12
13
# File 'lib/yass/liquid_template.rb', line 10

def initialize(filename, template)
  @filename = filename
  @template = template
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



8
9
10
# File 'lib/yass/liquid_template.rb', line 8

def filename
  @filename
end

Class Method Details

.compile(site, filename, src) ⇒ Object



3
4
5
6
# File 'lib/yass/liquid_template.rb', line 3

def self.compile(site, filename, src)
  template = Liquid::Template.parse(src, environment: site.liquid_env)
  new(filename, template)
end

Instance Method Details

#nameObject



15
# File 'lib/yass/liquid_template.rb', line 15

def name = filename.sub(/\.liquid$/, "")

#render(source) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yass/liquid_template.rb', line 17

def render(source)
  vars = {}
  vars["page"] = SourceDrop.new(source)
  vars["site"] = SiteDrop.new(source.site)
  vars["content"] = yield if block_given?

  content = @template.render(vars, { strict_variables: true, strict_filters: true, registers: { source: source } })
  if @template.errors.any?
    source.site.stderr.puts "Errors found in #{filename}:"
    source.site.stderr.puts @template.errors.map { |e| "  #{e}" }.join("\n")
  end
  content
end