From b464e024a8f90df169fbe46bd7b5877aedc5b17c Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Sat, 11 Sep 2010 14:52:55 +0200 Subject: [PATCH] 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. --- README.de.rdoc | 25 +++++++++++++++++++++++++ README.rdoc | 22 ++++++++++++++++++++++ lib/sinatra/base.rb | 4 ++++ sinatra.gemspec | 1 + test/textile_test.rb | 34 ++++++++++++++++++++++++++++++++++ test/views/hello.textile | 1 + 6 files changed, 87 insertions(+) create mode 100644 test/textile_test.rb create mode 100644 test/views/hello.textile diff --git a/README.de.rdoc b/README.de.rdoc index f03a7825..96ff7ab0 100644 --- a/README.de.rdoc +++ b/README.de.rdoc @@ -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 ./views/index.textile. + +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 diff --git a/README.rdoc b/README.rdoc index b8e489a2..f79dbac5 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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 ./views/index.textile. + +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 diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 714d1428..ff66bfeb 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -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 diff --git a/sinatra.gemspec b/sinatra.gemspec index 0ada0b2e..908f006e 100644 --- a/sinatra.gemspec +++ b/sinatra.gemspec @@ -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" diff --git a/test/textile_test.rb b/test/textile_test.rb new file mode 100644 index 00000000..38df1d2e --- /dev/null +++ b/test/textile_test.rb @@ -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 "

Hiya

", body + end + + it 'renders .textile files in views path' do + textile_app { textile :hello } + assert ok? + assert_equal "

Hello From Textile

", 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 diff --git a/test/views/hello.textile b/test/views/hello.textile new file mode 100644 index 00000000..02686a69 --- /dev/null +++ b/test/views/hello.textile @@ -0,0 +1 @@ +h1. Hello From Textile