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,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

View File

@ -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{<h1><%= @content %></h1>}
%Q{<h1><%= yield %></h1>}
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

View File

@ -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

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 %>
Hi <%= @content %>
Hi <%= yield %>

View File

@ -1,2 +1,2 @@
== #{@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