2010-12-13 05:40:17 -05:00
|
|
|
# encoding: UTF-8
|
2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2010-09-12 09:22:24 -04:00
|
|
|
File.delete(File.dirname(__FILE__) + '/views/layout.test') rescue nil
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-06-07 12:50:22 -04:00
|
|
|
class TestTemplate < Tilt::Template
|
2010-03-04 06:12:36 -05:00
|
|
|
def prepare
|
2009-06-07 12:50:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def evaluate(scope, locals={}, &block)
|
|
|
|
inner = block ? block.call : ''
|
|
|
|
data + inner
|
|
|
|
end
|
|
|
|
|
|
|
|
Tilt.register 'test', self
|
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class TemplatesTest < Test::Unit::TestCase
|
2010-09-19 11:57:48 -04:00
|
|
|
def render_app(base=Sinatra::Base, options = {}, &block)
|
|
|
|
base, options = Sinatra::Base, base if base.is_a? Hash
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app(base) do
|
2008-12-13 16:06:02 -05:00
|
|
|
set :views, File.dirname(__FILE__) + '/views'
|
2010-09-19 11:57:48 -04:00
|
|
|
set options
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', &block)
|
2008-12-13 16:06:02 -05:00
|
|
|
template(:layout3) { "Layout 3!\n" }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
get '/'
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_default_layout
|
|
|
|
layout = File.dirname(__FILE__) + '/views/layout.test'
|
|
|
|
File.open(layout, 'wb') { |io| io.write "Layout!\n" }
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
File.unlink(layout) rescue nil
|
|
|
|
end
|
|
|
|
|
2012-09-13 16:55:20 -04:00
|
|
|
it 'falls back to engine layout' do
|
|
|
|
mock_app do
|
|
|
|
template(:layout3) { 'Layout 3!<%= yield %>' }
|
|
|
|
set :erb, :layout => :layout3
|
|
|
|
|
|
|
|
get('/') do
|
|
|
|
erb('Hello World!', { :layout => true })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout 3!Hello World!", body
|
|
|
|
end
|
|
|
|
|
2012-09-13 18:29:58 -04:00
|
|
|
it 'falls back to default layout if engine layout is true' do
|
|
|
|
mock_app do
|
|
|
|
template(:layout) { 'Layout!!! <%= yield %>' }
|
|
|
|
set :erb, :layout => true
|
|
|
|
|
|
|
|
get('/') do
|
|
|
|
erb('Hello World!', { :layout => true })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout!!! Hello World!", body
|
|
|
|
end
|
|
|
|
|
2013-02-02 06:49:54 -05:00
|
|
|
it 'renders no layout if layout if falsy' do
|
|
|
|
mock_app do
|
|
|
|
template(:layout) { 'Layout!!! <%= yield %>' }
|
|
|
|
set :erb, :layout => true
|
|
|
|
|
|
|
|
get('/') do
|
|
|
|
erb('Hello World!', { :layout => nil })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal "Hello World!", body
|
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'renders String templates directly' do
|
2012-05-21 17:21:59 -04:00
|
|
|
render_app { render(:test, 'Hello World') }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders Proc templates using the call result' do
|
2012-05-21 17:21:59 -04:00
|
|
|
render_app { render(:test, Proc.new {'Hello World'}) }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'looks up Symbol templates in views directory' do
|
2012-05-21 17:21:59 -04:00
|
|
|
render_app { render(:test, :hello) }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Hello World!\n", body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the default layout template if not explicitly overridden' do
|
|
|
|
with_default_layout do
|
2012-05-21 17:21:59 -04:00
|
|
|
render_app { render(:test, :hello) }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout!\nHello World!\n", body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-08 12:01:36 -05:00
|
|
|
it 'uses the default layout template if not really overriden' do
|
|
|
|
with_default_layout do
|
2012-05-21 17:21:59 -04:00
|
|
|
render_app { render(:test, :hello, :layout => true) }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout!\nHello World!\n", body
|
2009-01-08 12:01:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'uses the layout template specified' do
|
2012-05-21 17:21:59 -04:00
|
|
|
render_app { render(:test, :hello, :layout => :layout2) }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout 2!\nHello World!\n", body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses layout templates defined with the #template method' do
|
2012-05-21 17:21:59 -04:00
|
|
|
render_app { render(:test, :hello, :layout => :layout3) }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout 3!\nHello World!\n", body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2010-09-19 08:56:12 -04:00
|
|
|
it 'avoids wrapping layouts around nested templates' do
|
2012-05-21 17:21:59 -04:00
|
|
|
render_app { render(:str, :nested, :layout => :layout2) }
|
2010-09-19 08:56:12 -04:00
|
|
|
assert ok?
|
2012-05-21 17:21:59 -04:00
|
|
|
assert_equal(
|
|
|
|
"<h1>String Layout!</h1>\n<content><h1>Hello From String</h1></content>",
|
|
|
|
body
|
|
|
|
)
|
2010-09-19 08:56:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows explicitly wrapping layouts around nested templates' do
|
2012-05-21 17:21:59 -04:00
|
|
|
render_app { render(:str, :explicitly_nested, :layout => :layout2) }
|
2010-09-19 08:56:12 -04:00
|
|
|
assert ok?
|
2012-05-21 17:21:59 -04:00
|
|
|
assert_equal(
|
|
|
|
"<h1>String Layout!</h1>\n<content><h1>String Layout!</h1>\n<h1>Hello From String</h1></content>",
|
|
|
|
body
|
|
|
|
)
|
2010-09-19 08:56:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'two independent render calls do not disable layouts' do
|
|
|
|
render_app do
|
|
|
|
render :str, :explicitly_nested, :layout => :layout2
|
|
|
|
render :str, :nested, :layout => :layout2
|
|
|
|
end
|
|
|
|
assert ok?
|
2012-05-21 17:21:59 -04:00
|
|
|
assert_equal(
|
|
|
|
"<h1>String Layout!</h1>\n<content><h1>Hello From String</h1></content>",
|
|
|
|
body
|
|
|
|
)
|
2010-09-19 08:56:12 -04:00
|
|
|
end
|
|
|
|
|
2010-11-11 18:33:30 -05:00
|
|
|
it 'is possible to use partials in layouts' do
|
|
|
|
render_app do
|
|
|
|
settings.layout { "<%= erb 'foo' %><%= yield %>" }
|
|
|
|
erb 'bar'
|
|
|
|
end
|
|
|
|
assert ok?
|
|
|
|
assert_equal "foobar", body
|
|
|
|
end
|
|
|
|
|
2010-01-29 12:18:19 -05:00
|
|
|
it 'loads templates from source file' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app { enable(:inline_templates) }
|
2009-06-07 12:50:22 -04:00
|
|
|
assert_equal "this is foo\n\n", @app.templates[:foo][0]
|
|
|
|
assert_equal "X\n= yield\nX\n", @app.templates[:layout][0]
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2009-04-09 05:17:29 -04:00
|
|
|
|
2010-09-16 08:44:37 -04:00
|
|
|
it 'ignores spaces after names of inline templates' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app { enable(:inline_templates) }
|
2010-09-16 08:44:37 -04:00
|
|
|
assert_equal "There's a space after 'bar'!\n\n", @app.templates[:bar][0]
|
2010-09-19 07:26:26 -04:00
|
|
|
assert_equal "this is not foo\n\n", @app.templates[:"foo bar"][0]
|
2010-09-16 08:44:37 -04:00
|
|
|
end
|
|
|
|
|
2010-01-29 12:18:19 -05:00
|
|
|
it 'loads templates from given source file' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app { set(:inline_templates, __FILE__) }
|
2010-01-29 12:18:19 -05:00
|
|
|
assert_equal "this is foo\n\n", @app.templates[:foo][0]
|
2009-05-23 10:15:19 -04:00
|
|
|
end
|
|
|
|
|
2010-01-29 12:18:19 -05:00
|
|
|
test 'inline_templates ignores IO errors' do
|
2012-05-21 17:21:59 -04:00
|
|
|
assert_nothing_raised { mock_app { set(:inline_templates, '/foo/bar') } }
|
2009-04-09 05:17:29 -04:00
|
|
|
|
|
|
|
assert @app.templates.empty?
|
|
|
|
end
|
2009-05-19 10:03:35 -04:00
|
|
|
|
2010-12-13 05:40:17 -05:00
|
|
|
it 'allows unicode in inline templates' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app { set(:inline_templates, __FILE__) }
|
|
|
|
assert_equal(
|
|
|
|
"Den som tror at hemma det är där man bor har aldrig vart hos mig.\n\n",
|
2010-12-13 05:40:17 -05:00
|
|
|
@app.templates[:umlaut][0]
|
2012-05-21 17:21:59 -04:00
|
|
|
)
|
2010-12-13 05:40:17 -05:00
|
|
|
end
|
|
|
|
|
2010-01-29 12:18:19 -05:00
|
|
|
it 'loads templates from specified views directory' do
|
2013-02-25 20:49:39 -05:00
|
|
|
render_app { render(:test, :hello, :views => settings.views + '/foo') }
|
2010-01-29 12:18:19 -05:00
|
|
|
|
|
|
|
assert_equal "from another views directory\n", body
|
|
|
|
end
|
|
|
|
|
2013-02-25 20:49:39 -05:00
|
|
|
it 'takes views directory into consideration for caching' do
|
|
|
|
render_app do
|
|
|
|
render(:test, :hello) + render(:test, :hello, :views => settings.views + '/foo')
|
|
|
|
end
|
|
|
|
assert_equal "Hello World!\nfrom another views directory\n", body
|
|
|
|
end
|
|
|
|
|
2009-05-19 10:03:35 -04:00
|
|
|
it 'passes locals to the layout' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
|
|
|
template(:my_layout) { 'Hello <%= name %>!<%= yield %>' }
|
2009-05-19 10:03:35 -04:00
|
|
|
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') do
|
|
|
|
erb('<p>content</p>', { :layout => :my_layout }, { :name => 'Mike'})
|
2009-05-19 10:03:35 -04:00
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-05-19 10:03:35 -04:00
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello Mike!<p>content</p>', body
|
|
|
|
end
|
2009-03-26 11:01:20 -04:00
|
|
|
|
2013-02-26 01:13:02 -05:00
|
|
|
it 'sets layout-only options via layout_options' do
|
|
|
|
mock_app do
|
|
|
|
get '/' do
|
|
|
|
render(:str, :in_a,
|
|
|
|
:views => settings.views + '/a',
|
|
|
|
:layout_options => { :views => settings.views },
|
|
|
|
:layout => :layout2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal "<h1>String Layout!</h1>\nGimme an A!\n", body
|
|
|
|
end
|
|
|
|
|
2009-03-26 11:01:20 -04:00
|
|
|
it 'loads templates defined in subclasses' do
|
|
|
|
base = Class.new(Sinatra::Base)
|
|
|
|
base.template(:foo) { 'bar' }
|
2012-05-21 17:21:59 -04:00
|
|
|
render_app(base) { render(:test, :foo) }
|
2009-03-26 11:01:20 -04:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'bar', body
|
|
|
|
end
|
|
|
|
|
2010-09-19 11:57:48 -04:00
|
|
|
it 'allows setting default content type per template engine' do
|
2012-05-21 17:21:59 -04:00
|
|
|
render_app(:str => { :content_type => :txt }) {
|
|
|
|
render :str, 'foo'
|
|
|
|
}
|
2010-09-19 11:57:48 -04:00
|
|
|
assert_equal 'text/plain;charset=utf-8', response['Content-Type']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'setting default content type does not affect other template engines' do
|
2012-05-21 17:21:59 -04:00
|
|
|
render_app(:str => { :content_type => :txt }) {
|
|
|
|
render :test, 'foo'
|
|
|
|
}
|
2010-09-19 11:57:48 -04:00
|
|
|
assert_equal 'text/html;charset=utf-8', response['Content-Type']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'setting default content type per template engine does not override content_type' do
|
|
|
|
render_app :str => { :content_type => :txt } do
|
|
|
|
content_type :html
|
|
|
|
render :str, 'foo'
|
|
|
|
end
|
|
|
|
assert_equal 'text/html;charset=utf-8', response['Content-Type']
|
|
|
|
end
|
|
|
|
|
2009-03-26 11:01:20 -04:00
|
|
|
it 'uses templates in superclasses before subclasses' do
|
|
|
|
base = Class.new(Sinatra::Base)
|
|
|
|
base.template(:foo) { 'template in superclass' }
|
2009-06-07 12:50:22 -04:00
|
|
|
assert_equal 'template in superclass', base.templates[:foo].first.call
|
|
|
|
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app(base) do
|
2009-06-07 12:50:22 -04:00
|
|
|
set :views, File.dirname(__FILE__) + '/views'
|
|
|
|
template(:foo) { 'template in subclass' }
|
|
|
|
get('/') { render :test, :foo }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-06-07 12:50:22 -04:00
|
|
|
assert_equal 'template in subclass', @app.templates[:foo].first.call
|
2009-03-26 11:01:20 -04:00
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'template in subclass', body
|
|
|
|
end
|
2010-12-14 16:40:42 -05:00
|
|
|
|
|
|
|
it "is possible to use a different engine for the layout than for the template itself explicitely" do
|
|
|
|
render_app do
|
|
|
|
settings.template(:layout) { 'Hello <%= yield %>!' }
|
|
|
|
render :str, "<%= 'World' %>", :layout_engine => :erb
|
|
|
|
end
|
|
|
|
assert_equal "Hello <%= 'World' %>!", body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is possible to use a different engine for the layout than for the template itself globally" do
|
|
|
|
render_app :str => { :layout_engine => :erb } do
|
|
|
|
settings.template(:layout) { 'Hello <%= yield %>!' }
|
|
|
|
render :str, "<%= 'World' %>"
|
|
|
|
end
|
|
|
|
assert_equal "Hello <%= 'World' %>!", body
|
|
|
|
end
|
2011-02-17 09:51:52 -05:00
|
|
|
|
2011-03-14 03:57:02 -04:00
|
|
|
it "does not leak the content type to the template" do
|
|
|
|
render_app :str => { :layout_engine => :erb } do
|
|
|
|
settings.template(:layout) { 'Hello <%= yield %>!' }
|
|
|
|
render :str, "<%= 'World' %>", :content_type => :txt
|
|
|
|
end
|
|
|
|
assert_equal "text/html;charset=utf-8", headers['Content-Type']
|
|
|
|
end
|
|
|
|
|
2011-02-17 09:51:52 -05:00
|
|
|
it "is possible to register another template" do
|
|
|
|
Tilt.register "html.erb", Tilt[:erb]
|
|
|
|
render_app { render :erb, :calc }
|
|
|
|
assert_equal '2', body
|
|
|
|
end
|
2011-02-19 08:10:18 -05:00
|
|
|
|
|
|
|
it "passes scope to the template" do
|
2011-02-19 08:14:17 -05:00
|
|
|
mock_app do
|
2012-05-21 17:21:59 -04:00
|
|
|
template(:scoped) { 'Hello <%= foo %>' }
|
2011-02-19 08:10:18 -05:00
|
|
|
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') do
|
2011-02-19 08:14:17 -05:00
|
|
|
some_scope = Object.new
|
|
|
|
def some_scope.foo() 'World!' end
|
2011-02-19 08:10:18 -05:00
|
|
|
erb :scoped, :scope => some_scope
|
|
|
|
end
|
2011-02-19 08:14:17 -05:00
|
|
|
end
|
2011-02-19 08:10:18 -05:00
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World!', body
|
|
|
|
end
|
2011-02-19 09:37:49 -05:00
|
|
|
|
|
|
|
it "is possible to use custom logic for finding template files" do
|
|
|
|
mock_app do
|
|
|
|
set :views, ["a", "b"].map { |d| File.dirname(__FILE__) + '/views/' + d }
|
|
|
|
def find_template(views, name, engine, &block)
|
|
|
|
Array(views).each { |v| super(v, name, engine, &block) }
|
|
|
|
end
|
|
|
|
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/:name') { render(:str, params[:name].to_sym) }
|
2011-02-19 09:37:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
get '/in_a'
|
|
|
|
assert_body 'Gimme an A!'
|
|
|
|
|
|
|
|
get '/in_b'
|
|
|
|
assert_body 'Gimme a B!'
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2009-04-17 08:17:15 -04:00
|
|
|
# __END__ : this is not the real end of the script.
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
__END__
|
|
|
|
|
|
|
|
@@ foo
|
|
|
|
this is foo
|
|
|
|
|
2012-07-18 15:07:24 -04:00
|
|
|
@@ bar
|
2010-09-16 08:44:37 -04:00
|
|
|
There's a space after 'bar'!
|
|
|
|
|
2010-09-19 07:26:26 -04:00
|
|
|
@@ foo bar
|
|
|
|
this is not foo
|
|
|
|
|
2010-12-13 05:40:17 -05:00
|
|
|
@@ umlaut
|
|
|
|
Den som tror at hemma det är där man bor har aldrig vart hos mig.
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
@@ layout
|
|
|
|
X
|
|
|
|
= yield
|
|
|
|
X
|