mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
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:
parent
970169b1fb
commit
b464e024a8
6 changed files with 87 additions and 0 deletions
|
@ -343,6 +343,31 @@ aufzurufen:
|
||||||
%h1 Hallo von Haml!
|
%h1 Hallo von Haml!
|
||||||
%p= markdown(:greetings)
|
%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
|
=== Inline-Templates
|
||||||
|
|
||||||
get '/' do
|
get '/' do
|
||||||
|
|
22
README.rdoc
22
README.rdoc
|
@ -337,6 +337,28 @@ Note that you may also call the markdown method from within other templates:
|
||||||
%h1 Hello From Haml!
|
%h1 Hello From Haml!
|
||||||
%p= markdown(:greetings)
|
%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
|
=== Inline Templates
|
||||||
|
|
||||||
get '/' do
|
get '/' do
|
||||||
|
|
|
@ -343,6 +343,10 @@ module Sinatra
|
||||||
render :markdown, template, options, locals
|
render :markdown, template, options, locals
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def textile(template, options={}, locals={})
|
||||||
|
render :textile, template, options, locals
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def render(engine, data, options={}, locals={}, &block)
|
def render(engine, data, options={}, locals={}, &block)
|
||||||
# merge app-level options
|
# merge app-level options
|
||||||
|
|
|
@ -88,6 +88,7 @@ Gem::Specification.new do |s|
|
||||||
s.add_development_dependency 'less'
|
s.add_development_dependency 'less'
|
||||||
s.add_development_dependency 'liquid'
|
s.add_development_dependency 'liquid'
|
||||||
s.add_development_dependency 'rdiscount'
|
s.add_development_dependency 'rdiscount'
|
||||||
|
s.add_development_dependency 'RedCloth'
|
||||||
|
|
||||||
s.has_rdoc = true
|
s.has_rdoc = true
|
||||||
s.homepage = "http://sinatra.rubyforge.org"
|
s.homepage = "http://sinatra.rubyforge.org"
|
||||||
|
|
34
test/textile_test.rb
Normal file
34
test/textile_test.rb
Normal 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
1
test/views/hello.textile
Normal file
|
@ -0,0 +1 @@
|
||||||
|
h1. Hello From Textile
|
Loading…
Reference in a new issue