From d874133b94c7b3bc94be806f5ff8c204631f4106 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Wed, 26 Mar 2008 23:45:32 -0700 Subject: [PATCH] inline templates! --- lib/sinatra.rb | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/sinatra.rb b/lib/sinatra.rb index 9789ad5b84..0e4902ac79 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -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 @@ -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) ||