From 7d5bc1f0da7f87120a2b4ae1920ec7fc5c4455f4 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Wed, 27 Feb 2008 21:07:06 -0800 Subject: [PATCH] Haml & Erb redo --- lib/sinatra.rb | 103 +++++++++++++---------------- test/erb_test.rb | 34 +++++++++- test/haml_test.rb | 44 +++++++++++- test/views/foo.haml | 1 + test/views/foo.html | 1 - test/views/foo_layout.erb | 2 +- test/views/foo_layout.haml | 2 +- test/views/layout_test/foo.haml | 1 + test/views/layout_test/layout.erb | 2 +- test/views/layout_test/layout.haml | 1 + 10 files changed, 125 insertions(+), 66 deletions(-) create mode 100644 test/views/foo.haml delete mode 100644 test/views/foo.html create mode 100644 test/views/layout_test/foo.haml create mode 100644 test/views/layout_test/layout.haml diff --git a/lib/sinatra.rb b/lib/sinatra.rb index 073a38cd..2f809f6b 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -305,54 +305,45 @@ module Sinatra module RenderingHelpers - def render(content, options={}) - options[:layout] ||= :layout - @content = evaluate_renderer(content, options) - layout = resolve_layout(options[:layout], options) - @content = evaluate_renderer(layout, options) if layout - @content + def render(renderer, template, options={}) + m = method("render_#{renderer}") + result = m.call(resolve_template(renderer, template, options)) + if layout = determine_layout(renderer, template, options) + result = m.call(resolve_template(renderer, layout, options)) { result } + end + result end - def partial(content, options={}) - renderer(content, options.merge(:layout => nil)) + def determine_layout(renderer, template, options) + layout_from_options = options[:layout] || :layout + layout = layouts[layout_from_options] + layout ||= resolve_template(renderer, layout_from_options, options) + layout end - - private - - def evaluate_renderer(content, options={}) - renderer = "evaluate_#{options[:renderer]}" - result = case content - when nil - '' - when String - content - when Proc - content.call - when File - content.read - when Symbol - evaluate_renderer( - File.new(path_to(content, options)), - options - ) - end - send(renderer, result) - end - - def resolve_layout(name, options={}) - return if name == false - if layout = layouts[name || :layout] - return layout - end - if File.file?(filename = path_to(name, options)) - File.new(filename) - end - end - - def path_to(name, options={}) - (options[:views_directory] || 'views') + "/#{name}.#{options[:ext]}" - end + private + + def resolve_template(renderer, template, options) + case template + when String + template + when Proc + template.call + when Symbol + template_file(renderer, template, options) + else + nil + end + end + + def template_file(renderer, name, options={}) + path = File.join( + options[:views_directory] || Sinatra.application.options.public, + "#{name}.#{renderer}" + ) + File.exists?(path) ? File.read(path) : nil + end + def layouts Sinatra.application.layouts end @@ -362,31 +353,31 @@ module Sinatra module Erb def erb(content, options={}) - render(content, options.merge(:renderer => :erb, :ext => :erb)) + require 'erb' + render(:erb, content, options) end - private - - def evaluate_erb(content) - require 'erb' + private + + def render_erb(content) ::ERB.new(content).result(binding) end - + end module Haml def haml(content, options={}) - render(content, options.merge(:renderer => :haml, :ext => :haml)) + require 'haml' + render(:haml, content, options) end private - - def evaluate_haml(content) - require 'haml' - ::Haml::Engine.new(content).render(self) - end + def render_haml(content, &b) + ::Haml::Engine.new(content).render(self, &b) + end + end class EventContext diff --git a/test/erb_test.rb b/test/erb_test.rb index 17b6fa57..81e79eba 100644 --- a/test/erb_test.rb +++ b/test/erb_test.rb @@ -34,7 +34,7 @@ context "Erb" do specify "can be inline" do layout do - %Q{This is <%= @content %>!} + %Q{This is <%= yield %>!} end get '/lay' do @@ -50,7 +50,7 @@ context "Erb" do specify "can use named layouts" do layout :pretty do - %Q{

<%= @content %>

} + %Q{

<%= yield %>

} end get '/pretty' do @@ -83,6 +83,34 @@ context "Erb" do end end - + + context "Templates (in general)" do + + specify "are read from files if Symbols" do + + get '/from_file' do + @name = 'Alena' + erb :foo, :views_directory => File.dirname(__FILE__) + "/views" + end + + get_it '/from_file' + + body.should.equal 'You rock Alena!' + + end + + specify "use layout.ext by default if available" do + + get '/layout_from_file' do + erb :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test" + end + + get_it '/layout_from_file' + should.be.ok + body.should.equal "x This is foo! x \n" + + end + + end end diff --git a/test/haml_test.rb b/test/haml_test.rb index 5b85a7f6..8aea5fb6 100644 --- a/test/haml_test.rb +++ b/test/haml_test.rb @@ -34,7 +34,7 @@ context "Haml" do specify "can be inline" do layout do - '== This is #{@content}!' + '== This is #{yield}!' end get '/lay' do @@ -50,7 +50,7 @@ context "Haml" do specify "can use named layouts" do layout :pretty do - "%h1== \#{@content}" + '%h1== #{yield}' end get '/pretty' do @@ -81,8 +81,46 @@ context "Haml" do body.should.equal "Welcome to the Hello Program\nHi Blake\n" end + + specify "can be read from file and layout from text" do + get '/foo' do + haml 'Test', :layout => '== Foo #{yield}' + end + + get_it '/foo' + + body.should.equal "Foo Test\n" + end end - + context "Templates (in general)" do + + specify "are read from files if Symbols" do + + get '/from_file' do + @name = 'Alena' + haml :foo, :views_directory => File.dirname(__FILE__) + "/views" + end + + get_it '/from_file' + + body.should.equal "You rock Alena!\n" + + end + + specify "use layout.ext by default if available" do + + get '/layout_from_file' do + haml :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test" + end + + get_it '/layout_from_file' + should.be.ok + body.should.equal "x This is foo!\n x\n" + + end + + end + end diff --git a/test/views/foo.haml b/test/views/foo.haml new file mode 100644 index 00000000..0fcc4f40 --- /dev/null +++ b/test/views/foo.haml @@ -0,0 +1 @@ +== You rock #{@name}! \ No newline at end of file diff --git a/test/views/foo.html b/test/views/foo.html deleted file mode 100644 index 8a4ddee5..00000000 --- a/test/views/foo.html +++ /dev/null @@ -1 +0,0 @@ -You rock #{@name}! \ No newline at end of file diff --git a/test/views/foo_layout.erb b/test/views/foo_layout.erb index 086c8f88..6e39e8c7 100644 --- a/test/views/foo_layout.erb +++ b/test/views/foo_layout.erb @@ -1,2 +1,2 @@ <%= @title %> -Hi <%= @content %> +Hi <%= yield %> diff --git a/test/views/foo_layout.haml b/test/views/foo_layout.haml index 024e8e67..5c041fb0 100644 --- a/test/views/foo_layout.haml +++ b/test/views/foo_layout.haml @@ -1,2 +1,2 @@ == #{@title} -== Hi #{@content} +== Hi #{yield} diff --git a/test/views/layout_test/foo.haml b/test/views/layout_test/foo.haml new file mode 100644 index 00000000..10872edd --- /dev/null +++ b/test/views/layout_test/foo.haml @@ -0,0 +1 @@ +This is foo! \ No newline at end of file diff --git a/test/views/layout_test/layout.erb b/test/views/layout_test/layout.erb index b6057648..7fec122a 100644 --- a/test/views/layout_test/layout.erb +++ b/test/views/layout_test/layout.erb @@ -1 +1 @@ -x <%= @content %> x +x <%= yield %> x diff --git a/test/views/layout_test/layout.haml b/test/views/layout_test/layout.haml new file mode 100644 index 00000000..75efae53 --- /dev/null +++ b/test/views/layout_test/layout.haml @@ -0,0 +1 @@ +== x #{yield} x