Skip to content

Commit

Permalink
add support for yajl templates, merges sinatra#450
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Haase <konstantin.mailinglists@googlemail.com>
  • Loading branch information
jamiehodge authored and rkh committed Jan 10, 2012
1 parent 4490a76 commit d255666
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ gem 'maruku'
gem 'creole'
gem 'markaby'
gem 'radius'
gem 'yajl-ruby'

if RUBY_ENGINE == 'jruby'
gem 'nokogiri', '!= 1.5.0'
Expand Down
15 changes: 15 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ def creole(template, options={}, locals={})
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)
Expand Down
1 change: 1 addition & 0 deletions test/views/hello.yajl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json = { :yajl => "hello" }
80 changes: 80 additions & 0 deletions test/yajl_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d255666

Please sign in to comment.