1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Haml & Erb redo

This commit is contained in:
Blake Mizerany 2008-02-27 21:07:06 -08:00
parent 59199dcc13
commit 7d5bc1f0da
10 changed files with 125 additions and 66 deletions

View file

@ -305,52 +305,43 @@ module Sinatra
module RenderingHelpers module RenderingHelpers
def render(content, options={}) def render(renderer, template, options={})
options[:layout] ||= :layout m = method("render_#{renderer}")
@content = evaluate_renderer(content, options) result = m.call(resolve_template(renderer, template, options))
layout = resolve_layout(options[:layout], options) if layout = determine_layout(renderer, template, options)
@content = evaluate_renderer(layout, options) if layout result = m.call(resolve_template(renderer, layout, options)) { result }
@content end
result
end end
def partial(content, options={}) def determine_layout(renderer, template, options)
renderer(content, options.merge(:layout => nil)) layout_from_options = options[:layout] || :layout
layout = layouts[layout_from_options]
layout ||= resolve_template(renderer, layout_from_options, options)
layout
end end
private private
def evaluate_renderer(content, options={}) def resolve_template(renderer, template, options)
renderer = "evaluate_#{options[:renderer]}" case template
result = case content
when nil
''
when String when String
content template
when Proc when Proc
content.call template.call
when File
content.read
when Symbol when Symbol
evaluate_renderer( template_file(renderer, template, options)
File.new(path_to(content, options)), else
options nil
end
end
def template_file(renderer, name, options={})
path = File.join(
options[:views_directory] || Sinatra.application.options.public,
"#{name}.#{renderer}"
) )
end File.exists?(path) ? File.read(path) : nil
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 end
def layouts def layouts
@ -362,13 +353,13 @@ module Sinatra
module Erb module Erb
def erb(content, options={}) def erb(content, options={})
render(content, options.merge(:renderer => :erb, :ext => :erb)) require 'erb'
render(:erb, content, options)
end end
private private
def evaluate_erb(content) def render_erb(content)
require 'erb'
::ERB.new(content).result(binding) ::ERB.new(content).result(binding)
end end
@ -377,14 +368,14 @@ module Sinatra
module Haml module Haml
def haml(content, options={}) def haml(content, options={})
render(content, options.merge(:renderer => :haml, :ext => :haml)) require 'haml'
render(:haml, content, options)
end end
private private
def evaluate_haml(content) def render_haml(content, &b)
require 'haml' ::Haml::Engine.new(content).render(self, &b)
::Haml::Engine.new(content).render(self)
end end
end end

View file

@ -34,7 +34,7 @@ context "Erb" do
specify "can be inline" do specify "can be inline" do
layout do layout do
%Q{This is <%= @content %>!} %Q{This is <%= yield %>!}
end end
get '/lay' do get '/lay' do
@ -50,7 +50,7 @@ context "Erb" do
specify "can use named layouts" do specify "can use named layouts" do
layout :pretty do layout :pretty do
%Q{<h1><%= @content %></h1>} %Q{<h1><%= yield %></h1>}
end end
get '/pretty' do get '/pretty' do
@ -84,5 +84,33 @@ 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 end

View file

@ -34,7 +34,7 @@ context "Haml" do
specify "can be inline" do specify "can be inline" do
layout do layout do
'== This is #{@content}!' '== This is #{yield}!'
end end
get '/lay' do get '/lay' do
@ -50,7 +50,7 @@ context "Haml" do
specify "can use named layouts" do specify "can use named layouts" do
layout :pretty do layout :pretty do
"%h1== \#{@content}" '%h1== #{yield}'
end end
get '/pretty' do get '/pretty' do
@ -82,7 +82,45 @@ context "Haml" do
end end
specify "can be read from file and layout from text" do
get '/foo' do
haml 'Test', :layout => '== Foo #{yield}'
end 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 end

1
test/views/foo.haml Normal file
View file

@ -0,0 +1 @@
== You rock #{@name}!

View file

@ -1 +0,0 @@
You rock #{@name}!

View file

@ -1,2 +1,2 @@
<%= @title %> <%= @title %>
Hi <%= @content %> Hi <%= yield %>

View file

@ -1,2 +1,2 @@
== #{@title} == #{@title}
== Hi #{@content} == Hi #{yield}

View file

@ -0,0 +1 @@
This is foo!

View file

@ -1 +1 @@
x <%= @content %> x x <%= yield %> x

View file

@ -0,0 +1 @@
== x #{yield} x