lib/malt/engines/erb.rb in malt-0.3.0 vs lib/malt/engines/erb.rb in malt-0.4.0
- old
+ new
@@ -13,74 +13,99 @@
default :erb, :rhtml
# Render ERB template.
#
- # The +params+ can be:
+ # @param [Hash] params
#
- # * :text - text of erb document
- # * :file - file name where text was read (or nil)
- # * :data - data source for template interpolation
- # * :safe -
- # * :trim -
+ # @option params [String] :text
+ # Text of document.
#
- # Returns a String.
- def render(params={}, &yld)
- text = params[:text]
- file = params[:file]
- data = params[:data]
+ # @option params [String] :file
+ # The file name where text was read (or nil).
+ #
+ # @option params [Object,Binding] :scope
+ # Scope from within which to render, serves as data source for
+ # template interpolation.
+ #
+ # @option params [Object,Binding] :locals
+ # Direct key/value data for template interpolation.
+ #
+ # @option params [Boolean] :safe
+ # Run in separate thread.
+ #
+ # @option params [String] :trim
+ # Trim mode, can be either of the following:
+ # a) `%` enables Ruby code processing for lines beginning with `%`.
+ # b) `<>` omit newline for lines starting with `<%` and ending in `%>`.
+ # c) `>` omit newline for lines ending in `%>`.
+ #
+ # @option params [Boolean] :precompile (true)
+ # Precompile the ERB template. Default is `true`.
+ # Note that `yield` currently does work with non-compiled tempaltes.
+ #
+ # @return [String] Rendered output.
+ #
+ def render(params={}, &content)
+ text, file, scope, locals = parameters(params, :text, :file, :scope, :locals)
- data = make_binding(data, &yld)
-
- if settings[:precompile] == false
- intermediate(params).result(data)
- else
- ruby = compile(params)
- eval(ruby, data, file)
- end
+ #if settings[:compile] == true
+ #else
+ bind = make_binding(scope, locals, &content)
+ prepare_engine(params).result(bind)
+ #end
end
- # Compile ERB template into Ruby source code.
- def compile(params={})
- file = params[:file]
- if cache?
- @source[file] ||= intermediate(params).src
- else
- intermediate(params).src
- end
- end
-
# Returns instance of underlying ::ERB class.
- def intermediate(params={})
- text = params[:text]
- file = params[:file]
+ #def prepare_engine(params={})
+ # create_engine(params)
+ #end
+ #
+ def create_engine(params={})
+ text = parameters(params, :text)
+
opts = engine_options(params)
safe = opts[:safe]
trim = opts[:trim]
- if cache?
- @cache[file] ||= ::ERB.new(text, safe, trim)
- else
+ cached(text,safe,trim) do
::ERB.new(text, safe, trim)
end
end
- private
+ private
# Load ERB library if not already loaded.
- def initialize_engine
+ def require_engine
return if defined? ::ERB
require_library('erb')
end
- def engine_options(params)
- opts = {}
- opts[:safe] = params[:safe] || settings[:safe]
- opts[:trim] = params[:trim] || settings[:trim]
- opts
+ #
+ def engine_option_names
+ [:safe, :trim]
end
+ #def engine_options(params)
+ # opts = {}
+ # opts[:safe] = params[:safe] || settings[:safe]
+ # opts[:trim] = params[:trim] || settings[:trim]
+ # opts
+ #end
+
+ # Compile ERB template into Ruby source code.
+ #
+ # @return [String] Ruby source code.
+ #def compile(params={})
+ # if cache?
+ # @source[params] ||= (
+ # intermediate(params).src
+ # )
+ # else
+ # intermediate(params).src
+ # end
+ #end
+
end
end
-