sinatra 博客分类: C++读书笔记 Sinatra
程序员文章站
2024-02-14 11:51:52
...
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
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
推荐阅读
-
sinatra 博客分类: C++读书笔记 Sinatra
-
ruby sinatra 内部机制(二) 博客分类: ruby rubysinatra
-
ruby sinatra 内部机制(一) 博客分类: ruby sinatra
-
使用eclipse进行c++的开发 博客分类: C++ eclipsec++
-
使用eclipse进行c++的开发 博客分类: C++ eclipsec++
-
《深入分析Java Web技术内幕》读书笔记 博客分类: 技术总结研究报告 《深入分析Java Web技术内幕》读书笔记
-
C++【变量说明】 博客分类: C program
-
C++【函数模板】 博客分类: C program
-
C++【函数模板】 博客分类: C program
-
c++引用计数的本质 博客分类: c/c++