Skip to content

Instantly share code, notes, and snippets.

@cypher
Created February 17, 2010 09:50
Show Gist options
  • Save cypher/306473 to your computer and use it in GitHub Desktop.
Save cypher/306473 to your computer and use it in GitHub Desktop.
Sample sinatra app that uses lenary's partial helper (http://gist.github.com/119874). Tested using Sinatra 0.9.4
require 'sinatra/base'
require 'my_app'
run MyApp
class MyApp < Sinatra::Base
use_in_file_templates!
helpers do
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
get '/' do
erb :index
end
end
__END__
@@ layout
<html>
<body>
<%= yield %>
</body>
</html>
@@ index
<h1>Index</h1>
<%= partial :my_partial %>
@@ /_my_partial
<h4>inside the partial</h4>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment