module Sinatra::Partial::Helpers
Public Instance Methods
partial(partial_name, options={})
click to toggle source
Renders a partial to a string.
@param [#to_s] partial_name The partial to render. @param [Hash] options The options to render the partial with. @option options [Hash] :locals Local variables to render with @option options [Array] :collection Renders the template once per object in this array. @option options [Symbol] :template_engine The template engine to use. Haml by default. @option options [true,false] :underscores Set to true if you wish to follow the Rails convention of partial files having a leading underscore.
@return [String] The rendered template contents.
@example simply render a partial
partial(:meta, :locals => {meta: meta}) # => renders views/_meta.haml
@example render a partial in a subfolder
partial("meta/news", :locals => {news: [<News>]}) # => renders views/meta/_news.haml
@example render a collection of objects with one partial
partial(:"meta/news", :collection => [<News>]) # => renders views/meta/_news.haml once per item in :collection, with the local variable `news` being the current item in the iteration
# File lib/sinatra/partial.rb, line 57 def partial(partial_name, options={}) options.merge! :layout => false partial_location = partial_name.to_s engine = options.fetch(:template_engine, settings.partial_template_engine) underscores = options.fetch(:underscores, settings.partial_underscores) template = Private.partial_expand_path(partial_location, underscores) if collection = options.delete(:collection) member_local = Private.partial_local(partial_location) locals = options.fetch(:locals, {}) collection.inject([]) do |buffer, member| new_locals = {member_local => member}.merge(locals) buffer << self.method(engine).call(template, options.merge(:locals => new_locals)) end.join("\n") else # TODO benchmark this and see if caching the method # speeds things up self.method(engine).call(template, options) end end