mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Add rdoc helper method. Tilt supports RDoc 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
b464e024a8
commit
c248dbac9d
5 changed files with 86 additions and 0 deletions
|
@ -368,6 +368,31 @@ aufzurufen:
|
|||
%h1 Hallo von Haml!
|
||||
%p= textile(:greetings)
|
||||
|
||||
=== RDoc-Templates
|
||||
|
||||
Das rdoc gem wird benötigt um RDoc-Templates rendern zu können:
|
||||
|
||||
## redcloth muss eingebunden werden
|
||||
require "rdoc"
|
||||
|
||||
get '/' do
|
||||
rdoc :index
|
||||
end
|
||||
|
||||
Dieser Code rendert <tt>./views/index.rdoc</tt>.
|
||||
|
||||
Da es weder möglich ist Methoden aufzurufen, noch +locals+ zu übergeben, ist
|
||||
es am sinnvollsten RDoc in Kombination mit einer anderen Template-Engine
|
||||
zu nutzen:
|
||||
|
||||
erb :overview, :locals => { :text => rdoc(:introduction) }
|
||||
|
||||
Es ist auch möglich die +rdoc+ Methode aus anderen Templates heraus
|
||||
aufzurufen:
|
||||
|
||||
%h1 Hallo von Haml!
|
||||
%p= rdoc(:greetings)
|
||||
|
||||
=== Inline-Templates
|
||||
|
||||
get '/' do
|
||||
|
|
22
README.rdoc
22
README.rdoc
|
@ -359,6 +359,28 @@ Note that you may also call the textile method from within other templates:
|
|||
%h1 Hello From Haml!
|
||||
%p= textile(:greetings)
|
||||
|
||||
=== RDoc Templates
|
||||
|
||||
The RDoc gem/library is required to render RDoc templates:
|
||||
|
||||
## You'll need to require rdiscount in your app
|
||||
require "rdoc"
|
||||
|
||||
get '/' do
|
||||
rdoc :index
|
||||
end
|
||||
|
||||
Renders <tt>./views/index.rdoc</tt>.
|
||||
|
||||
It is not possible to call methods from rdoc, nor to pass locals to it. You therefore will usually use it in combination with another rendering engine:
|
||||
|
||||
erb :overview, :locals => { :text => rdoc(:introduction) }
|
||||
|
||||
Note that you may also call the rdoc method from within other templates:
|
||||
|
||||
%h1 Hello From Haml!
|
||||
%p= rdoc(:greetings)
|
||||
|
||||
=== Inline Templates
|
||||
|
||||
get '/' do
|
||||
|
|
|
@ -347,6 +347,10 @@ module Sinatra
|
|||
render :textile, template, options, locals
|
||||
end
|
||||
|
||||
def rdoc(template, options={}, locals={})
|
||||
render :rdoc, template, options, locals
|
||||
end
|
||||
|
||||
private
|
||||
def render(engine, data, options={}, locals={}, &block)
|
||||
# merge app-level options
|
||||
|
|
34
test/rdoc_test.rb
Normal file
34
test/rdoc_test.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
require File.dirname(__FILE__) + '/helper'
|
||||
|
||||
begin
|
||||
require 'rdoc'
|
||||
|
||||
class RdocTest < Test::Unit::TestCase
|
||||
def rdoc_app(&block)
|
||||
mock_app do
|
||||
set :views, File.dirname(__FILE__) + '/views'
|
||||
get '/', &block
|
||||
end
|
||||
get '/'
|
||||
end
|
||||
|
||||
it 'renders inline rdoc strings' do
|
||||
rdoc_app { rdoc '= Hiya' }
|
||||
assert ok?
|
||||
assert_equal "<h1>Hiya</h1>\n", body
|
||||
end
|
||||
|
||||
it 'renders .rdoc files in views path' do
|
||||
rdoc_app { rdoc :hello }
|
||||
assert ok?
|
||||
assert_equal "<h1>Hello From RDoc</h1>\n", body
|
||||
end
|
||||
|
||||
it "raises error if template not found" do
|
||||
mock_app { get('/') { rdoc :no_such_template } }
|
||||
assert_raise(Errno::ENOENT) { get('/') }
|
||||
end
|
||||
end
|
||||
rescue
|
||||
warn "#{$!.to_s}: skipping rdoc tests"
|
||||
end
|
1
test/views/hello.rdoc
Normal file
1
test/views/hello.rdoc
Normal file
|
@ -0,0 +1 @@
|
|||
= Hello From RDoc
|
Loading…
Reference in a new issue