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:
Blake Mizerany 2009-01-16 17:01:41 -08:00
parent 15863661c3
commit eec7d21416
3 changed files with 18 additions and 4 deletions

View File

@ -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:

View File

@ -2,3 +2,5 @@ $LOAD_PATH.unshift File.dirname(__FILE__)
require 'sinatra/base'
require 'sinatra/main'
require 'sinatra/compat'
use_in_file_templates!

View File

@ -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