Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit eec7d2141685b8a44fc202a58f384bfad6c3d435 1 parent 1586366
@bmizerany bmizerany authored
Showing with 18 additions and 4 deletions.
  1. +8 −2 README.rdoc
  2. +2 −0  lib/sinatra.rb
  3. +8 −2 lib/sinatra/base.rb
View
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:
View
2  lib/sinatra.rb
@@ -2,3 +2,5 @@
require 'sinatra/base'
require 'sinatra/main'
require 'sinatra/compat'
+
+use_in_file_templates!
View
10 lib/sinatra/base.rb
@@ -493,8 +493,13 @@ def layout(name=:layout, &block)
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 @@ def self.configure(*envs)
end
def self.call(env)
+ $LOADED_FEATURES.delete("sinatra.rb")
reload! if reload?
super
end
Please sign in to comment.
Something went wrong with that request. Please try again.