Class: Textigniter::Parsers::TemplateParser

Inherits:
Object
  • Object
show all
Defined in:
lib/textigniter/parsers/template_parser.rb

Overview

This class parses templates. Currently it only parses liquid templates

Instance Method Summary collapse

Instance Method Details

#parse(layout_file, item, blogs) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/textigniter/parsers/template_parser.rb', line 24

def parse(layout_file, item, blogs)
  # merge item and blogs if item has blog key
  # if item.has_key? 'blog'
  blogs.each do |key, value|
    item["#{key}"] = value
  end
  # end  
            
  # require liquid lib
  require 'liquid'
  #parse the template
  Liquid::Template.file_system = Liquid::LocalFileSystem.new("#{$twd}/layouts")
  template = Liquid::Template.parse(layout_file)
  # render the template
  output = template.render(item)
  # return the output
  return output
end

#process(item_list) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/textigniter/parsers/template_parser.rb', line 4

def process(item_list)
  # Output message
  STDOUT.puts "Rendering ".yellow_on_black + "[liquid]".blue_on_black + " templates ".yellow_on_black + "[OK]".green_on_black
  # create an array to store processed templates
  items = Array.new
  # iterate through the item list and parse templates
  item_list['items'].each do |item|
    # specifiy the template
    file = File.open("#{$twd}/layouts/" + item['layout'] + '.liquid', 'rb')
    # load the template
    template_from_file = file.read
    # render the output
    item['output'] = parse(template_from_file, item, item_list['blogs'])         
    # push the item onto the array
    items.push item
  end
  # return the items
  return items
end