sinatra-content-for2 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/LICENSE +22 -0
- data/README.rdoc +49 -0
- data/lib/sinatra/content_for2.rb +65 -0
- data/sinatra-content-for2.gemspec +35 -0
- data/test/content_for_test.rb +156 -0
- metadata +127 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2008-2009 Nicolas Sanguinetti, entp.com
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
'Software'), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
= ContentFor
|
2
|
+
|
3
|
+
Small extension for the Sinatra[http://sinatrarb.com] web framework
|
4
|
+
that allows you to use the following helpers in your views:
|
5
|
+
|
6
|
+
<% content_for :some_key do %>
|
7
|
+
<chunk of="html">...</chunk>
|
8
|
+
<% end %>
|
9
|
+
|
10
|
+
<% yield_content :some_key %>
|
11
|
+
|
12
|
+
This allows you to capture blocks inside views to be rendered later
|
13
|
+
in this request. For example, to populate different parts of your
|
14
|
+
layout from your view.
|
15
|
+
|
16
|
+
When using this with the Haml rendering engine, you should do the
|
17
|
+
following:
|
18
|
+
|
19
|
+
- content_for :some_key do
|
20
|
+
%chunk{ :of => "html" } ...
|
21
|
+
|
22
|
+
= yield_content :some_key
|
23
|
+
|
24
|
+
<b>Note</b> that with ERB <tt>yield_content</tt> is called <b>without</b>
|
25
|
+
an '=' block (<tt><%= %></tt>), but with Haml it uses <tt>= yield_content</tt>.
|
26
|
+
|
27
|
+
Using an '=' block in ERB will output the content twice for each block,
|
28
|
+
so if you have problems with that, make sure to check for this.
|
29
|
+
|
30
|
+
== Usage
|
31
|
+
|
32
|
+
If you're writing "classic" style apps, then requring
|
33
|
+
<tt>sinatra/content_for</tt> should be enough. If you're writing
|
34
|
+
"classy" apps, then you also need to call
|
35
|
+
<tt>helpers Sinatra::ContentFor</tt> in your app definition.
|
36
|
+
|
37
|
+
== And how is this useful?
|
38
|
+
|
39
|
+
For example, some of your views might need a few javascript tags and
|
40
|
+
stylesheets, but you don't want to force this files in all your pages.
|
41
|
+
Then you can put <tt><% yield_content :scripts_and_styles %></tt> on
|
42
|
+
your layout, inside the <head> tag, and each view can call
|
43
|
+
<tt>content_for</tt> setting the appropriate set of tags that should
|
44
|
+
be added to the layout.
|
45
|
+
|
46
|
+
== Credits
|
47
|
+
|
48
|
+
Code by foca[http://github.com/foca], inspired on the Ruby on Rails
|
49
|
+
helpers with the same name. Haml support by mattly[http://github.com/mattly].
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Sinatra
|
2
|
+
module ContentFor2
|
3
|
+
# Capture a block of content to be rendered later. For example:
|
4
|
+
#
|
5
|
+
# <% content_for :head do %>
|
6
|
+
# <script type="text/javascript" src="/foo.js"></script>
|
7
|
+
# <% end %>
|
8
|
+
#
|
9
|
+
# You can call +content_for+ multiple times with the same key
|
10
|
+
# (in the example +:head+), and when you render the blocks for
|
11
|
+
# that key all of them will be rendered, in the same order you
|
12
|
+
# captured them.
|
13
|
+
#
|
14
|
+
# Your blocks can also receive values, which are passed to them
|
15
|
+
# by <tt>yield_content</tt>
|
16
|
+
def content_for(key, &block)
|
17
|
+
content_blocks[key.to_sym] << block if block_given?
|
18
|
+
end
|
19
|
+
|
20
|
+
# Render the captured blocks for a given key. For example:
|
21
|
+
#
|
22
|
+
# <head>
|
23
|
+
# <title>Example</title>
|
24
|
+
# <% yield_content :head %>
|
25
|
+
# </head>
|
26
|
+
#
|
27
|
+
# Would render everything you declared with <tt>content_for
|
28
|
+
# :head</tt> before closing the <tt><head></tt> tag.
|
29
|
+
#
|
30
|
+
# You can also pass values to the content blocks by passing them
|
31
|
+
# as arguments after the key:
|
32
|
+
#
|
33
|
+
# <% yield_content :head, 1, 2 %>
|
34
|
+
#
|
35
|
+
# Would pass <tt>1</tt> and <tt>2</tt> to all the blocks registered
|
36
|
+
# for <tt>:head</tt>.
|
37
|
+
#
|
38
|
+
# *NOTICE* that you call this without an <tt>=</tt> sign. IE,
|
39
|
+
# in a <tt><% %></tt> block, and not in a <tt><%= %></tt> block.
|
40
|
+
#
|
41
|
+
# *NOTICE*
|
42
|
+
# if you call from erubis, you call this with <tt>=</tt> sign.IE,
|
43
|
+
#in a <tt><%= %></tt> block, and not in a <tt><% %></tt> block.
|
44
|
+
def yield_content(key, *args)
|
45
|
+
content_blocks[key.to_sym].map do |content|
|
46
|
+
if respond_to?(:block_is_haml?) && block_is_haml?(content)
|
47
|
+
capture_haml(*args, &content)
|
48
|
+
elsif Erubis::VERSION # this line may cause trouble..
|
49
|
+
content.binding.eval ' _buf = "" '
|
50
|
+
content.call(*args)
|
51
|
+
else
|
52
|
+
content.call
|
53
|
+
end
|
54
|
+
end.join
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def content_blocks
|
60
|
+
@content_blocks ||= Hash.new {|h,k| h[k] = [] }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
helpers ContentFor2
|
65
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "sinatra-content-for2"
|
3
|
+
s.version = "0.2.1"
|
4
|
+
s.date = "2010-12-03"
|
5
|
+
|
6
|
+
s.description = "Small Sinatra extension to add a content_for helper similar to Rails'"
|
7
|
+
s.summary = "Small Sinatra extension to add a content_for helper similar to Rails'"
|
8
|
+
s.homepage = "http://sinatrarb.com"
|
9
|
+
|
10
|
+
s.authors = ["Nicolás Sanguinetti"]
|
11
|
+
s.email = "contacto@nicolassanguinetti.info"
|
12
|
+
|
13
|
+
s.require_paths = ["lib"]
|
14
|
+
s.rubyforge_project = "sinatra-ditties"
|
15
|
+
s.has_rdoc = true
|
16
|
+
s.rubygems_version = "1.3.1"
|
17
|
+
|
18
|
+
s.add_dependency "sinatra"
|
19
|
+
|
20
|
+
if s.respond_to?(:add_development_dependency)
|
21
|
+
s.add_development_dependency "contest"
|
22
|
+
s.add_development_dependency "sr-mg"
|
23
|
+
s.add_development_dependency "redgreen"
|
24
|
+
end
|
25
|
+
|
26
|
+
s.files = %w[
|
27
|
+
.gitignore
|
28
|
+
LICENSE
|
29
|
+
README.rdoc
|
30
|
+
sinatra-content-for2.gemspec
|
31
|
+
lib/sinatra/content_for2.rb
|
32
|
+
test/content_for_test.rb
|
33
|
+
]
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,156 @@
|
|
1
|
+
ENV['RACK_ENV'] = 'test'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'rack'
|
5
|
+
rescue LoadError
|
6
|
+
require 'rubygems'
|
7
|
+
require 'rack'
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'contest'
|
11
|
+
require 'sinatra/test'
|
12
|
+
require 'haml'
|
13
|
+
|
14
|
+
begin
|
15
|
+
require 'redgreen'
|
16
|
+
rescue LoadError
|
17
|
+
end
|
18
|
+
|
19
|
+
require File.dirname(__FILE__) + '/../lib/sinatra/content_for'
|
20
|
+
|
21
|
+
Sinatra::Base.set :environment, :test
|
22
|
+
|
23
|
+
module Sinatra
|
24
|
+
class Base
|
25
|
+
set :environment, :test
|
26
|
+
helpers ContentFor
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Test::Unit::TestCase
|
31
|
+
include Sinatra::Test
|
32
|
+
|
33
|
+
class << self
|
34
|
+
alias_method :it, :test
|
35
|
+
end
|
36
|
+
|
37
|
+
def mock_app(base=Sinatra::Base, &block)
|
38
|
+
@app = Sinatra.new(base, &block)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class ContentForTest < Test::Unit::TestCase
|
43
|
+
context 'using erb' do
|
44
|
+
def erb_app(view)
|
45
|
+
mock_app {
|
46
|
+
layout { '<% yield_content :foo %>' }
|
47
|
+
get('/') { erb view }
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'renders blocks declared with the same key you use when rendering' do
|
52
|
+
erb_app '<% content_for :foo do %>foo<% end %>'
|
53
|
+
|
54
|
+
get '/'
|
55
|
+
assert ok?
|
56
|
+
assert_equal 'foo', body
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'does not render a block with a different key' do
|
60
|
+
erb_app '<% content_for :bar do %>bar<% end %>'
|
61
|
+
|
62
|
+
get '/'
|
63
|
+
assert ok?
|
64
|
+
assert_equal '', body
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'renders multiple blocks with the same key' do
|
68
|
+
erb_app <<-erb_snippet
|
69
|
+
<% content_for :foo do %>foo<% end %>
|
70
|
+
<% content_for :foo do %>bar<% end %>
|
71
|
+
<% content_for :baz do %>WON'T RENDER ME<% end %>
|
72
|
+
<% content_for :foo do %>baz<% end %>
|
73
|
+
erb_snippet
|
74
|
+
|
75
|
+
get '/'
|
76
|
+
assert ok?
|
77
|
+
assert_equal 'foobarbaz', body
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'passes values to the blocks' do
|
81
|
+
mock_app {
|
82
|
+
layout { '<% yield_content :foo, 1, 2 %>' }
|
83
|
+
get('/') { erb '<% content_for :foo do |a, b| %><i><%= a %></i> <%= b %><% end %>' }
|
84
|
+
}
|
85
|
+
|
86
|
+
get '/'
|
87
|
+
assert ok?
|
88
|
+
assert_equal '<i>1</i> 2', body
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'with haml' do
|
93
|
+
def haml_app(view)
|
94
|
+
mock_app {
|
95
|
+
layout { '= yield_content :foo' }
|
96
|
+
get('/') { haml view }
|
97
|
+
}
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'renders blocks declared with the same key you use when rendering' do
|
101
|
+
haml_app <<-haml_end
|
102
|
+
- content_for :foo do
|
103
|
+
foo
|
104
|
+
haml_end
|
105
|
+
|
106
|
+
get '/'
|
107
|
+
assert ok?
|
108
|
+
assert_equal "foo\n", body
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'does not render a block with a different key' do
|
112
|
+
haml_app <<-haml_end
|
113
|
+
- content_for :bar do
|
114
|
+
bar
|
115
|
+
haml_end
|
116
|
+
|
117
|
+
get '/'
|
118
|
+
assert ok?
|
119
|
+
assert_equal "\n", body
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'renders multiple blocks with the same key' do
|
123
|
+
haml_app <<-haml_end
|
124
|
+
- content_for :foo do
|
125
|
+
foo
|
126
|
+
- content_for :foo do
|
127
|
+
bar
|
128
|
+
- content_for :baz do
|
129
|
+
WON'T RENDER ME
|
130
|
+
- content_for :foo do
|
131
|
+
baz
|
132
|
+
haml_end
|
133
|
+
|
134
|
+
get '/'
|
135
|
+
assert ok?
|
136
|
+
assert_equal "foo\nbar\nbaz\n", body
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'passes values to the blocks' do
|
140
|
+
mock_app {
|
141
|
+
layout { '= yield_content :foo, 1, 2' }
|
142
|
+
get('/') {
|
143
|
+
haml <<-haml_end
|
144
|
+
- content_for :foo do |a, b|
|
145
|
+
%i= a
|
146
|
+
=b
|
147
|
+
haml_end
|
148
|
+
}
|
149
|
+
}
|
150
|
+
|
151
|
+
get '/'
|
152
|
+
assert ok?
|
153
|
+
assert_equal "<i>1</i>\n2\n", body
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-content-for2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "Nicol\xC3\xA1s Sanguinetti"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-03 00:00:00 +03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: sinatra
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: contest
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: sr-mg
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: redgreen
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
type: :development
|
76
|
+
version_requirements: *id004
|
77
|
+
description: Small Sinatra extension to add a content_for helper similar to Rails'
|
78
|
+
email: contacto@nicolassanguinetti.info
|
79
|
+
executables: []
|
80
|
+
|
81
|
+
extensions: []
|
82
|
+
|
83
|
+
extra_rdoc_files: []
|
84
|
+
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- LICENSE
|
88
|
+
- README.rdoc
|
89
|
+
- sinatra-content-for2.gemspec
|
90
|
+
- lib/sinatra/content_for2.rb
|
91
|
+
- test/content_for_test.rb
|
92
|
+
has_rdoc: true
|
93
|
+
homepage: http://sinatrarb.com
|
94
|
+
licenses: []
|
95
|
+
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
hash: 3
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
version: "0"
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
hash: 3
|
116
|
+
segments:
|
117
|
+
- 0
|
118
|
+
version: "0"
|
119
|
+
requirements: []
|
120
|
+
|
121
|
+
rubyforge_project: sinatra-ditties
|
122
|
+
rubygems_version: 1.3.7
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: Small Sinatra extension to add a content_for helper similar to Rails'
|
126
|
+
test_files: []
|
127
|
+
|