Class: OpenxmlDocxTemplater::Template

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, output) ⇒ Template

Returns a new instance of Template.



8
9
10
11
# File 'lib/openxml_docx_templater/template.rb', line 8

def initialize(template, output)
  FileUtils.cp(template, output)
  @template = output
end

Instance Attribute Details

#templateObject

Returns the value of attribute template.



6
7
8
# File 'lib/openxml_docx_templater/template.rb', line 6

def template
  @template
end

Instance Method Details

#process(context) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/openxml_docx_templater/template.rb', line 13

def process context
  tmpfiles = []
  Zip::File.open(@template) do |zipfile|
    zipfile.entries.select { |entry| entry.name[/\.xml$/] }.each do |xml_file|
      content = zipfile.read(xml_file).refact.force_encoding("utf-8")

      docxeruby = DocxEruby.new(XmlReader.new(content))
      out = docxeruby.evaluate(context)

      tmpfiles << (file = Tempfile.new("openxml_template"))
      file << out
      file.close

      zipfile.replace(xml_file, file.path)
    end
  end
end