1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

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.

This commit is contained in:
Konstantin Haase 2010-09-12 17:00:33 +02:00
parent 8718419b5b
commit 8ce74b3ad2
8 changed files with 94 additions and 3 deletions

View file

@ -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 file

@ -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 file

@ -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 file

@ -355,6 +355,10 @@ module Sinatra
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

1
test/hello.mab Normal file
View file

@ -0,0 +1 @@
h1 "Hello From Markaby"

58
test/markaby_test.rb Normal file
View file

@ -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
test/views/hello.mab Normal file
View file

@ -0,0 +1 @@
h1 "Hello From Markaby"

2
test/views/layout2.mab Normal file
View file

@ -0,0 +1,2 @@
h1 "Markaby Layout!"
p { yield }