Created
February 17, 2010 09:50
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra/base' | |
require 'my_app' | |
run MyApp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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