sinatra-filler 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/TODO +4 -0
- data/lib/sinatra/sinatra_filler.rb +93 -0
- data/lib/sinatra/version.rb +5 -0
- data/sinatra-filler.gemspec +23 -0
- data/test/content_for_test.rb +156 -0
- metadata +119 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
module Sinatra
|
2
|
+
module Filler
|
3
|
+
# Capture a block of filler to be rendered later. For example:
|
4
|
+
#
|
5
|
+
# <% filler_for :head do %>
|
6
|
+
# <script type="text/javascript" src="/foo.js"></script>
|
7
|
+
# <% end %>
|
8
|
+
#
|
9
|
+
# You can call +filler_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_filler</tt>
|
16
|
+
def filler_for(key, &block)
|
17
|
+
filler_blocks[key.to_sym] << block
|
18
|
+
end
|
19
|
+
|
20
|
+
def filler_for?(key)
|
21
|
+
filler_blocks.has_key?(key.to_sym)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Render the captured blocks for a given key. For example:
|
25
|
+
#
|
26
|
+
# <head>
|
27
|
+
# <title>Example</title>
|
28
|
+
# <% output_filler_for :head %>
|
29
|
+
# </head>
|
30
|
+
#
|
31
|
+
# Would render everything you declared with <tt>filler_for
|
32
|
+
# :head</tt> before closing the <tt><head></tt> tag.
|
33
|
+
#
|
34
|
+
# You can also pass values to the filler blocks by passing them
|
35
|
+
# as arguments after the key:
|
36
|
+
#
|
37
|
+
# <% yield_filler :head %>
|
38
|
+
def output_filler_for(key)
|
39
|
+
filler_blocks[key.to_sym].map do |content|
|
40
|
+
content.call
|
41
|
+
end.join
|
42
|
+
end
|
43
|
+
|
44
|
+
# Capture title in templates:
|
45
|
+
#
|
46
|
+
# <% title "Sinatra is awesome!" %>
|
47
|
+
#
|
48
|
+
# Use the captured block in layouts:
|
49
|
+
#
|
50
|
+
# <head>
|
51
|
+
# <title><%= filler_for?(:title) ? output_filler_for(:title) : "Untitled" %></title>
|
52
|
+
# </head>
|
53
|
+
# <body>
|
54
|
+
# <% if filler_for?(:title) && show_title? %>
|
55
|
+
# <h1><%= output_filler_for(:title) %></h1>
|
56
|
+
# <% end %>
|
57
|
+
def title(page_title, show_title = true)
|
58
|
+
filler_for(:title) { page_title.to_s }
|
59
|
+
@show_title = show_title
|
60
|
+
end
|
61
|
+
|
62
|
+
def show_title?
|
63
|
+
@show_title
|
64
|
+
end
|
65
|
+
|
66
|
+
# Capture content in templates:
|
67
|
+
#
|
68
|
+
# <% javascripts "/js/application.js" %>
|
69
|
+
# <% stylesheets "/css/application.css" %>
|
70
|
+
#
|
71
|
+
# Use the captured blocks in layouts:
|
72
|
+
#
|
73
|
+
# <head>
|
74
|
+
# <%= output_filler_for(:head) %>
|
75
|
+
# </head>
|
76
|
+
def stylesheets(*args)
|
77
|
+
filler_for(:head) { stylesheet_link_tag(*args) }
|
78
|
+
end
|
79
|
+
|
80
|
+
def javascripts(*args)
|
81
|
+
filler_for(:head) { javascript_script_tag(*args) }
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def filler_blocks
|
87
|
+
@filler_blocks ||= Hash.new {|h,k| h[k] = [] }
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
helpers Filler
|
93
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "sinatra/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "sinatra-filler"
|
7
|
+
s.version = Sinatra::Filler::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Włodek Bzyl"]
|
10
|
+
s.email = ["matwb@ug.edu.pl"]
|
11
|
+
s.homepage = "http://www.sinatrarb.com/"
|
12
|
+
s.summary = %q{Small Sinatra extension to add several helpers methods}
|
13
|
+
s.description = %q{Small Sinatra extension to add several helpers methods: filler_for, stylesheets, javascripts}
|
14
|
+
|
15
|
+
s.rubyforge_project = "sinatra-filler"
|
16
|
+
s.add_runtime_dependency 'sinatra', '>= 1.1'
|
17
|
+
s.add_runtime_dependency 'sinatra-static-assets', '>= 0.5.0'
|
18
|
+
s.add_development_dependency 'spec'
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
@@ -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,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-filler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "W\xC5\x82odek Bzyl"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-23 00:00:00 +01: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: 13
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 1
|
33
|
+
version: "1.1"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: sinatra-static-assets
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 11
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
- 5
|
48
|
+
- 0
|
49
|
+
version: 0.5.0
|
50
|
+
type: :runtime
|
51
|
+
version_requirements: *id002
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: spec
|
54
|
+
prerelease: false
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
66
|
+
description: "Small Sinatra extension to add several helpers methods: filler_for, stylesheets, javascripts"
|
67
|
+
email:
|
68
|
+
- matwb@ug.edu.pl
|
69
|
+
executables: []
|
70
|
+
|
71
|
+
extensions: []
|
72
|
+
|
73
|
+
extra_rdoc_files: []
|
74
|
+
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- Rakefile
|
79
|
+
- TODO
|
80
|
+
- lib/sinatra/sinatra_filler.rb
|
81
|
+
- lib/sinatra/version.rb
|
82
|
+
- sinatra-filler.gemspec
|
83
|
+
- test/content_for_test.rb
|
84
|
+
has_rdoc: true
|
85
|
+
homepage: http://www.sinatrarb.com/
|
86
|
+
licenses: []
|
87
|
+
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
hash: 3
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
version: "0"
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
hash: 3
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
version: "0"
|
111
|
+
requirements: []
|
112
|
+
|
113
|
+
rubyforge_project: sinatra-filler
|
114
|
+
rubygems_version: 1.3.7
|
115
|
+
signing_key:
|
116
|
+
specification_version: 3
|
117
|
+
summary: Small Sinatra extension to add several helpers methods
|
118
|
+
test_files:
|
119
|
+
- test/content_for_test.rb
|