Sha256: 1067e76bdd94fba701896d8a7e1a41e173e91d58d85821ff453dc49f2b27f5d9

Contents?: true

Size: 1.07 KB

Versions: 42

Compression:

Stored size: 1.07 KB

Contents

# stolen from http://github.com/cschneid/irclogger/blob/master/lib/partials.rb
#   and made a lot more robust by me
# this implementation uses erb by default. if you want to use any other template mechanism
#   then replace `erb` on line 13 and line 17 with `haml` or whatever 
module Sinatra::Partials
  def partial(template, *args)
    template_array = template.to_s.split('/')
    template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
    options = args.last.is_a?(Hash) ? args.pop : {}
    options.merge!(:layout => false)
    locals = options[:locals] || {}
    if collection = options.delete(:collection)
      collection.inject([]) do |buffer, member|
        buffer << erb(:"#{template}", options.merge(:layout =>
        false, :locals => {template_array[-1].to_sym => member}.merge(locals)))
      end.join("\n")
    elsif partial_object = options.delete(:object)
      erb(:"#{template}", options.merge(:layout =>
        false, :locals => {template_array[-1].to_sym => partial_object}).merge(locals))
    else
      erb(:"#{template}", options)
    end
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
mongodb_logger-0.6.5 lib/mongodb_logger/server/helpers/partials.rb
mongodb_logger-0.6.4 lib/mongodb_logger/server/helpers/partials.rb
mongodb_logger-0.6.3 lib/mongodb_logger/server/helpers/partials.rb
mongodb_logger-0.6.2 lib/mongodb_logger/server/helpers/partials.rb
mongodb_logger-0.6.1 lib/mongodb_logger/server/helpers/partials.rb
mongodb_logger-0.6.0 lib/mongodb_logger/server/helpers/partials.rb
mongodb_logger-0.5.2 lib/mongodb_logger/server/helpers/partials.rb
mongodb_logger-0.5.1 lib/mongodb_logger/server/helpers/partials.rb
mongodb_logger-0.5.0 lib/mongodb_logger/server/helpers/partials.rb
mongodb_logger-0.4.2 lib/mongodb_logger/server/partials.rb
mongodb_logger-0.4.1 lib/mongodb_logger/server/partials.rb
mongodb_logger-0.4.0 lib/mongodb_logger/server/partials.rb
mongodb_logger-0.3.3-jruby lib/mongodb_logger/server/partials.rb
mongodb_logger-0.3.3 lib/mongodb_logger/server/partials.rb
mongodb_logger-0.3.2-jruby lib/mongodb_logger/server/partials.rb
mongodb_logger-0.3.2 lib/mongodb_logger/server/partials.rb
mongodb_logger-0.3.1-jruby lib/mongodb_logger/server/partials.rb
mongodb_logger-0.3.1 lib/mongodb_logger/server/partials.rb
mongodb_logger-0.3.0-jruby lib/mongodb_logger/server/partials.rb
mongodb_logger-0.3.0 lib/mongodb_logger/server/partials.rb