Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit 0ff41a69d489127be9c5c543104ef1ee76c5a40a 1 parent 09626e8
@rkh rkh authored
View
3  CHANGES
@@ -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
6 README.rdoc
@@ -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
14 lib/sinatra/base.rb
@@ -406,7 +406,8 @@ def less(template, options={}, locals={})
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 @@ def radius(template, options={}, locals={})
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 @@ def coffee(template, options={}, locals={})
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 @@ def slim(template, options={}, locals={})
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
34 test/markaby_test.rb
@@ -12,6 +12,13 @@ def markaby_app(&block)
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 @@ def markaby_app(&block)
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('/') }
Please sign in to comment.
Something went wrong with that request. Please try again.