1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Add nokogiri helper method. Tilt supports Nokogiri for quite some time now, but it was not as easy to use as builder, and not documented. Tests and documentation included.

This commit is contained in:
Konstantin Haase 2010-10-10 13:53:43 +02:00
parent 44ab0902e3
commit dd81da11bb
9 changed files with 154 additions and 4 deletions

View file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -350,10 +350,7 @@ module Sinatra
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 @@ module Sinatra
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)

69
test/nokogiri_test.rb Normal file
View file

@ -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 file

@ -0,0 +1 @@
xml.exclaim "You're my boy, #{@name}!"

View file

@ -0,0 +1,3 @@
xml.layout do
xml << yield
end