Haml & Erb redo
This commit is contained in:
parent
59199dcc13
commit
7d5bc1f0da
|
@ -305,52 +305,43 @@ 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
|
||||
''
|
||||
def resolve_template(renderer, template, options)
|
||||
case template
|
||||
when String
|
||||
content
|
||||
template
|
||||
when Proc
|
||||
content.call
|
||||
when File
|
||||
content.read
|
||||
template.call
|
||||
when Symbol
|
||||
evaluate_renderer(
|
||||
File.new(path_to(content, options)),
|
||||
options
|
||||
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}"
|
||||
)
|
||||
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]}"
|
||||
File.exists?(path) ? File.read(path) : nil
|
||||
end
|
||||
|
||||
def layouts
|
||||
|
@ -362,13 +353,13 @@ 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'
|
||||
def render_erb(content)
|
||||
::ERB.new(content).result(binding)
|
||||
end
|
||||
|
||||
|
@ -377,14 +368,14 @@ module Sinatra
|
|||
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)
|
||||
def render_haml(content, &b)
|
||||
::Haml::Engine.new(content).render(self, &b)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
@ -84,5 +84,33 @@ context "Erb" do
|
|||
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
@ -82,7 +82,45 @@ context "Haml" do
|
|||
|
||||
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
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
== You rock #{@name}!
|
|
@ -1 +0,0 @@
|
|||
You rock #{@name}!
|
|
@ -1,2 +1,2 @@
|
|||
<%= @title %>
|
||||
Hi <%= @content %>
|
||||
Hi <%= yield %>
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
== #{@title}
|
||||
== Hi #{@content}
|
||||
== Hi #{yield}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
This is foo!
|
|
@ -1 +1 @@
|
|||
x <%= @content %> x
|
||||
x <%= yield %> x
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
== x #{yield} x
|
Loading…
Reference in New Issue