Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adding RABL support #521

Merged
merged 2 commits into from

4 participants

@jc00ke

I tried updating the READMEs as best I could. I'd be happy to make changes.

The one thing I noticed that I'm not thrilled on is the ActiveSupport dependency I introduced to test the XML options. I suppose since AS is a dependency of RABL it's no big deal. Suggestions?

@travisbot

This pull request fails (merged 5bf54d5 into 4a07b13).

@jc00ke

A bug in JRuby? ping @headius

@rkh
Owner
rkh commented

Maybe just disable RABL on JRuby for now? (I do that in the Gemfile, usually)

@jc00ke

Yeah, looking at RABL's .travis.yml file they don't test on JRuby, so I'll disable shortly.

@jc00ke jc00ke Don't install RABL on JRuby
JRuby is throwing a LocalJumpError in a RABL test, so disable for now.
Issue has been filed jruby/jruby#200
15998be
@travisbot

This pull request passes (merged 15998be into 4a07b13).

@jc00ke

ping @rkh

@rkh
Owner

This pull request cannot be automatically merged.

Will manually merge it later.

@jc00ke

Thanks!

@rkh rkh merged commit 15998be into from
@postmodern

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

Owner

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

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

Not a bad idea at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 1, 2012
  1. @jc00ke

    Adding RABL support

    jc00ke authored
Commits on Jun 6, 2012
  1. @jc00ke

    Don't install RABL on JRuby

    jc00ke authored
    JRuby is throwing a LocalJumpError in a RABL test, so disable for now.
    Issue has been filed jruby/jruby#200
This page is out of date. Refresh to see the latest.
View
1  Gemfile
@@ -42,6 +42,7 @@ gem 'maruku'
gem 'creole'
gem 'markaby'
gem 'radius'
+gem 'rabl' unless RUBY_ENGINE == 'jruby'
if RUBY_ENGINE == 'jruby'
gem 'nokogiri', '!= 1.5.0'
View
6 README.de.rdoc
@@ -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
6 README.es.rdoc
@@ -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
6 README.fr.rdoc
@@ -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
13 README.jp.rdoc
@@ -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
6 README.ko.rdoc
@@ -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
6 README.rdoc
@@ -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
6 README.ru.rdoc
@@ -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
5 lib/sinatra/base.rb
@@ -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
89 test/rabl_test.rb
@@ -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
View
2  test/views/hello.rabl
@@ -0,0 +1,2 @@
+object @foo
+attributes :bar
View
3  test/views/layout2.rabl
@@ -0,0 +1,3 @@
+node(:qux) do
+ ::JSON.parse(yield)
+end
Something went wrong with that request. Please try again.