module Roda::RodaPlugins::RenderEach::InstanceMethods
Public Instance Methods
Source
# File lib/roda/plugins/render_each.rb, line 109 def render_each(enum, template, opts=(no_opts = true; optimized_template = _cached_render_each_template_method(template); OPTS), &block) if optimized_template return _optimized_render_each(enum, optimized_template, render_each_default_local(template), {}, &block) elsif opts.has_key?(:local) as = opts[:local] else 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, &block) end end if as opts = opts.dup if locals opts[:locals] = locals else locals = opts[:locals] = if locals = opts[:locals] Hash[locals] else {} end locals[as] = nil end if (opts.keys - ALLOWED_KEYS).empty? && (optimized_template = _optimized_render_method_for_locals(template, locals)) return _optimized_render_each(enum, optimized_template, as, locals, &block) end end if defined?(yield) enum.each do |v| locals[as] = v if as yield render_template(template, opts) end nil else enum.map do |v| locals[as] = v if as render_template(template, opts) end.join end end
For each value in enum, render the given template using the given opts. The template and options hash are passed to render
. Additional options supported:
- :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.
Private Instance Methods
Source
# File lib/roda/plugins/render_each.rb, line 187 def _cached_render_each_template_method(template) case template when String, Symbol if (method_cache = render_opts[:template_method_cache]) key = render_opts[:assume_fixed_locals] ? template : [:_render_locals, template, [template.to_sym]] _cached_template_method_lookup(method_cache, key) end else false end end
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 been explicitly disabled. Otherwise return nil.
Source
# File lib/roda/plugins/render_each.rb, line 200 def _optimized_render_each(enum, optimized_template, as, locals) if defined?(yield) enum.each do |v| locals[as] = v yield _call_optimized_template_method(optimized_template, locals) end nil else enum.map do |v| locals[as] = v _call_optimized_template_method(optimized_template, locals) end.join end end
Use an optimized render for each value in the enum.
Source
# File lib/roda/plugins/render_each.rb, line 158 def render_each_default_local(template) # Optimize to avoid allocations when possible template = case template when Symbol s = template.name return template unless s.include?("/") || s.include?(".") s when String return template.to_sym unless template.include?("/") || template.include?(".") template else template.to_s end File.basename(template).sub(/\..+\z/, '').to_sym end
The default local variable name to use for the template, if the :local option is not used when calling render_each.