From 7cb94f2d3f9e52e6d75ef1cf036f91064f8fc600 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Sun, 12 Sep 2010 15:14:45 +0200 Subject: [PATCH] Add radius helper method. Tilt supports radius 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 | 18 ++++++++++++ README.rdoc | 18 ++++++++++++ lib/sinatra/base.rb | 4 +++ test/radius_test.rb | 59 +++++++++++++++++++++++++++++++++++++++ test/views/hello.radius | 1 + test/views/layout2.radius | 2 ++ 6 files changed, 102 insertions(+) create mode 100644 test/radius_test.rb create mode 100644 test/views/hello.radius create mode 100644 test/views/layout2.radius diff --git a/README.de.rdoc b/README.de.rdoc index 980fdf41..5d833294 100644 --- a/README.de.rdoc +++ b/README.de.rdoc @@ -393,6 +393,24 @@ aufzurufen: %h1 Hallo von Haml! %p= rdoc(:greetings) +=== Radius-Templates + +Das radius gem wird benötigt um Radius-Templates rendern zu können: + + ## radius muss eingebunden werden + require 'radius' + + get '/' do + radius :index + end + +Dieser Code rendert ./views/index.radius. + +Da man aus Radius-Templates heraus keine Methoden (abgesehen von +yield+) +aufrufen kann, will man nahezu in allen Fällen +locals+ übergeben: + + radius :index, :locals => { :key => 'value' } + === Inline-Templates get '/' do diff --git a/README.rdoc b/README.rdoc index 3b8e6206..1c63ad2a 100644 --- a/README.rdoc +++ b/README.rdoc @@ -381,6 +381,24 @@ Note that you may also call the rdoc method from within other templates: %h1 Hello From Haml! %p= rdoc(:greetings) +=== Radius Templates + +The radius gem/library is required to render Radius templates: + + ## You'll need to require radius in your app + require 'radius' + + get '/' do + radius :index + end + +Renders ./views/index.radius. + +Since you cannot call Ruby methods (except for +yield+) from a Radius +template, you almost always want to pass locals to it: + + radius :index, :locals => { :key => 'value' } + === Inline Templates get '/' do diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 807bc0e3..126a375b 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -351,6 +351,10 @@ module Sinatra render :rdoc, template, options, locals end + def radius(template, options={}, locals={}) + render :radius, template, options, locals + end + private def render(engine, data, options={}, locals={}, &block) # merge app-level options diff --git a/test/radius_test.rb b/test/radius_test.rb new file mode 100644 index 00000000..7f43928a --- /dev/null +++ b/test/radius_test.rb @@ -0,0 +1,59 @@ +require File.dirname(__FILE__) + '/helper' + +begin +fail 'Radius broken on 1.9.' if RUBY_VERSION >= '1.9.1' +require 'radius' + +class RadiusTest < Test::Unit::TestCase + def radius_app(&block) + mock_app do + set :views, File.dirname(__FILE__) + '/views' + get '/', &block + end + get '/' + end + + it 'renders inline radius strings' do + radius_app { radius '

Hiya

' } + assert ok? + assert_equal "

Hiya

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

Hello From Radius

\n", body + end + + it "renders with inline layouts" do + mock_app do + layout { "

THIS. IS.

" } + get('/') { radius 'SPARTA' } + end + get '/' + assert ok? + assert_equal "

THIS. IS. SPARTA

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

Radius Layout!

\n

Hello World

\n", body + end + + it "raises error if template not found" do + mock_app { get('/') { radius :no_such_template } } + assert_raise(Errno::ENOENT) { get('/') } + end + + it "allows passing locals" do + radius_app do + radius '', :locals => { :value => 'foo' } + end + assert ok? + assert_equal 'foo', body + end +end +rescue + warn "#{$!.to_s}: skipping radius tests" +end diff --git a/test/views/hello.radius b/test/views/hello.radius new file mode 100644 index 00000000..98e35f37 --- /dev/null +++ b/test/views/hello.radius @@ -0,0 +1 @@ +

Hello From Radius

diff --git a/test/views/layout2.radius b/test/views/layout2.radius new file mode 100644 index 00000000..57a06c04 --- /dev/null +++ b/test/views/layout2.radius @@ -0,0 +1,2 @@ +

Radius Layout!

+