阅读更多
request.path_info
request.url
env["HTTP_REFERER"]
Sinatra#register
extension.registered(self) if extension.respond_to?(:registered)
Views / Templates
Templates are assumed to be located directly under the ./views directory. To use a different views directory:
set :views, File.dirname(__FILE__) + '/templates'
One important thing to remember is that you always have to reference templates with symbols, even if they’re in a subdirectory (in this case, use :'subdir/template'). You must use a symbol because otherwise rendering methods will render any strings passed to them directly.
Accessing Variables in Templates
Templates are evaluated within the same context as route handlers. Instance variables set in route handlers are directly accessible by templates:
get '/:id' do
@foo = Foo.find(params[:id])
haml '%h1= @foo.name'
end
Or, specify an explicit Hash of local variables:
get '/:id' do
foo = Foo.find(params[:id])
haml '%h1= bar.name', :locals => { :bar => foo }
end
This is typically used when rendering templates as partials from within other templates.
731 # Returns pass block.
732 def process_route(pattern, keys, conditions)
733 @original_params ||= @params
734 route = @request.route
735 route = '/' if route.empty? and not settings.empty_path_info?
736 if match = pattern.match(route)
737 values = match.captures.to_a
738 params =
739 if keys.any?
740 keys.zip(values).inject({}) do |hash,(k,v)|
741 if k == 'splat'
742 (hash[k] ||= []) << v
743 else
744 hash[k] = v
745 end
746 hash
747 end
748 elsif values.any?
749 {'captures' => values}
750 else
751 {}
752 end
753 @params = @original_params.merge(params)
754 @block_params = values
755 catch(:pass) do
756 conditions.each { |cond|
757 throw :pass if instance_eval(&cond) == false }
758 yield
759 end
760 end
761 ensure
762 @params = @original_params
763 end