Sha256: 21bb893346b0908df64296fc4ece2aa2d99eb558ce01a28690175816515d788c

Contents?: true

Size: 1.2 KB

Versions: 7

Compression:

Stored size: 1.2 KB

Contents

module Sablon
  class Template
    def initialize(path)
      @path = path
    end

    # Same as +render_to_string+ but writes the processed template to +output_path+.
    def render_to_file(output_path, context, properties = {})
      File.open(output_path, 'w') do |f|
        f.write render_to_string(context, properties)
      end
    end

    # Process the template. The +context+ hash will be available in the template.
    def render_to_string(context, properties = {})
      render(context, properties).string
    end

    private
    def render(context, properties = {})
      Zip::OutputStream.write_buffer(StringIO.new) do |out|
        Zip::File.open(@path).each do |entry|
          entry_name = entry.name
          out.put_next_entry(entry_name)
          content = entry.get_input_stream.read
          if entry_name == 'word/document.xml'
            out.write(Processor.process(Nokogiri::XML(content), context, properties).to_xml)
          elsif entry_name =~ /word\/header\d*\.xml/ || entry_name =~ /word\/footer\d*\.xml/
            out.write(Processor.process(Nokogiri::XML(content), context).to_xml)
          else
            out.write(content)
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sablon-0.0.8 lib/sablon/template.rb
sablon-0.0.7 lib/sablon/template.rb
sablon-0.0.6 lib/sablon/template.rb
sablon-0.0.5 lib/sablon/template.rb
sablon-0.0.4 lib/sablon/template.rb
sablon-0.0.3 lib/sablon/template.rb
sablon-0.0.2 lib/sablon/template.rb