Add textile helper method. Tilt supports textile 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 14:52:55 +02:00
parent 970169b1fb
commit b464e024a8
6 changed files with 87 additions and 0 deletions

View File

@ -343,6 +343,31 @@ aufzurufen:
%h1 Hallo von Haml!
%p= markdown(:greetings)
=== Textile-Templates
Das RedCloth gem wird benötigt um Textile-Templates rendern zu können:
## redcloth muss eingebunden werden
require "redcloth"
get '/' do
textile :index
end
Dieser Code rendert <tt>./views/index.textile</tt>.
Da es weder möglich ist Methoden aufzurufen, noch +locals+ zu übergeben, ist
es am sinnvollsten Textile in Kombination mit einer anderen Template-Engine
zu nutzen:
erb :overview, :locals => { :text => textile(:introduction) }
Es ist auch möglich die +textile+ Methode aus anderen Templates heraus
aufzurufen:
%h1 Hallo von Haml!
%p= textile(:greetings)
=== Inline-Templates
get '/' do

View File

@ -337,6 +337,28 @@ Note that you may also call the markdown method from within other templates:
%h1 Hello From Haml!
%p= markdown(:greetings)
=== Textile Templates
The RedCloth gem/library is required to render Textile templates:
## You'll need to require rdiscount in your app
require "redcloth"
get '/' do
textile :index
end
Renders <tt>./views/index.textile</tt>.
It is not possible to call methods from textile, nor to pass locals to it. You therefore will usually use it in combination with another rendering engine:
erb :overview, :locals => { :text => textile(:introduction) }
Note that you may also call the textile method from within other templates:
%h1 Hello From Haml!
%p= textile(:greetings)
=== Inline Templates
get '/' do

View File

@ -343,6 +343,10 @@ module Sinatra
render :markdown, template, options, locals
end
def textile(template, options={}, locals={})
render :textile, template, options, locals
end
private
def render(engine, data, options={}, locals={}, &block)
# merge app-level options

View File

@ -88,6 +88,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'less'
s.add_development_dependency 'liquid'
s.add_development_dependency 'rdiscount'
s.add_development_dependency 'RedCloth'
s.has_rdoc = true
s.homepage = "http://sinatra.rubyforge.org"

34
test/textile_test.rb Normal file
View File

@ -0,0 +1,34 @@
require File.dirname(__FILE__) + '/helper'
begin
require 'redcloth'
class TextileTest < Test::Unit::TestCase
def textile_app(&block)
mock_app do
set :views, File.dirname(__FILE__) + '/views'
get '/', &block
end
get '/'
end
it 'renders inline textile strings' do
textile_app { textile 'h1. Hiya' }
assert ok?
assert_equal "<h1>Hiya</h1>", body
end
it 'renders .textile files in views path' do
textile_app { textile :hello }
assert ok?
assert_equal "<h1>Hello From Textile</h1>", body
end
it "raises error if template not found" do
mock_app { get('/') { textile :no_such_template } }
assert_raise(Errno::ENOENT) { get('/') }
end
end
rescue
warn "#{$!.to_s}: skipping textile tests"
end

1
test/views/hello.textile Normal file
View File

@ -0,0 +1 @@
h1. Hello From Textile