toolmantim-sinatra-content-for 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.md +52 -0
- data/lib/sinatra/content_for.rb +44 -0
- data/sinatra-content-for.gemspec +34 -0
- data/test/content_for_test.rb +130 -0
- metadata +98 -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.md
ADDED
@@ -0,0 +1,52 @@
|
|
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
|
+
<strong>Note</strong> that with ERB <code>yield_content</code> is called <strong>without</strong>
|
25
|
+
an '=' block (<code><%= %></code>), but with Haml it uses <code>= yield\_content</code>.
|
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
|
+
<code>sinatra/content_for</code> should be enough. If you're writing
|
34
|
+
"classy" apps, then you also need to call
|
35
|
+
<code>helpers Sinatra::ContentFor</code> 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 <code><% yield\_content :scripts\_and\_styles %></code> on
|
42
|
+
your layout, inside the <head> tag, and each view can call
|
43
|
+
<code>content_for</code> setting the appropriate set of tags that should
|
44
|
+
be added to the layout.
|
45
|
+
|
46
|
+
## How is this different from [foca](http://github.com/foca)'s version?
|
47
|
+
|
48
|
+
This code was based on [foca's version](http://github.com/foca), which supports passing block values to the content, which means lazy rendering of the blocks. This plays havoc with anything that assumes that views are rendered before layouts, such as setting instance variables and the [fancyviews](http://github.com/quackingduck/fancyviews) plugin.
|
49
|
+
|
50
|
+
## Credits
|
51
|
+
|
52
|
+
Original code by [foca](http://github.com/foca), inspired on the Ruby on Rails helpers with the same name. Haml support by [macodely](http://github.com/macodely). Modified by me (Tim Lucas) for the above reasons.
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Sinatra
|
2
|
+
module ContentFor
|
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
|
+
def content_for(key, &block)
|
14
|
+
content_blocks[key.to_sym] << begin
|
15
|
+
if respond_to?(:block_is_haml?) && block_is_haml?(block)
|
16
|
+
capture_haml(&block)
|
17
|
+
else
|
18
|
+
block.call
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Render the captured blocks for a given key. For example:
|
24
|
+
#
|
25
|
+
# <head>
|
26
|
+
# <title>Example</title>
|
27
|
+
# <% yield_content :head %>
|
28
|
+
# </head>
|
29
|
+
#
|
30
|
+
# Would render everything you declared with <tt>content_for
|
31
|
+
# :head</tt> before closing the <tt><head></tt> tag.
|
32
|
+
def yield_content(key)
|
33
|
+
content_blocks[key.to_sym].join
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def content_blocks
|
39
|
+
@content_blocks ||= Hash.new {|h,k| h[k] = [] }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
helpers ContentFor
|
44
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "sinatra-content-for"
|
3
|
+
s.version = "0.2.1"
|
4
|
+
s.date = "2009-08-11"
|
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.md
|
30
|
+
sinatra-content-for.gemspec
|
31
|
+
lib/sinatra/content_for.rb
|
32
|
+
test/content_for_test.rb
|
33
|
+
]
|
34
|
+
end
|
@@ -0,0 +1,130 @@
|
|
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
|
+
end
|
81
|
+
|
82
|
+
context 'with haml' do
|
83
|
+
def haml_app(view)
|
84
|
+
mock_app {
|
85
|
+
layout { '= yield_content :foo' }
|
86
|
+
get('/') { haml view }
|
87
|
+
}
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'renders blocks declared with the same key you use when rendering' do
|
91
|
+
haml_app <<-haml_end
|
92
|
+
- content_for :foo do
|
93
|
+
foo
|
94
|
+
haml_end
|
95
|
+
|
96
|
+
get '/'
|
97
|
+
assert ok?
|
98
|
+
assert_equal "foo\n", body
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'does not render a block with a different key' do
|
102
|
+
haml_app <<-haml_end
|
103
|
+
- content_for :bar do
|
104
|
+
bar
|
105
|
+
haml_end
|
106
|
+
|
107
|
+
get '/'
|
108
|
+
assert ok?
|
109
|
+
assert_equal "\n", body
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'renders multiple blocks with the same key' do
|
113
|
+
haml_app <<-haml_end
|
114
|
+
- content_for :foo do
|
115
|
+
foo
|
116
|
+
- content_for :foo do
|
117
|
+
bar
|
118
|
+
- content_for :baz do
|
119
|
+
WON'T RENDER ME
|
120
|
+
- content_for :foo do
|
121
|
+
baz
|
122
|
+
haml_end
|
123
|
+
|
124
|
+
get '/'
|
125
|
+
assert ok?
|
126
|
+
assert_equal "foo\nbar\nbaz\n", body
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: toolmantim-sinatra-content-for
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Nicol\xC3\xA1s Sanguinetti"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-11 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: sinatra
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: contest
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: sr-mg
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: redgreen
|
47
|
+
type: :development
|
48
|
+
version_requirement:
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
description: Small Sinatra extension to add a content_for helper similar to Rails'
|
56
|
+
email: contacto@nicolassanguinetti.info
|
57
|
+
executables: []
|
58
|
+
|
59
|
+
extensions: []
|
60
|
+
|
61
|
+
extra_rdoc_files: []
|
62
|
+
|
63
|
+
files:
|
64
|
+
- .gitignore
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- sinatra-content-for.gemspec
|
68
|
+
- lib/sinatra/content_for.rb
|
69
|
+
- test/content_for_test.rb
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: http://sinatrarb.com
|
72
|
+
licenses:
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: "0"
|
83
|
+
version:
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: "0"
|
89
|
+
version:
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project: sinatra-ditties
|
93
|
+
rubygems_version: 1.3.5
|
94
|
+
signing_key:
|
95
|
+
specification_version: 2
|
96
|
+
summary: Small Sinatra extension to add a content_for helper similar to Rails'
|
97
|
+
test_files: []
|
98
|
+
|