Skip to content

Commit

Permalink
inline templates!
Browse files Browse the repository at this point in the history
  • Loading branch information
bmizerany committed Mar 27, 2008
1 parent 6bda16b commit d874133
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions lib/sinatra.rb
Expand Up @@ -412,24 +412,28 @@ def resolve_template(renderer, template, options, scream = true)
template.call
when Symbol
if proc = templates[template]
proc.call
resolve_template(renderer, proc, options, scream)
else
path = File.join(
options[:views_directory] || Sinatra.application.options.views,
"#{template}.#{renderer}"
)
unless File.exists?(path)
raise Errno::ENOENT.new(path) if scream
nil
else
File.read(path)
end
read_template_file(renderer, template, options, scream)
end
else
nil
end
end

def read_template_file(renderer, template, options, scream = true)
path = File.join(
options[:views_directory] || Sinatra.application.options.views,
"#{template}.#{renderer}"
)
unless File.exists?(path)
raise Errno::ENOENT.new(path) if scream
nil
else
File.read(path)
end
end

def templates
Sinatra.application.templates
end
Expand Down Expand Up @@ -887,6 +891,21 @@ def template(name, &b)
Sinatra.application.define_template(name, &b)
end

def use_in_file_templates!
require 'stringio'
templates = IO.read(caller.first.split(':').first).split('__FILE__').last
data = StringIO.new(templates)
current_template = nil
data.each do |line|
if line =~ /^##\s?(.*)/
current_template = $1.to_sym
Sinatra.application.templates[current_template] = ''
elsif current_template
Sinatra.application.templates[current_template] << line
end
end
end

def configures(*envs, &b)
yield if !Sinatra.application.reloading &&
(envs.include?(Sinatra.application.options.env) ||
Expand Down

0 comments on commit d874133

Please sign in to comment.