Skip to content

Instantly share code, notes, and snippets.

@sax
Forked from lenary/partials.rb
Created April 6, 2010 16:38

Revisions

  1. sax revised this gist Apr 6, 2010. 1 changed file with 20 additions and 13 deletions.
    33 changes: 20 additions & 13 deletions partials.rb
    Original file line number Diff line number Diff line change
    @@ -2,19 +2,26 @@
    # 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)
    if collection = options.delete(:collection) then
    collection.inject([]) do |buffer, member|
    buffer << erb(:"#{template}", options.merge(:layout =>
    false, :locals => {template_array[-1].to_sym => member}))
    end.join("\n")
    else
    erb(:"#{template}", options)

    require 'sinatra/base'

    module Sinatra
    module 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)
    if collection = options.delete(:collection) then
    collection.inject([]) do |buffer, member|
    buffer << erb(:"#{template}", options.merge(:layout =>
    false, :locals => {template_array[-1].to_sym => member}))
    end.join("\n")
    else
    erb(:"#{template}", options)
    end
    end
    end

    helpers Partials
    end
  2. @lenary lenary revised this gist Jun 2, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion partials.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # 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 14 and line 18 with `haml` or whatever
    # 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('/')
  3. @lenary lenary revised this gist Jun 2, 2009. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions partials.rb
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,19 @@
    # 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 14 and line 18 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)
    if collection = options.delete(:collection) then
    collection.inject([]) do |buffer, member|
    buffer << erb(template, options.merge(:layout =>
    false, :locals => {template.to_sym => member}))
    buffer << erb(:"#{template}", options.merge(:layout =>
    false, :locals => {template_array[-1].to_sym => member}))
    end.join("\n")
    else
    template_array = template.to_s.split('/')
    template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
    erb(:"#{template}", options)
    end
    end
  4. @lenary lenary revised this gist May 29, 2009. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion partials.rb
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,8 @@ def partial(template, *args)
    false, :locals => {template.to_sym => member}))
    end.join("\n")
    else
    template = template.to_s.split('/')[0..-2].join('/') + "/_#{template.to_s.split('/')[-1]}"
    template_array = template.to_s.split('/')
    template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
    erb(:"#{template}", options)
    end
    end
  5. @lenary lenary revised this gist May 29, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion partials.rb
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ def partial(template, *args)
    false, :locals => {template.to_sym => member}))
    end.join("\n")
    else
    template = template.split('/')[0..-2].join('/') + "/_#{template.split('/')[-1]}"
    template = template.to_s.split('/')[0..-2].join('/') + "/_#{template.to_s.split('/')[-1]}"
    erb(:"#{template}", options)
    end
    end
  6. @lenary lenary revised this gist May 29, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion partials.rb
    Original file line number Diff line number Diff line change
    @@ -13,4 +13,4 @@ def partial(template, *args)
    erb(:"#{template}", options)
    end
    end
    en
    end
  7. @lenary lenary revised this gist May 29, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion partials.rb
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ def partial(template, *args)
    false, :locals => {template.to_sym => member}))
    end.join("\n")
    else
    template = template.split('/')[0..-2] + "_#{template.split('/')[-1]}"
    template = template.split('/')[0..-2].join('/') + "/_#{template.split('/')[-1]}"
    erb(:"#{template}", options)
    end
    end
  8. @lenary lenary created this gist May 29, 2009.
    16 changes: 16 additions & 0 deletions partials.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    # stolen from http://github.com/cschneid/irclogger/blob/master/lib/partials.rb
    module Sinatra::Partials
    def partial(template, *args)
    options = args.last.is_a?(Hash) ? args.pop : {}
    options.merge!(:layout => false)
    if collection = options.delete(:collection) then
    collection.inject([]) do |buffer, member|
    buffer << erb(template, options.merge(:layout =>
    false, :locals => {template.to_sym => member}))
    end.join("\n")
    else
    template = template.split('/')[0..-2] + "_#{template.split('/')[-1]}"
    erb(:"#{template}", options)
    end
    end
    en