From 8ce74b3ad271bdf072845dcb17eadf4ee3569a6b Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Sun, 12 Sep 2010 17:00:33 +0200 Subject: [PATCH] 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. --- README.de.rdoc | 13 ++++++++++ README.rdoc | 13 ++++++++++ Rakefile | 5 ++-- lib/sinatra/base.rb | 4 +++ test/hello.mab | 1 + test/markaby_test.rb | 58 ++++++++++++++++++++++++++++++++++++++++++ test/views/hello.mab | 1 + test/views/layout2.mab | 2 ++ 8 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 test/hello.mab create mode 100644 test/markaby_test.rb create mode 100644 test/views/hello.mab create mode 100644 test/views/layout2.mab diff --git a/README.de.rdoc b/README.de.rdoc index 5d833294..1fd7e778 100644 --- a/README.de.rdoc +++ b/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 ./views/index.mab. + === Inline-Templates get '/' do diff --git a/README.rdoc b/README.rdoc index 1c63ad2a..5000aab0 100644 --- a/README.rdoc +++ b/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 ./views/index.mab. + === Inline Templates get '/' do diff --git a/Rakefile b/Rakefile index afa31bf9..db31ab9c 100644 --- a/Rakefile +++ b/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| diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 126a375b..bcd8c5b2 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -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 diff --git a/test/hello.mab b/test/hello.mab new file mode 100644 index 00000000..6e0c9cbf --- /dev/null +++ b/test/hello.mab @@ -0,0 +1 @@ +h1 "Hello From Markaby" diff --git a/test/markaby_test.rb b/test/markaby_test.rb new file mode 100644 index 00000000..8f09ab6b --- /dev/null +++ b/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 "

Hiya

", body + end + + it 'renders .markaby files in views path' do + markaby_app { markaby :hello } + assert ok? + assert_equal "

Hello From Markaby

", 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 "

THIS. IS. SPARTA

", body + end + + it "renders with file layouts" do + markaby_app { markaby 'text "Hello World"', :layout => :layout2 } + assert ok? + assert_equal "

Markaby Layout!

Hello World

", 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 diff --git a/test/views/hello.mab b/test/views/hello.mab new file mode 100644 index 00000000..6e0c9cbf --- /dev/null +++ b/test/views/hello.mab @@ -0,0 +1 @@ +h1 "Hello From Markaby" diff --git a/test/views/layout2.mab b/test/views/layout2.mab new file mode 100644 index 00000000..01da339b --- /dev/null +++ b/test/views/layout2.mab @@ -0,0 +1,2 @@ +h1 "Markaby Layout!" +p { yield }