Permalink
Browse files

Add liquid helper method. Tilt supports liquid for quite some time no…

…w, but it was not as easy to use as haml or erb, and not documented. Tests and documentation (English and German) included.
  • Loading branch information...
1 parent 99680c7 commit 28a3a350bba42f911be6d144ca7af45776c778a7 @rkh rkh committed Sep 11, 2010
Showing with 102 additions and 0 deletions.
  1. +18 −0 README.de.rdoc
  2. +18 −0 README.rdoc
  3. +4 −0 lib/sinatra/base.rb
  4. +1 −0 sinatra.gemspec
  5. +58 −0 test/liquid_test.rb
  6. +1 −0 test/views/hello.liquid
  7. +2 −0 test/views/layout2.liquid
View
@@ -300,6 +300,24 @@ Das less gem wird benötigt um Less-Templates rendern zu können:
Dieser Code rendert <tt>./views/stylesheet.less</tt>.
+=== Liquid Templates
+
+Das liquid gem wird benötigt um Liquid-Templates rendern zu können:
+
+ ## liquid muss eingebunden werden
+ require 'liquid'
+
+ get '/' do
+ liquid :index
+ end
+
+Dieser Code rendert <tt>./views/index.liquid</tt>.
+
+Da man aus Liquid-Templates heraus keine Methoden (abgesehen von +yield+)
+aufrufen kann, will man nahezu in allen Fällen +locals+ übergeben:
+
+ liquid :index, :locals => { :key => 'value' }
+
=== Inline Templates
get '/' do
View
@@ -296,6 +296,24 @@ The less gem/library is required to render Less templates:
Renders <tt>./views/stylesheet.less</tt>.
+=== Liquid Templates
+
+The liquid gem/library is required to render Liquid templates:
+
+ ## You'll need to require liquid in your app
+ require 'liquid'
+
+ get '/' do
+ liquid :index
+ end
+
+Renders <tt>./views/index.liquid</tt>.
+
+Since you cannot call Ruby methods (except for +yield+) from a Liquid
+template, you almost always want to pass locals to it:
+
+ liquid :index, :locals => { :key => 'value' }
+
=== Inline Templates
get '/' do
View
@@ -335,6 +335,10 @@ def builder(template=nil, options={}, locals={}, &block)
render :builder, template, options, locals
end
+ def liquid(template, options={}, locals={})
+ render :liquid, template, options, locals
+ end
+
private
def render(engine, data, options={}, locals={}, &block)
# merge app-level options
View
@@ -86,6 +86,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'builder'
s.add_development_dependency 'erubis'
s.add_development_dependency 'less'
+ s.add_development_dependency 'liquid'
s.has_rdoc = true
s.homepage = "http://sinatra.rubyforge.org"
View
@@ -0,0 +1,58 @@
+require File.dirname(__FILE__) + '/helper'
+
+begin
+require 'liquid'
+
+class LiquidTest < Test::Unit::TestCase
+ def liquid_app(&block)
+ mock_app do
+ set :views, File.dirname(__FILE__) + '/views'
+ get '/', &block
+ end
+ get '/'
+ end
+
+ it 'renders inline liquid strings' do
+ liquid_app { liquid '<h1>Hiya</h1>' }
+ assert ok?
+ assert_equal "<h1>Hiya</h1>", body
+ end
+
+ it 'renders .liquid files in views path' do
+ liquid_app { liquid :hello }
+ assert ok?
+ assert_equal "<h1>Hello From Liquid</h1>\n", body
+ end
+
+ it "renders with inline layouts" do
+ mock_app do
+ layout { "<h1>THIS. IS. {{ yield }}</h1>" }
+ get('/') { liquid '<EM>SPARTA</EM>' }
+ end
+ get '/'
+ assert ok?
+ assert_equal "<h1>THIS. IS. <EM>SPARTA</EM></h1>", body
+ end
+
+ it "renders with file layouts" do
+ liquid_app { liquid 'Hello World', :layout => :layout2 }
+ assert ok?
+ assert_equal "<h1>Liquid Layout!</h1>\n<p>Hello World</p>\n", body
+ end
+
+ it "raises error if template not found" do
+ mock_app { get('/') { liquid :no_such_template } }
+ assert_raise(Errno::ENOENT) { get('/') }
+ end
+
+ it "allows passing locals" do
+ liquid_app do
+ liquid '{{ value }}', :locals => { :value => 'foo' }
+ end
+ assert ok?
+ assert_equal 'foo', body
+ end
+end
+rescue
+ warn "#{$!.to_s}: skipping liquid tests"
+end
View
@@ -0,0 +1 @@
+<h1>Hello From Liquid</h1>
@@ -0,0 +1,2 @@
+<h1>Liquid Layout!</h1>
+<p>{{ yield }}</p>

0 comments on commit 28a3a35

Please sign in to comment.