mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Adding RABL support
This commit is contained in:
parent
4a07b13b71
commit
5bf54d517e
12 changed files with 149 additions and 0 deletions
1
Gemfile
1
Gemfile
|
@ -42,6 +42,7 @@ gem 'maruku'
|
|||
gem 'creole'
|
||||
gem 'markaby'
|
||||
gem 'radius'
|
||||
gem 'rabl'
|
||||
|
||||
if RUBY_ENGINE == 'jruby'
|
||||
gem 'nokogiri', '!= 1.5.0'
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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/]
|
||||
|
|
|
@ -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/]
|
||||
|
|
|
@ -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ライブラリが必要です:
|
||||
|
|
|
@ -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/]
|
||||
|
|
|
@ -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/]
|
||||
|
|
|
@ -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/]
|
||||
|
|
|
@ -650,6 +650,11 @@ module Sinatra
|
|||
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)
|
||||
|
|
89
test/rabl_test.rb
Normal file
89
test/rabl_test.rb
Normal file
|
@ -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
|
2
test/views/hello.rabl
Normal file
2
test/views/hello.rabl
Normal file
|
@ -0,0 +1,2 @@
|
|||
object @foo
|
||||
attributes :bar
|
3
test/views/layout2.rabl
Normal file
3
test/views/layout2.rabl
Normal file
|
@ -0,0 +1,3 @@
|
|||
node(:qux) do
|
||||
::JSON.parse(yield)
|
||||
end
|
Loading…
Reference in a new issue