lib/roda/plugins/render_each.rb in roda-3.31.0 vs lib/roda/plugins/render_each.rb in roda-3.32.0
- old
+ new
@@ -24,11 +24,13 @@
#
# render_each([1,2,3], :foo, local: :bar)
#
# Will render the +foo+ template, but the local variable used inside
# the template will be +bar+. You can use <tt>local: nil</tt> to
- # not set a local variable inside the template.
+ # not set a local variable inside the template. By default, the
+ # local variable name is based on the template name, with any
+ # directories and file extensions removed.
module RenderEach
# Load the render plugin before this plugin, since this plugin
# calls the render method.
def self.load_dependencies(app)
app.plugin :render
@@ -43,15 +45,15 @@
# :local :: The local variable to use for the current enum value
# inside the template. An explicit +nil+ value does not
# set a local variable. If not set, uses the template name.
def render_each(enum, template, opts=(no_opts = true; optimized_template = _cached_render_each_template_method(template); OPTS))
if optimized_template
- return _optimized_render_each(enum, optimized_template, template.to_s.to_sym, {})
+ return _optimized_render_each(enum, optimized_template, render_each_default_local(template), {})
elsif opts.has_key?(:local)
as = opts[:local]
else
- as = template.to_s.to_sym
+ as = render_each_default_local(template)
if no_opts && optimized_template.nil? && (optimized_template = _optimized_render_method_for_locals(template, (locals = {as=>nil})))
return _optimized_render_each(enum, optimized_template, as, locals)
end
end
@@ -74,9 +76,15 @@
render_template(template, opts)
end.join
end
private
+
+ # The default local variable name to use for the template, if the :local option
+ # is not used when calling render_each.
+ def render_each_default_local(template)
+ File.basename(template.to_s).sub(/\..+\z/, '').to_sym
+ end
if Render::COMPILED_METHOD_SUPPORT
# If compiled method support is enabled in the render plugin, return the
# method name to call to render the template. Return false if not given
# a string or symbol, or if compiled method support for this template has