Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add markaby helper method. Tilt supports Markaby for quite some time …

…now, but it was not as easy to use as haml or erb, and not documented. Tests and documentation (English and German) included.
  • Loading branch information...
commit 8ce74b3ad271bdf072845dcb17eadf4ee3569a6b 1 parent 8718419
@rkh rkh authored
View
13 README.de.rdoc
@@ -411,6 +411,19 @@ aufrufen kann, will man nahezu in allen Fällen +locals+ übergeben:
radius :index, :locals => { :key => 'value' }
+=== Markaby-Templates
+
+Das markaby gem wird benötigt um Markaby-Templates rendern zu können:
+
+ ## markaby muss eingebunden werden
+ require 'markaby'
+
+ get '/' do
+ markaby :index
+ end
+
+Dieser Code rendert <tt>./views/index.mab</tt>.
+
=== Inline-Templates
get '/' do
View
13 README.rdoc
@@ -399,6 +399,19 @@ template, you almost always want to pass locals to it:
radius :index, :locals => { :key => 'value' }
+=== Markaby Templates
+
+The markaby gem/library is required to render Markaby templates:
+
+ ## You'll need to require markaby in your app
+ require 'markaby'
+
+ get '/' do
+ markaby :index
+ end
+
+Renders <tt>./views/index.mab</tt>.
+
=== Inline Templates
get '/' do
View
5 Rakefile
@@ -16,10 +16,9 @@ end
if !ENV['NO_TEST_FIX'] and RUBY_VERSION == '1.9.2' and RUBY_PATCHLEVEL == 0
# Avoids seg fault
task(:test) do
- second_run = %w[test/settings_test.rb test/rdoc_test.rb]
+ second_run = %w[settings rdoc markaby].map { |l| "test/#{l}_test.rb" }
first_run = Dir.glob('test/*_test.rb') - second_run
- sh "testrb #{first_run.join ' '}"
- sh "testrb #{second_run.join ' '}"
+ [first_run, second_run].each { |f| sh "testrb #{f.join ' '}" }
end
else
Rake::TestTask.new(:test) do |t|
View
4 lib/sinatra/base.rb
@@ -355,6 +355,10 @@ def radius(template, options={}, locals={})
render :radius, template, options, locals
end
+ def markaby(template, options={}, locals={})
+ render :mab, template, options, locals
+ end
+
private
def render(engine, data, options={}, locals={}, &block)
# merge app-level options
View
1  test/hello.mab
@@ -0,0 +1 @@
+h1 "Hello From Markaby"
View
58 test/markaby_test.rb
@@ -0,0 +1,58 @@
+require File.dirname(__FILE__) + '/helper'
+
+begin
+require 'markaby'
+
+class MarkabyTest < Test::Unit::TestCase
+ def markaby_app(&block)
+ mock_app do
+ set :views, File.dirname(__FILE__) + '/views'
+ get '/', &block
+ end
+ get '/'
+ end
+
+ it 'renders inline markaby strings' do
+ markaby_app { markaby 'h1 "Hiya"' }
+ assert ok?
+ assert_equal "<h1>Hiya</h1>", body
+ end
+
+ it 'renders .markaby files in views path' do
+ markaby_app { markaby :hello }
+ assert ok?
+ assert_equal "<h1>Hello From Markaby</h1>", body
+ end
+
+ it "renders with inline layouts" do
+ mock_app do
+ layout { 'h1 { text "THIS. IS. "; yield }' }
+ get('/') { markaby 'em "SPARTA"' }
+ end
+ get '/'
+ assert ok?
+ assert_equal "<h1>THIS. IS. <em>SPARTA</em></h1>", body
+ end
+
+ it "renders with file layouts" do
+ markaby_app { markaby 'text "Hello World"', :layout => :layout2 }
+ assert ok?
+ assert_equal "<h1>Markaby Layout!</h1><p>Hello World</p>", body
+ end
+
+ it "raises error if template not found" do
+ mock_app { get('/') { markaby :no_such_template } }
+ assert_raise(Errno::ENOENT) { get('/') }
+ end
+
+ it "allows passing locals" do
+ markaby_app do
+ markaby 'text value', :locals => { :value => 'foo' }
+ end
+ assert ok?
+ assert_equal 'foo', body
+ end
+end
+rescue
+ warn "#{$!.to_s}: skipping markaby tests"
+end
View
1  test/views/hello.mab
@@ -0,0 +1 @@
+h1 "Hello From Markaby"
View
2  test/views/layout2.mab
@@ -0,0 +1,2 @@
+h1 "Markaby Layout!"
+p { yield }
Please sign in to comment.
Something went wrong with that request. Please try again.