add support for yajl templates, merges #450

Signed-off-by: Konstantin Haase <konstantin.mailinglists@googlemail.com>
This commit is contained in:
Jamie Hodge 2012-01-10 09:15:06 +01:00 committed by Konstantin Haase
parent 4490a76380
commit d255666d49
6 changed files with 104 additions and 0 deletions

View File

@ -1,5 +1,7 @@
= 1.4.0 / Not Yet Released
* Add support for Yajl templates. (Jamie Hodge)
* No longer include Sinatra::Delegator in Object, instead extend the main
object only. (Konstantin Haase)

View File

@ -42,6 +42,7 @@ gem 'maruku'
gem 'creole'
gem 'markaby'
gem 'radius'
gem 'yajl-ruby'
if RUBY_ENGINE == 'jruby'
gem 'nokogiri', '!= 1.5.0'

View File

@ -514,6 +514,21 @@ Dependency:: {coffee-script}[https://github.com/josh/ruby-coffee-script]
File Extensions:: <tt>.coffee</tt>
Example:: <tt>coffee :index</tt>
=== Yajl Templates
Dependency:: {yajl-ruby}[https://github.com/brianmario/yajl-ruby]
File Extensions:: <tt>.yajl</tt>
Example:: <tt>yajl :index, :locals => { :key => 'qux' }, :callback => 'present', :variable => 'resource' </tt>
The template source is evaluated as a Ruby string, and the resulting json variable is converted #to_json.
json = { :foo => 'bar' }
json[:baz] = key
The <tt>:callback</tt> and <tt>:variable</tt> options can be used to decorate the rendered object.
var resource = {"foo":"bar","baz":"qux"}; present(resource);
=== Embedded Templates
get '/' do

View File

@ -603,6 +603,11 @@ module Sinatra
render :creole, template, options, locals
end
def yajl(template, options={}, locals={})
options[:default_content_type] = :json
render :yajl, 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)

1
test/views/hello.yajl Normal file
View File

@ -0,0 +1 @@
json = { :yajl => "hello" }

80
test/yajl_test.rb Normal file
View File

@ -0,0 +1,80 @@
require File.expand_path('../helper', __FILE__)
begin
require 'yajl'
class YajlTest < Test::Unit::TestCase
def yajl_app(&block)
mock_app {
set :views, File.dirname(__FILE__) + '/views'
get '/', &block
}
get '/'
end
it 'renders inline Yajl strings' do
yajl_app { yajl 'json = { :foo => "bar" }' }
assert ok?
assert_body '{"foo":"bar"}'
end
it 'renders .yajl files in views path' do
yajl_app { yajl :hello }
assert ok?
assert_body '{"yajl":"hello"}'
end
it 'raises error if template not found' do
mock_app {
get('/') { yajl :no_such_template }
}
assert_raise(Errno::ENOENT) { get('/') }
end
it 'accepts a :locals option' do
yajl_app {
locals = { :object => { :foo => 'bar' } }
yajl 'json = object', :locals => locals
}
assert ok?
assert_body '{"foo":"bar"}'
end
it 'accepts a :scope option' do
yajl_app {
scope = { :object => { :foo => 'bar' } }
yajl 'json = self[:object]', :scope => scope
}
assert ok?
assert_body '{"foo":"bar"}'
end
it 'decorates the json with a callback' do
yajl_app {
yajl 'json = { :foo => "bar" }', { :callback => 'baz' }
}
assert ok?
assert_body 'baz({"foo":"bar"});'
end
it 'decorates the json with a variable' do
yajl_app {
yajl 'json = { :foo => "bar" }', { :variable => 'qux' }
}
assert ok?
assert_body 'var qux = {"foo":"bar"};'
end
it 'decorates the json with a callback and a variable' do
yajl_app {
yajl 'json = { :foo => "bar" }',
{ :callback => 'baz', :variable => 'qux' }
}
assert ok?
assert_body 'var qux = {"foo":"bar"}; baz(qux);'
end
end
rescue LoadError
warn "#{$!.to_s}: skipping yajl tests"
end