Skip to content

Commit

Permalink
Add markaby helper method. Tilt supports Markaby for quite some time …
Browse files Browse the repository at this point in the history
…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
rkh committed Sep 12, 2010
1 parent 8718419 commit 8ce74b3
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 3 deletions.
13 changes: 13 additions & 0 deletions README.de.rdoc
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions README.rdoc
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions Rakefile
Expand Up @@ -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|
Expand Down
4 changes: 4 additions & 0 deletions lib/sinatra/base.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/hello.mab
@@ -0,0 +1 @@
h1 "Hello From Markaby"
58 changes: 58 additions & 0 deletions 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
1 change: 1 addition & 0 deletions test/views/hello.mab
@@ -0,0 +1 @@
h1 "Hello From Markaby"
2 changes: 2 additions & 0 deletions test/views/layout2.mab
@@ -0,0 +1,2 @@
h1 "Markaby Layout!"
p { yield }

0 comments on commit 8ce74b3

Please sign in to comment.