Documentation on the renderer

This commit is contained in:
Mitchell Hashimoto 2010-03-17 17:04:21 -07:00
parent edc1bbec8c
commit 974624e5cb
1 changed files with 17 additions and 2 deletions

View File

@ -3,8 +3,14 @@ module Vagrant
# `GEM_ROOT/templates` directory.
class TemplateRenderer < OpenStruct
class <<self
def render!(*args)
renderer = new(*args)
# Render a given template and return the result. This method optionally
# takes a block which will be passed the renderer prior to rendering, which
# allows the caller to set any view variables within the renderer itself.
#
# @param [String] template Name of the template file, without the extension
# @return [String] Rendered template
def render!(template, data={})
renderer = new(template, data)
yield renderer if block_given?
renderer.render
end
@ -19,6 +25,11 @@ module Vagrant
end
end
# Renders the template using the class intance as the binding. Because the
# renderer inherits from `OpenStruct`, additional view variables can be
# added like normal accessors.
#
# @return [String]
def render
result = nil
File.open(full_template_path, 'r') do |f|
@ -29,6 +40,10 @@ module Vagrant
result
end
# Returns the full path to the template, taking into accoun the gem directory
# and adding the `.erb` extension to the end.
#
# @return [String]
def full_template_path
File.join(PROJECT_ROOT, 'templates', "#{template}.erb")
end