mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
In-file-templates are automaticly loaded for you.
Sinatra will now load you in-file-templates from the file that required sinatra. If you need to use in-file-templates outside this you will need to explicitly call use_in_file_templates! in that file.
This commit is contained in:
parent
15863661c3
commit
eec7d21416
3 changed files with 18 additions and 4 deletions
10
README.rdoc
10
README.rdoc
|
@ -165,12 +165,13 @@ other templates.
|
|||
|
||||
Templates may be defined at the end of the source file:
|
||||
|
||||
require 'rubygems'
|
||||
require 'sinatra'
|
||||
|
||||
get '/' do
|
||||
haml :index
|
||||
end
|
||||
|
||||
use_in_file_templates!
|
||||
|
||||
__END__
|
||||
|
||||
@@ layout
|
||||
|
@ -180,6 +181,11 @@ Templates may be defined at the end of the source file:
|
|||
@@ index
|
||||
%div.title Hello world!!!!!
|
||||
|
||||
NOTE: Sinatra will automaticly load any in-file-templates in the
|
||||
source file that first required sinatra. If you have in-file-templates
|
||||
in another source file you will need to explicitly call
|
||||
+use_in_file_templates! on main in that file.
|
||||
|
||||
It's also possible to define named templates using the top-level template
|
||||
method:
|
||||
|
||||
|
|
|
@ -2,3 +2,5 @@ $LOAD_PATH.unshift File.dirname(__FILE__)
|
|||
require 'sinatra/base'
|
||||
require 'sinatra/main'
|
||||
require 'sinatra/compat'
|
||||
|
||||
use_in_file_templates!
|
||||
|
|
|
@ -493,8 +493,13 @@ module Sinatra
|
|||
end
|
||||
|
||||
def use_in_file_templates!
|
||||
line = caller.detect { |s| s !~ /lib\/sinatra.*\.rb/ &&
|
||||
s !~ /\(.*\)/ }
|
||||
line = caller.detect do |s|
|
||||
[
|
||||
/lib\/sinatra.*\.rb/,
|
||||
/\(.*\)/,
|
||||
/rubygems\/custom_require\.rb/
|
||||
].all? { |x| s !~ x }
|
||||
end
|
||||
file = line.sub(/:\d+.*$/, '')
|
||||
if data = ::IO.read(file).split('__END__')[1]
|
||||
data.gsub!(/\r\n/, "\n")
|
||||
|
@ -791,6 +796,7 @@ module Sinatra
|
|||
end
|
||||
|
||||
def self.call(env)
|
||||
$LOADED_FEATURES.delete("sinatra.rb")
|
||||
reload! if reload?
|
||||
super
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue