Class: DocxTemplater::TemplateProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/docx_templater/template_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, escape_html = true) ⇒ TemplateProcessor

data is expected to be a hash of symbols => string or arrays of hashes.



8
9
10
11
# File 'lib/docx_templater/template_processor.rb', line 8

def initialize(data, escape_html = true)
  @data = data
  @escape_html = escape_html
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/docx_templater/template_processor.rb', line 5

def data
  @data
end

#escape_htmlObject (readonly)

Returns the value of attribute escape_html.



5
6
7
# File 'lib/docx_templater/template_processor.rb', line 5

def escape_html
  @escape_html
end

Instance Method Details

#render(document) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/docx_templater/template_processor.rb', line 13

def render(document)
  document.force_encoding(Encoding::UTF_8) if document.respond_to?(:force_encoding)
  data.each do |key, value|
    if value.class == Array
      document = enter_multiple_values(document, key)
      document.gsub!("#SUM:#{key.to_s.upcase}#", value.count.to_s)
    else
      document.gsub!("$#{key.to_s.upcase}$", safe(value))
    end
  end
  document
end