class Sfp::Template

Public Class Methods

render(template, map) click to toggle source

Render given template string, and then return the result @template template string to be rendered @map a Hash of accessible variables in the template

# File lib/sfpagent/template.rb, line 30
def self.render(template, map)
        if map.is_a?(Hash)
                renderer = ::Sfp::Template.new(map)
                renderer.render(template)
        elsif map.is_a?(OpenStruct)
                ERB.new(template).result(map.instance_eval { binding })
        else
                raise Exception, 'A Hash or OpenStruct is required!'
        end
end
render_file(file, map) click to toggle source

Render given file, and then save the result back to the file @file target file to be rendered @map a Hash of accessible variables in the template

# File lib/sfpagent/template.rb, line 45
def self.render_file(file, map)
        renderer = ::Sfp::Template.new(map)
        renderer.render_file(file)
end

Public Instance Methods

render(template) click to toggle source

Render given template string, and then return the result @template template string to be rendered

# File lib/sfpagent/template.rb, line 8
def render(template)
        ERB.new(template).result(binding)
end
render_file(file) click to toggle source

Render given file, and then save the result back to the file @file target file that will be rendered

# File lib/sfpagent/template.rb, line 15
def render_file(file)
        File.open(file, File::RDWR|File::CREAT) do |f|
                f.flock(File::LOCK_EX)
                result = render(f.read)
                f.rewind
                f.write(result)
                f.flush
                f.truncate(f.pos)
        end
end