Add support for passing a block to the markaby method:

get '/' do
      markaby { h1 'welcome' }
    end

At them moment this only works with Tilt master (mentioned in the README,
increasing Tilt version in gemspec would make master unusable).

Tests and docs (English only) are, as usual, included.
This commit is contained in:
Konstantin Haase 2010-11-12 14:18:52 -06:00
parent 09626e8e40
commit 0ff41a69d4
4 changed files with 50 additions and 7 deletions

View File

@ -2,6 +2,9 @@
* Added `slim` rendering method for rendering Slim templates. (Steve Hodgkiss)
* The `markaby` rendering method now allows passing a block, making inline
usage possible. Requires Tilt 1.2 or newer. (Konstantin Haase)
* README has been translated to Russian. (Nickolay Schwarz, Vasily Polovnyov)
* Nested templates without a `:layout` option can now be used from the layout

View File

@ -420,6 +420,12 @@ The markaby gem/library is required to render Markaby templates:
Renders <tt>./views/index.mab</tt>.
If you have Tilt 1.2 or later, you may also use inline markaby:
get '/' do
markaby { h1 "Welcome!" }
end
=== Slim Templates
The slim gem/library is required to render Slim templates:

View File

@ -406,7 +406,8 @@ module Sinatra
end
def builder(template=nil, options={}, locals={}, &block)
render_xml(:builder, template, options, locals, &block)
options[:default_content_type] = :xml
render_ruby(:builder, template, options, locals, &block)
end
def liquid(template, options={}, locals={})
@ -429,8 +430,8 @@ module Sinatra
render :radius, template, options, locals
end
def markaby(template, options={}, locals={})
render :mab, template, options, locals
def markaby(template=nil, options={}, locals={}, &block)
render_ruby(:mab, template, options, locals, &block)
end
def coffee(template, options={}, locals={})
@ -439,8 +440,8 @@ module Sinatra
end
def nokogiri(template=nil, options={}, locals={}, &block)
options[:layout] = false if Tilt::VERSION <= "1.1"
render_xml(:nokogiri, template, options, locals, &block)
options[:default_content_type] = :xml
render_ruby(:nokogiri, template, options, locals, &block)
end
def slim(template, options={}, locals={})
@ -449,8 +450,7 @@ module Sinatra
private
# logic shared between builder and nokogiri
def render_xml(engine, template, options={}, locals={}, &block)
options[:default_content_type] = :xml
def render_ruby(engine, template, options={}, locals={}, &block)
options, template = template, nil if template.is_a?(Hash)
template = Proc.new { block } if template.nil?
render engine, template, options, locals

View File

@ -12,6 +12,13 @@ class MarkabyTest < Test::Unit::TestCase
get '/'
end
def check_tilt(&block)
instance_eval(&block)
rescue TypeError => e
raise e unless Tilt::VERSION < '1.2'
warn "\nUpgrade Tilt!"
end
it 'renders inline markaby strings' do
markaby_app { markaby 'h1 "Hiya"' }
assert ok?
@ -40,6 +47,33 @@ class MarkabyTest < Test::Unit::TestCase
assert_equal "<h1>Markaby Layout!</h1><p>Hello World</p>", body
end
it 'renders inline markaby blocks' do
check_tilt do
markaby_app { markaby { h1 'Hiya' } }
assert ok?
assert_equal "<h1>Hiya</h1>", body
end
end
it 'renders inline markaby blocks with inline layouts' do
check_tilt do
markaby_app do
settings.layout { 'h1 { text "THIS. IS. "; yield }' }
markaby { em 'SPARTA' }
end
assert ok?
assert_equal "<h1>THIS. IS. <em>SPARTA</em></h1>", body
end
end
it 'renders inline markaby blocks with file layouts' do
check_tilt do
markaby_app { markaby(:layout => :layout2) { text "Hello World" } }
assert ok?
assert_equal "<h1>Markaby Layout!</h1><p>Hello World</p>", body
end
end
it "raises error if template not found" do
mock_app { get('/') { markaby :no_such_template } }
assert_raise(Errno::ENOENT) { get('/') }