Sha256: 1afd3cf85b959271ef06b1243000fdc132be539a20cdfb53b5bac9f9d699aee4

Contents?: true

Size: 659 Bytes

Versions: 3

Compression:

Stored size: 659 Bytes

Contents

require "erb"

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

	alias result_without_tagen result
  alias result result_with_tagen

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

3 entries across 3 versions & 1 rubygems

Version Path
tagen-2.0.2 lib/tagen/erb.rb
tagen-2.0.1 lib/tagen/erb.rb
tagen-2.0.0 lib/tagen/erb.rb