Permalink
Browse files

add support for yajl templates, merges #450

Signed-off-by: Konstantin Haase <konstantin.mailinglists@googlemail.com>
  • Loading branch information...
1 parent 4490a76 commit d255666d49122d69e0bd42c35370413bbbf311b5 @jamiehodge jamiehodge committed with rkh Jan 10, 2012
Showing with 104 additions and 0 deletions.
  1. +2 −0 CHANGES
  2. +1 −0 Gemfile
  3. +15 −0 README.rdoc
  4. +5 −0 lib/sinatra/base.rb
  5. +1 −0 test/views/hello.yajl
  6. +80 −0 test/yajl_test.rb
View
@@ -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
@@ -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
@@ -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
@@ -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)
View
@@ -0,0 +1 @@
+json = { :yajl => "hello" }
View
@@ -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.