Skip to content

Commit

Permalink
Add support for passing a block to the markaby method:
Browse files Browse the repository at this point in the history
    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.
  • Loading branch information
rkh committed Nov 12, 2010
1 parent 09626e8 commit 0ff41a6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@


* Added `slim` rendering method for rendering Slim templates. (Steve Hodgkiss) * 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) * README has been translated to Russian. (Nickolay Schwarz, Vasily Polovnyov)


* Nested templates without a `:layout` option can now be used from the layout * Nested templates without a `:layout` option can now be used from the layout
Expand Down
6 changes: 6 additions & 0 deletions README.rdoc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -420,6 +420,12 @@ The markaby gem/library is required to render Markaby templates:


Renders <tt>./views/index.mab</tt>. 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 === Slim Templates


The slim gem/library is required to render Slim templates: The slim gem/library is required to render Slim templates:
Expand Down
14 changes: 7 additions & 7 deletions lib/sinatra/base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ def less(template, options={}, locals={})
end end


def builder(template=nil, options={}, locals={}, &block) 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 end


def liquid(template, options={}, locals={}) def liquid(template, options={}, locals={})
Expand All @@ -429,8 +430,8 @@ def radius(template, options={}, locals={})
render :radius, template, options, locals render :radius, template, options, locals
end end


def markaby(template, options={}, locals={}) def markaby(template=nil, options={}, locals={}, &block)
render :mab, template, options, locals render_ruby(:mab, template, options, locals, &block)
end end


def coffee(template, options={}, locals={}) def coffee(template, options={}, locals={})
Expand All @@ -439,8 +440,8 @@ def coffee(template, options={}, locals={})
end end


def nokogiri(template=nil, options={}, locals={}, &block) def nokogiri(template=nil, options={}, locals={}, &block)
options[:layout] = false if Tilt::VERSION <= "1.1" options[:default_content_type] = :xml
render_xml(:nokogiri, template, options, locals, &block) render_ruby(:nokogiri, template, options, locals, &block)
end end


def slim(template, options={}, locals={}) def slim(template, options={}, locals={})
Expand All @@ -449,8 +450,7 @@ def slim(template, options={}, locals={})


private private
# logic shared between builder and nokogiri # logic shared between builder and nokogiri
def render_xml(engine, template, options={}, locals={}, &block) def render_ruby(engine, template, options={}, locals={}, &block)
options[:default_content_type] = :xml
options, template = template, nil if template.is_a?(Hash) options, template = template, nil if template.is_a?(Hash)
template = Proc.new { block } if template.nil? template = Proc.new { block } if template.nil?
render engine, template, options, locals render engine, template, options, locals
Expand Down
34 changes: 34 additions & 0 deletions test/markaby_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ def markaby_app(&block)
get '/' get '/'
end 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 it 'renders inline markaby strings' do
markaby_app { markaby 'h1 "Hiya"' } markaby_app { markaby 'h1 "Hiya"' }
assert ok? assert ok?
Expand Down Expand Up @@ -40,6 +47,33 @@ def markaby_app(&block)
assert_equal "<h1>Markaby Layout!</h1><p>Hello World</p>", body assert_equal "<h1>Markaby Layout!</h1><p>Hello World</p>", body
end 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 it "raises error if template not found" do
mock_app { get('/') { markaby :no_such_template } } mock_app { get('/') { markaby :no_such_template } }
assert_raise(Errno::ENOENT) { get('/') } assert_raise(Errno::ENOENT) { get('/') }
Expand Down

0 comments on commit 0ff41a6

Please sign in to comment.