Skip to content
Browse files

Add nokogiri helper method. Tilt supports Nokogiri for quite some tim…

…e now, but it was not as easy to use as builder, and not documented. Tests and documentation included.
  • Loading branch information...
1 parent 44ab090 commit dd81da11bb7deaa5af5714e415f072b9a1622d2b @rkh rkh committed Oct 10, 2010
Showing with 154 additions and 4 deletions.
  1. +13 −0 README.de.rdoc
  2. +14 −0 README.es.rdoc
  3. +14 −0 README.fr.rdoc
  4. +13 −0 README.jp.rdoc
  5. +13 −0 README.rdoc
  6. +14 −4 lib/sinatra/base.rb
  7. +69 −0 test/nokogiri_test.rb
  8. +1 −0 test/views/hello.nokogiri
  9. +3 −0 test/views/layout2.nokogiri
View
13 README.de.rdoc
@@ -234,6 +234,19 @@ Das buidler gem wird benötigt, um Builder-Templates rendern zu können:
Dieser Code rendert <tt>./views/index.builder</tt>.
+=== Nokogiri-Templates
+
+Das nokogiri gem wird benötigt, um Nokogiri-Templates rendern zu können:
+
+ ## nokogiri muss eingebunden werden
+ require 'nokogiri'
+
+ get '/' do
+ nokogiri :index
+ end
+
+Dieser Code rendert <tt>./views/index.nokogiri</tt>.
+
=== Sass-Templates
Das haml gem wird benötigt, um SASS-Templates rendern zu können:
View
14 README.es.rdoc
@@ -233,6 +233,20 @@ La gem/librería builder es necesaria para renderizar plantillas builder:
Renderiza <tt>./views/index.builder</tt>.
+=== Plantillas Nokogiri
+
+La gem/librería nokogiri es necesaria para renderizar plantillas nokogiri:
+
+ ## Vas a necesitar requerir nokogiri en tu app
+ require 'nokogiri'
+
+ get '/' do
+ content_type 'application/xml', :charset => 'utf-8'
+ nokogiri :index
+ end
+
+Renderiza <tt>./views/index.nokogiri</tt>.
+
=== Plantillas Sass
La gem/librería sass es necesaria para renderizar plantillas Sass:
View
14 README.fr.rdoc
@@ -230,6 +230,20 @@ Le gem builder est nécessaire pour utiliser la fonction de rendu builder:
Utilisera le template: <tt>./views/index.builder</tt>.
+=== Templates Nokogiri
+
+Le gem nokogiri est nécessaire pour utiliser la fonction de rendu nokogiri:
+
+ ## Chargez la bibliothèque nokogiri dans votre application
+ require 'nokogiri'
+
+ get '/' do
+ content_type 'application/xml', :charset => 'utf-8'
+ nokogiri :index
+ end
+
+Utilisera le template: <tt>./views/index.nokogiri</tt>.
+
=== Templates Sass
Le gem sass est nécessaire pour utiliser la fonction de rendu Sass:
View
13 README.jp.rdoc
@@ -159,6 +159,19 @@ builderを使うにはbuilderライブラリが必要です:
<tt>./views/index.builder</tt>を表示します。
+=== 鋸 テンプレート
+
+鋸を使うには鋸ライブラリが必要です:
+
+ ## 鋸を読み込みます
+ require 'nokogiri'
+
+ get '/' do
+ nokogiri :index
+ end
+
+<tt>./views/index.nokogiri</tt>を表示します。
+
=== Sass テンプレート
Sassテンプレートを使うにはsassライブラリが必要です:
View
13 README.rdoc
@@ -230,6 +230,19 @@ The builder gem/library is required to render builder templates:
Renders <tt>./views/index.builder</tt>.
+=== Nokogiri Templates
+
+The nokogiri gem/library is required to render nokogiri templates:
+
+ ## You'll need to require nokogiri in your app
+ require 'nokogiri'
+
+ get '/' do
+ nokogiri :index
+ end
+
+Renders <tt>./views/index.nokogiri</tt>.
+
=== Sass Templates
The sass gem/library is required to render Sass templates:
View
18 lib/sinatra/base.rb
@@ -350,10 +350,7 @@ def less(template, options={}, locals={})
end
def builder(template=nil, options={}, locals={}, &block)
- options[:default_content_type] = :xml
- options, template = template, nil if template.is_a?(Hash)
- template = Proc.new { block } if template.nil?
- render :builder, template, options, locals
+ render_xml(:builder, template, options, locals, &block)
end
def liquid(template, options={}, locals={})
@@ -385,7 +382,20 @@ def coffee(template, options={}, locals={})
render :coffee, template, options, locals
end
+ def nokogiri(template=nil, options={}, locals={}, &block)
+ options[:layout] = false if Tilt::VERSION <= "1.1"
+ render_xml(:nokogiri, template, options, locals, &block)
+ end
+
private
+ # logic shared between builder and nokogiri
+ def render_xml(engine, template, options={}, locals={}, &block)
+ options[:default_content_type] = :xml
+ options, template = template, nil if template.is_a?(Hash)
+ template = Proc.new { block } if template.nil?
+ render engine, template, options, locals
+ end
+
def render(engine, data, options={}, locals={}, &block)
# merge app-level options
options = settings.send(engine).merge(options) if settings.respond_to?(engine)
View
69 test/nokogiri_test.rb
@@ -0,0 +1,69 @@
+require File.dirname(__FILE__) + '/helper'
+
+begin
+require 'nokogiri'
+
+class NokogiriTest < Test::Unit::TestCase
+ def nokogiri_app(&block)
+ mock_app do
+ set :views, File.dirname(__FILE__) + '/views'
+ get '/', &block
+ end
+ get '/'
+ end
+
+ it 'renders inline Nokogiri strings' do
+ nokogiri_app { nokogiri 'xml' }
+ assert ok?
+ assert_equal %{<?xml version="1.0"?>\n}, body
+ end
+
+ it 'renders inline blocks' do
+ nokogiri_app do
+ @name = "Frank & Mary"
+ nokogiri do |xml|
+ xml.couple @name
+ end
+ end
+ assert ok?
+ assert_equal "<?xml version=\"1.0\"?>\n<couple>Frank &amp; Mary</couple>\n", body
+ end
+
+ it 'renders .nokogiri files in views path' do
+ nokogiri_app do
+ @name = "Blue"
+ nokogiri :hello
+ end
+ assert ok?
+ assert_equal %(<?xml version="1.0"?>\n<exclaim>You're my boy, Blue!</exclaim>\n), body
+ end
+
+ it "renders with inline layouts" do
+ next if Tilt::VERSION <= "1.1"
+ mock_app do
+ layout { %(xml.layout { xml << yield }) }
+ get('/') { nokogiri %(xml.em 'Hello World') }
+ end
+ get '/'
+ assert ok?
+ assert_equal "<?xml version=\"1.0\"?>\n<layout>\n <em>Hello World</em>\n</layout>\n", body
+ end
+
+ it "renders with file layouts" do
+ next if Tilt::VERSION <= "1.1"
+ nokogiri_app do
+ @name = "Blue"
+ nokogiri %(xml.em 'Hello World'), :layout => :layout2
+ end
+ assert ok?
+ assert_equal "<?xml version=\"1.0\"?>\n<layout>\n <em>Hello World</em>\n</layout>\n", body
+ end
+
+ it "raises error if template not found" do
+ mock_app { get('/') { nokogiri :no_such_template } }
+ assert_raise(Errno::ENOENT) { get('/') }
+ end
+end
+rescue
+ warn "#{$!.to_s}: skipping nokogiri tests"
+end
View
1 test/views/hello.nokogiri
@@ -0,0 +1 @@
+xml.exclaim "You're my boy, #{@name}!"
View
3 test/views/layout2.nokogiri
@@ -0,0 +1,3 @@
+xml.layout do
+ xml << yield
+end

0 comments on commit dd81da1

Please sign in to comment.
Something went wrong with that request. Please try again.