Permalink
Browse files

Haml & Erb redo

  • Loading branch information...
Blake Mizerany
Blake Mizerany committed Feb 28, 2008
1 parent 59199dc commit 7d5bc1f0da7f87120a2b4ae1920ec7fc5c4455f4
View
@@ -305,54 +305,45 @@ def headers(header = nil)
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
- )
- 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)
+ template_file(renderer, template, options)
+ else
+ nil
end
end
- def path_to(name, options={})
- (options[:views_directory] || 'views') + "/#{name}.#{options[:ext]}"
+ 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 @@ def layouts
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
@@ -34,7 +34,7 @@
specify "can be inline" do
layout do
- %Q{This is <%= @content %>!}
+ %Q{This is <%= yield %>!}
end
get '/lay' do
@@ -50,7 +50,7 @@
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 @@
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
@@ -34,7 +34,7 @@
specify "can be inline" do
layout do
- '== This is #{@content}!'
+ '== This is #{yield}!'
end
get '/lay' do
@@ -50,7 +50,7 @@
specify "can use named layouts" do
layout :pretty do
- "%h1== \#{@content}"
+ '%h1== #{yield}'
end
get '/pretty' do
@@ -81,8 +81,46 @@
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
View
@@ -0,0 +1 @@
+== You rock #{@name}!
View
@@ -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

0 comments on commit 7d5bc1f

Please sign in to comment.