Sha256: fc9f4cd809cdb81a828304c31961b4435aa4e67c8d38373b1be822c5f37c5e13

Contents?: true

Size: 665 Bytes

Versions: 7

Compression:

Stored size: 665 Bytes

Contents

require 'erb'

class ERB
	alias original_result result

	# add locals support
	#
	# @example
	#  erb = Erb.new("<%=a%>")
	#  erb.result(nil, a: 1) #=> "1"
	#
	# @param [Hash,OpenOption] locals
	def result bind=nil, locals={}
		bind ||= TOPLEVEL_BINDING
		if locals.empty?
			original_result bind
		else
			result_with_locals bind, locals
		end
	end

	private
	def result_with_locals bind, locals
		locals = locals.class.to_s=="OpenOption" ? locals._data : locals

		@locals = locals
		evalstr = <<-EOF
def run_erb
	#{locals.map{|k,v| %~#{k} = @locals[ #{Symbol===k ? ':' : ''}'#{k}' ]~}.join(';')}
	#{self.src}
	_erbout
end
		EOF
		eval evalstr
		run_erb
	end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
tagen-1.1.0 lib/tagen/erb.rb
tagen-1.0.4 lib/tagen/erb.rb
tagen-1.0.3 lib/tagen/erb.rb
tagen-1.0.2 lib/tagen/erb.rb
tagen-1.0.1 lib/tagen/erb.rb
tagen-1.0.0 lib/tagen/erb.rb
tagen-0.2.5 lib/tagen/erb.rb