Sha256: fe9edeaad3d73c80efd6b52aa55e67b2c1d54557c7ac01526663352b8baa922b

Contents?: true

Size: 599 Bytes

Versions: 7

Compression:

Stored size: 599 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
		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.7 lib/tagen/erb.rb
tagen-1.1.6 lib/tagen/erb.rb
tagen-1.1.5 lib/tagen/erb.rb
tagen-1.1.4 lib/tagen/erb.rb
tagen-1.1.3 lib/tagen/erb.rb
tagen-1.1.2 lib/tagen/erb.rb
tagen-1.1.1 lib/tagen/erb.rb