Class: Templater

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

Overview

Indexing

Author: Stefan Rusterholz Contact: [email protected]> Version: 0.0.1 Date: 2007-10-12

About

A helper class for ERB, allows constructs like the one in the Synopsis to enable simple use of variables/methods in templates.

Synopsis

tmpl = Templater.new("Hello <%= name %>!")
tmpl.result(self, :name => 'world') # => 'Hello World!'

Defined Under Namespace

Modules: VERSION Classes: Variables

Constant Summary collapse

Raiser =

A proc for &on_error in Templater::Variables.new or Templater#result. Raises the error further on.

proc { |e|
	raise
}
Teller =

A proc for &on_error in Templater::Variables.new or Templater#result. Inserts <<error_class: error_message>> in the place where the error occurred.

proc { |e|
	"<<#{e.class}: #{e}>>"
}
Opt =

Option defaults

{
	:safe_level => nil,
	:trim_mode  => '%<>',
	:eoutvar    => '_erbout'
}
Binder =

binding method

Object.instance_method(:binding)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, opt = {}) ⇒ Templater

Arguments

  • string: The template string, it becomes frozen

  • opt: Option hash, keys:

    • :filename: The filename used for the evaluation (useful for error messages)

    • :safe_level: see ERB.new

    • :trim_mode: see ERB.new

    • :eoutvar: see ERB.new



116
117
118
119
120
121
122
123
# File 'lib/templater.rb', line 116

def initialize(string, opt={})
	opt, string   = string, nil if string.kind_of?(Hash)
	opt           = Opt.merge(opt)
	file          = opt.delete(:filename)
	@string       = string.freeze
	@erb          = ERB.new(@string, *opt.values_at(:safe_level, :trim_mode, :eoutvar))
	@erb.filename = file if file
end

Instance Attribute Details

#stringObject (readonly)

The template string



101
102
103
# File 'lib/templater.rb', line 101

def string
  @string
end

Class Method Details

.file(path, opt = nil) ⇒ Object

Like Templater.new, but instead of a template string, the path to the file containing the template. Sets :filename.



105
106
107
# File 'lib/templater.rb', line 105

def self.file(path, opt=nil)
	new(File.read, (opt || {}).merge(:filename => path))
end

Instance Method Details

#inspectObject

 :nodoc:



137
138
139
140
141
142
143
# File 'lib/templater.rb', line 137

def inspect # :nodoc:
	"#<%s:0x%x string=%s>" %  [
		self.class,
		object_id << 1,
		@string.inspect
	]
end

#result(delegator = nil, variables = {}, &on_error) ⇒ Object

See Templater::Variables.new Returns the evaluated template. Default &on_error is the Templater::Raiser proc.



128
129
130
131
132
133
134
135
# File 'lib/templater.rb', line 128

def result(delegator=nil, variables={}, &on_error)
	variables ||= {}
	on_error  ||= Raiser
	variables = Variables.new(delegator, variables, &on_error) unless variables.kind_of?(Variables)
	@erb.result(Binder.bind(variables).call)
rescue NameError => e
	raise NameError, e.message+" for #{self.inspect} with #{variables.inspect}", e.backtrace
end