Method: Inversion::Template.load

Defined in:
lib/inversion/template.rb

.load(path, parsestate = nil, opts = {}) ⇒ Object

Read a template object from the specified path.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/inversion/template.rb', line 169

def self::load( path, parsestate=nil, opts={} )

	# Shift the options hash over if there isn't a parse state
	if parsestate.is_a?( Hash )
		opts = parsestate
		parsestate = nil
	end

	tmpl = nil
	path = Pathname( path )
	opts[:template_paths] ||= self.template_paths
	search_path = opts[:template_paths] + [ Dir.pwd ]
	self.log.debug "Searching template paths: %p" % [ search_path ]

	# Unrestricted template location.
	if path.absolute?
		tmpl = path

	# Template files searched under paths specified in 'template_paths', then
	# the current working directory. First match wins.
	else
		tmpl = search_path.collect {|dir| Pathname(dir) + path }.find do |fullpath|
			fullpath.exist?
		end

		raise RuntimeError, "Unable to find template %p within configured paths %p" %
			[ path.to_s, search_path ] if tmpl.nil?
	end

	# We trust files read from disk
	source = if opts.key?( :encoding )
			tmpl.read( encoding: opts[:encoding] )
		else
			tmpl.read
		end

	# Load the instance and set the path to the source
	template = self.new( source, parsestate, opts )
	template.source_file = tmpl

	return template
end