mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
add support for yajl templates, merges #450
Signed-off-by: Konstantin Haase <konstantin.mailinglists@googlemail.com>
This commit is contained in:
parent
4490a76380
commit
d255666d49
6 changed files with 104 additions and 0 deletions
2
CHANGES
2
CHANGES
|
@ -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)
|
||||
|
||||
|
|
1
Gemfile
1
Gemfile
|
@ -42,6 +42,7 @@ gem 'maruku'
|
|||
gem 'creole'
|
||||
gem 'markaby'
|
||||
gem 'radius'
|
||||
gem 'yajl-ruby'
|
||||
|
||||
if RUBY_ENGINE == 'jruby'
|
||||
gem 'nokogiri', '!= 1.5.0'
|
||||
|
|
15
README.rdoc
15
README.rdoc
|
@ -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
|
||||
|
|
|
@ -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
1
test/views/hello.yajl
Normal file
|
@ -0,0 +1 @@
|
|||
json = { :yajl => "hello" }
|
80
test/yajl_test.rb
Normal file
80
test/yajl_test.rb
Normal 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
|
Loading…
Reference in a new issue