Permalink
Browse files

Adding RABL support

  • Loading branch information...
1 parent 4a07b13 commit 5bf54d517e06e4d81073ee879dde085638c568b6 @jc00ke jc00ke committed Jun 1, 2012
Showing with 149 additions and 0 deletions.
  1. +1 −0 Gemfile
  2. +6 −0 README.de.rdoc
  3. +6 −0 README.es.rdoc
  4. +6 −0 README.fr.rdoc
  5. +13 −0 README.jp.rdoc
  6. +6 −0 README.ko.rdoc
  7. +6 −0 README.rdoc
  8. +6 −0 README.ru.rdoc
  9. +5 −0 lib/sinatra/base.rb
  10. +89 −0 test/rabl_test.rb
  11. +2 −0 test/views/hello.rabl
  12. +3 −0 test/views/layout2.rabl
View
@@ -42,6 +42,7 @@ gem 'maruku'
gem 'creole'
gem 'markaby'
gem 'radius'
+gem 'rabl'
if RUBY_ENGINE == 'jruby'
gem 'nokogiri', '!= 1.5.0'
View
@@ -508,6 +508,12 @@ Nimmt ebenso einen Block für Inline-Templates entgegen (siehe Beispiel).
=== Slim Templates
+Abhängigkeit:: {rabl}[https://github.com/nesquena/rabl]
+Dateierweiterungs:: <tt>.rabl</tt>
+Beispiel:: <tt>rabl :index</tt>
+
+=== Slim Templates
+
Abhängigkeit:: {slim}[http://slim-lang.com/]
Dateierweiterungs:: <tt>.slim</tt>
Beispiel:: <tt>slim :index</tt>
View
@@ -496,6 +496,12 @@ Ejemplos:: <tt>markaby { h1 "Bienvenido!" }</tt>
Además, acepta un bloque con la definición de la plantilla (ver el ejemplo).
+=== Plantillas RABL
+
+Dependencias:: {rabl}[https://github.com/nesquena/rabl]
+Extensiones de Archivo:: <tt>.rabl</tt>
+Ejemplo:: <tt>rabl :index</tt>
+
=== Plantillas Slim
Dependencias:: {slim}[http://slim-lang.com/]
View
@@ -508,6 +508,12 @@ Exemple:: <tt>markaby { h1 "Bienvenue !" }</tt>
Ce moteur accepte également un bloc pour des templates en ligne (voir exemple).
+=== Templates RABL
+
+Dépendances:: {rabl}[https://github.com/nesquena/rabl]
+Extensions de fichier:: <tt>.rabl</tt>
+Exemple:: <tt>rabl :index</tt>
+
=== Templates Slim
Dépendances:: {slim}[http://slim-lang.com/]
View
@@ -426,6 +426,19 @@ Markabyテンプレートを使うにはmarkabyライブラリが必要です:
<tt>./views/index.mab</tt>を表示します。
+=== RABL テンプレート
+
+RABLテンプレートを使うにはrablライブラリが必要です:
+
+ # rablを読み込みます
+ require 'rabl'
+
+ get '/' do
+ rabl :index
+ end
+
+<tt>./views/index.rabl</tt>を表示します。
+
=== Slim テンプレート
Slimテンプレートを使うにはslimライブラリが必要です:
View
@@ -454,6 +454,12 @@ Radius 템플릿에서는 루비 메서드를 호출할 수 없기 때문에,
인라인 템플릿으로 블록을 받을 수도 있음(예제 참조).
+=== RABL 템플릿
+
+의존:: {rabl}[https://github.com/nesquena/rabl]
+파일 확장자:: <tt>.rabl</tt>
+예제:: <tt>rabl :index</tt>
+
=== Slim 템플릿
의존:: {slim}[http://slim-lang.com/]
View
@@ -481,6 +481,12 @@ Example:: <tt>markaby { h1 "Welcome!" }</tt>
It also takes a block for inline templates (see example).
+=== RABL Templates
+
+Dependency:: {rabl}[https://github.com/nesquena/rabl]
+File Extensions:: <tt>.rabl</tt>
+Example:: <tt>rabl :index</tt>
+
=== Slim Templates
Dependency:: {slim}[http://slim-lang.com/]
View
@@ -488,6 +488,12 @@ Thin — это более производительный и функцион
Блок также используется и для встроенных шаблонов (см. пример).
+=== RABL шаблоны
+
+Зависимости:: {rabl}[https://github.com/nesquena/rabl]
+Расширения файлов:: <tt>.rabl</tt>
+Пример:: <tt>rabl :index</tt>
+
=== Slim шаблоны
Зависимости:: {slim}[http://slim-lang.com/]
View
@@ -650,6 +650,11 @@ def yajl(template, options={}, locals={})
render :yajl, template, options, locals
end
+ def rabl(template, options={}, locals={})
+ Rabl.register!
+ render :rabl, template, options, locals
+ end
+
# Calls the given block for every possible template file in views,
# named name.ext, where ext is registered on engine.
def find_template(views, name, engine)
View
@@ -0,0 +1,89 @@
+require File.expand_path('../helper', __FILE__)
+
+begin
+require 'rabl'
+require 'ostruct'
+require 'json'
+require 'active_support/core_ext/hash/conversions'
+
+class RablTest < Test::Unit::TestCase
+ def rabl_app(&block)
+ mock_app {
+ set :views, File.dirname(__FILE__) + '/views'
+ get '/', &block
+ }
+ get '/'
+ end
+
+ it 'renders inline rabl strings' do
+ rabl_app do
+ @foo = OpenStruct.new(:baz => 'w00t')
+ rabl %q{
+ object @foo
+ attributes :baz
+ }
+ end
+ assert ok?
+ assert_equal '{"openstruct":{"baz":"w00t"}}', body
+ end
+ it 'renders .rabl files in views path' do
+ rabl_app do
+ @foo = OpenStruct.new(:bar => 'baz')
+ rabl :hello
+ end
+ assert ok?
+ assert_equal '{"openstruct":{"bar":"baz"}}', body
+ end
+
+ it "renders with file layouts" do
+ rabl_app {
+ @foo = OpenStruct.new(:bar => 'baz')
+ rabl :hello, :layout => :layout2
+ }
+ assert ok?
+ assert_equal '{"qux":{"openstruct":{"bar":"baz"}}}', body
+ end
+
+ it "raises error if template not found" do
+ mock_app {
+ get('/') { rabl :no_such_template }
+ }
+ assert_raise(Errno::ENOENT) { get('/') }
+ end
+
+ it "passes rabl options to the rabl engine" do
+ mock_app do
+ get('/') do
+ @foo = OpenStruct.new(:bar => 'baz')
+ rabl %q{
+ object @foo
+ attributes :bar
+ }, :format => 'xml'
+ end
+ end
+ get '/'
+ assert ok?
+ assert_body '<?xml version="1.0" encoding="UTF-8"?><openstruct><bar>baz</bar></openstruct>'
+ end
+
+ it "passes default rabl options to the rabl engine" do
+ mock_app do
+ set :rabl, :format => 'xml'
+ get('/') do
+ @foo = OpenStruct.new(:bar => 'baz')
+ rabl %q{
+ object @foo
+ attributes :bar
+ }
+ end
+ end
+ get '/'
+ assert ok?
+ assert_body '<?xml version="1.0" encoding="UTF-8"?><openstruct><bar>baz</bar></openstruct>'
+ end
+
+end
+
+rescue LoadError
+ warn "#{$!.to_s}: skipping rabl tests"
+end
@@ -0,0 +1,2 @@
+object @foo
+attributes :bar
@@ -0,0 +1,3 @@
+node(:qux) do
+ ::JSON.parse(yield)
+end

4 comments on commit 5bf54d5

@postmodern
Contributor

Is their a limit on the number of templating languages Sinatra should support? This could end up cluttering Sinatra.

@rkh
Member
rkh commented on 5bf54d5 Jun 23, 2012

I thought about pulling template engines out of base for 2.0.

@postmodern
Contributor

Yeah, something like sinatra-serializers and sinatra-templates.

@jc00ke
Contributor
jc00ke commented on 5bf54d5 Jun 24, 2012

Not a bad idea at all.

Please sign in to comment.