1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Add liquid helper method. Tilt supports liquid for quite some time now, but it was not as easy to use as haml or erb, and not documented. Tests and documentation (English and German) included.

This commit is contained in:
Konstantin Haase 2010-09-11 13:53:49 +02:00
parent 99680c7f88
commit 28a3a350bb
7 changed files with 102 additions and 0 deletions

View file

@ -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 file

@ -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 file

@ -335,6 +335,10 @@ module Sinatra
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 file

@ -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"

58
test/liquid_test.rb Normal file
View file

@ -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

1
test/views/hello.liquid Normal file
View file

@ -0,0 +1 @@
<h1>Hello From Liquid</h1>

View file

@ -0,0 +1,2 @@
<h1>Liquid Layout!</h1>
<p>{{ yield }}</p>