This commit is contained in:
Blake Mizerany 2008-02-24 01:32:52 -08:00
parent 93d47e99a4
commit 520ac2e918
3 changed files with 131 additions and 14 deletions

View File

@ -154,10 +154,6 @@ module Sinatra
module RenderingHelpers
def erb(content, options={})
render(content, options.merge(:renderer => :erb, :ext => :erb))
end
def render(content, options={})
options[:layout] ||= :layout
template = resolve_template(content, options)
@ -167,13 +163,12 @@ module Sinatra
@content
end
def partial(content, options={})
renderer(content, options.merge(:layout => nil))
end
private
def evaluate_erb(content, options={})
require 'erb'
ERB.new(content).result(binding)
end
def evaluate_renderer(content, options={})
renderer = "evaluate_#{options[:renderer]}"
result = case content
@ -184,7 +179,7 @@ module Sinatra
when File
content.read
end
send(renderer, result, options)
send(renderer, result)
end
def resolve_template(content, options={})
@ -192,7 +187,7 @@ module Sinatra
when String
content
when Symbol
File.new(filename_for(content, options))
File.new(path_to(content, options))
end
end
@ -201,12 +196,12 @@ module Sinatra
if layout = layouts[name || :layout]
return layout
end
if File.file?(filename = filename_for(name, options))
if File.file?(filename = path_to(name, options))
File.new(filename)
end
end
def filename_for(name, options={})
def path_to(name, options={})
(options[:views_directory] || 'views') + "/#{name}.#{options[:ext]}"
end
@ -216,10 +211,42 @@ module Sinatra
end
module Erb
def erb(content, options={})
render(content, options.merge(:renderer => :erb, :ext => :erb))
end
private
def evaluate_erb(content)
require 'erb'
::ERB.new(content).result(binding)
end
end
module Haml
def haml(content, options={})
render(content, options.merge(:renderer => :haml, :ext => :haml))
end
private
def evaluate_haml(content)
require 'haml'
::Haml::Engine.new(content).render(self)
end
end
class EventContext
include ResponseHelpers
include RenderingHelpers
include Erb
include Haml
attr_accessor :request, :response

88
test/haml_test.rb Normal file
View File

@ -0,0 +1,88 @@
require File.dirname(__FILE__) + '/helper'
context "Haml" do
setup do
Sinatra.application = nil
end
context "without layouts" do
setup do
Sinatra.application = nil
end
specify "should render" do
get '/no_layout' do
haml '== #{1+1}'
end
get_it '/no_layout'
should.be.ok
body.should == "2\n"
end
end
context "with layouts" do
setup do
Sinatra.application = nil
end
specify "can be inline" do
layout do
'== This is #{@content}!'
end
get '/lay' do
haml 'Blake'
end
get_it '/lay'
should.be.ok
body.should.equal "This is Blake\n!\n"
end
specify "can use named layouts" do
layout :pretty do
"%h1== \#{@content}"
end
get '/pretty' do
haml 'Foo', :layout => :pretty
end
get '/not_pretty' do
haml 'Bar'
end
get_it '/pretty'
body.should.equal "<h1>Foo</h1>\n"
get_it '/not_pretty'
body.should.equal "Bar\n"
end
specify "can be read from a file if they're not inlined" do
get '/foo' do
@title = 'Welcome to the Hello Program'
haml 'Blake', :layout => :foo_layout,
:views_directory => File.dirname(__FILE__) + "/views"
end
get_it '/foo'
body.should.equal "Welcome to the Hello Program\nHi Blake\n"
end
end
end

View File

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