sinatra-partial 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -1
- data/CHANGES +2 -0
- data/README.markdown +180 -0
- data/lib/sinatra/partial/version.rb +1 -1
- metadata +8 -6
- data/README +0 -69
data/.gitignore
CHANGED
data/CHANGES
CHANGED
data/README.markdown
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
## Sinatra Partial ##
|
2
|
+
|
3
|
+
Partials for Sinatra!
|
4
|
+
|
5
|
+
### Quick note ###
|
6
|
+
|
7
|
+
If you do decide to use this gem, please let me know if it isn't working for you - make a contribution! Github makes it so simple..!
|
8
|
+
|
9
|
+
Back to our previously scheduled programming...
|
10
|
+
|
11
|
+
### Por qué? ###
|
12
|
+
|
13
|
+
You may say "why is this needed?".
|
14
|
+
|
15
|
+
Go on then.
|
16
|
+
|
17
|
+
_huff_ "Why is this needed?"
|
18
|
+
|
19
|
+
Because I was forever copying the code I took from http://www.sinatrarb.com/faq.html#partials into each and every project. It may well be that this is included in some other gem full of useful helpers, but I haven't found it yet, and besides _this is what I really want_. The whole point of Sinatra is not to get a lot of stuff you didn't really need anyway.
|
20
|
+
|
21
|
+
So here it is, partials, and that's it.
|
22
|
+
|
23
|
+
### Installation ###
|
24
|
+
|
25
|
+
gem install sinatra-partial
|
26
|
+
|
27
|
+
### Some examples ###
|
28
|
+
|
29
|
+
At the top of your app.rb:
|
30
|
+
|
31
|
+
require 'sinatra/partial'
|
32
|
+
|
33
|
+
class Blah < Sinatra::Base
|
34
|
+
helpers Sinatra::Partial
|
35
|
+
|
36
|
+
|
37
|
+
#### Inside a route ####
|
38
|
+
|
39
|
+
get "/" do
|
40
|
+
output = ""
|
41
|
+
output << partial( :top )
|
42
|
+
output << partial( :middle )
|
43
|
+
output << partial( :bottom )
|
44
|
+
output
|
45
|
+
end
|
46
|
+
|
47
|
+
-# top.haml
|
48
|
+
%h2
|
49
|
+
The is the top
|
50
|
+
|
51
|
+
-# middle.haml
|
52
|
+
%p
|
53
|
+
Can you guess what I am yet?
|
54
|
+
|
55
|
+
-# bottom.haml
|
56
|
+
%p
|
57
|
+
Is it worse to be at the bottom or the foot?
|
58
|
+
|
59
|
+
#### Local variables ####
|
60
|
+
|
61
|
+
get "/" do
|
62
|
+
output = ""
|
63
|
+
@title = "My contrived example"
|
64
|
+
username = current_user.username
|
65
|
+
output << partial( :left_col )
|
66
|
+
output << partial( :middle, :locals => { username: username} )
|
67
|
+
output << partial( :right )
|
68
|
+
output
|
69
|
+
end
|
70
|
+
|
71
|
+
-# middle.haml
|
72
|
+
%p
|
73
|
+
Wow, here is that #{username} you just passed me!
|
74
|
+
:-o
|
75
|
+
|
76
|
+
|
77
|
+
#### Here's one using views ####
|
78
|
+
|
79
|
+
Remember that since this is a helper method it can be called inside routes and views - use it where you need it!
|
80
|
+
|
81
|
+
-# welcome_message.haml
|
82
|
+
%h2
|
83
|
+
Welcome back #{username}
|
84
|
+
|
85
|
+
-# content.haml
|
86
|
+
Blah Blah Blah
|
87
|
+
|
88
|
+
-# footer.haml
|
89
|
+
You've reached the bottom of the page!
|
90
|
+
|
91
|
+
-# layout.haml
|
92
|
+
%html
|
93
|
+
%head
|
94
|
+
%body
|
95
|
+
#header
|
96
|
+
= partial :welcome_message, locals: {username: "Iain" }
|
97
|
+
|
98
|
+
#main
|
99
|
+
= partial :content
|
100
|
+
|
101
|
+
= yield
|
102
|
+
|
103
|
+
#footer
|
104
|
+
= partial :footer
|
105
|
+
|
106
|
+
|
107
|
+
### Collections ###
|
108
|
+
|
109
|
+
Here's how to use a collection, in this case to render a menu:
|
110
|
+
|
111
|
+
# app.rb
|
112
|
+
|
113
|
+
before do
|
114
|
+
@menu = [
|
115
|
+
["home", uri("/")],
|
116
|
+
["login", uri("/login")],
|
117
|
+
["contact_us", uri("/contact-us")],
|
118
|
+
["about", uri("/about")],
|
119
|
+
]
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
-# menu.haml
|
124
|
+
#nav
|
125
|
+
%ul
|
126
|
+
= partial :menu_item, collection: menu
|
127
|
+
|
128
|
+
-# menu_item.haml
|
129
|
+
- atts ||= {}
|
130
|
+
- atts[:active] = {class: "active" } if request.url == menu_item.last
|
131
|
+
%li{ atts }
|
132
|
+
%a{ class: "nav", href: menu_item.last }
|
133
|
+
= menu_item.first
|
134
|
+
|
135
|
+
|
136
|
+
-# layout.haml
|
137
|
+
%html
|
138
|
+
%head
|
139
|
+
%title= @title
|
140
|
+
%body
|
141
|
+
= yield
|
142
|
+
|
143
|
+
= partial :menu, locals: { menu: @menu }
|
144
|
+
|
145
|
+
|
146
|
+
You'll get a menu built for you.
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
### Another quick note ###
|
151
|
+
|
152
|
+
Since the locals hash was getting clobbered when using a collection, I've changed the code so that you can pass in a collection _and_ locals that will be accessible too.
|
153
|
+
|
154
|
+
= partial( :record, collection: v[:records], :locals => {something_else: 243} )
|
155
|
+
|
156
|
+
Now you'll be able to access the local `something_else` within the `:record` partial.
|
157
|
+
|
158
|
+
### HULK SMASH!!! ###
|
159
|
+
|
160
|
+
You can also decide to do some clobbering of your own and HULK SMASH! the defaults for the collection, which in this case would be :record (as it's the template name) and :layout => false. Perhaps you might need this so I've left it up to you, but probably best to leave it alone and call your locals something else. It's your code.
|
161
|
+
|
162
|
+
### Thanks ###
|
163
|
+
|
164
|
+
Thanks to Chris Schneider and Sam Elliott for sharing their code, I just made it into this gem and tinkered ever so slightly.
|
165
|
+
|
166
|
+
|
167
|
+
### Licence ###
|
168
|
+
|
169
|
+
Copyright (c) 2011 Iain Barnett
|
170
|
+
|
171
|
+
MIT Licence
|
172
|
+
|
173
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
174
|
+
|
175
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
176
|
+
|
177
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
178
|
+
|
179
|
+
|
180
|
+
i.e. be good!
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-partial
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,11 +11,12 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2011-
|
14
|
+
date: 2011-12-09 00:00:00.000000000 +00:00
|
15
|
+
default_executable:
|
15
16
|
dependencies:
|
16
17
|
- !ruby/object:Gem::Dependency
|
17
18
|
name: sinatra
|
18
|
-
requirement: &
|
19
|
+
requirement: &2152539520 !ruby/object:Gem::Requirement
|
19
20
|
none: false
|
20
21
|
requirements:
|
21
22
|
- - ! '>='
|
@@ -23,7 +24,7 @@ dependencies:
|
|
23
24
|
version: '0'
|
24
25
|
type: :runtime
|
25
26
|
prerelease: false
|
26
|
-
version_requirements: *
|
27
|
+
version_requirements: *2152539520
|
27
28
|
description: Just the partials helper in a gem. That is all.
|
28
29
|
email:
|
29
30
|
- iainspeed@gmail.com
|
@@ -33,10 +34,11 @@ extra_rdoc_files: []
|
|
33
34
|
files:
|
34
35
|
- .gitignore
|
35
36
|
- CHANGES
|
36
|
-
- README
|
37
|
+
- README.markdown
|
37
38
|
- lib/sinatra/partial.rb
|
38
39
|
- lib/sinatra/partial/version.rb
|
39
40
|
- sinatra-partial.gemspec
|
41
|
+
has_rdoc: true
|
40
42
|
homepage: https://github.com/yb66/Sinatra-Partial
|
41
43
|
licenses:
|
42
44
|
- MIT
|
@@ -58,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
60
|
version: '0'
|
59
61
|
requirements: []
|
60
62
|
rubyforge_project:
|
61
|
-
rubygems_version: 1.
|
63
|
+
rubygems_version: 1.6.2
|
62
64
|
signing_key:
|
63
65
|
specification_version: 3
|
64
66
|
summary: Rack middleware for Geo IP city lookup
|
data/README
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
Quick note: If you do decide to use this gem, please let me know if it isn't working for you - make a contribution!
|
2
|
-
|
3
|
-
Back to our previously scheduled programming...
|
4
|
-
|
5
|
-
You may say "why is this needed?".
|
6
|
-
|
7
|
-
Go on then.
|
8
|
-
|
9
|
-
_huff_ "Why is this needed?"
|
10
|
-
|
11
|
-
Because I was forever copying the code I took from http://www.sinatrarb.com/faq.html#partials into each and every project. It may well be that this is included in some other gem full of useful helpers, but I haven't found it yet, and besides _this is what I really want_. The whole point of Sinatra is not to get a lot of stuff you didn't really need anyway.
|
12
|
-
|
13
|
-
So here it is, partials, and that's it.
|
14
|
-
|
15
|
-
Installation
|
16
|
-
============
|
17
|
-
|
18
|
-
gem install sinatra-partial
|
19
|
-
|
20
|
-
Some examples
|
21
|
-
=============
|
22
|
-
|
23
|
-
At the top of your app.rb:
|
24
|
-
|
25
|
-
require 'sinatra/partial'
|
26
|
-
|
27
|
-
class Blah < Sinatra::Base
|
28
|
-
helpers Sinatra::Partial
|
29
|
-
|
30
|
-
|
31
|
-
Inside a route:
|
32
|
-
|
33
|
-
get "/" do
|
34
|
-
output = ''
|
35
|
-
post_links = some_array_full_of_links
|
36
|
-
output << partial( :main, :locals => { :output => partial( :post_links, :locals => { title: "Posts", posts: post_links } ) } )
|
37
|
-
haml :content, :locals => { :output => output, title: "posts" }
|
38
|
-
end
|
39
|
-
|
40
|
-
|
41
|
-
Here's one inside a view:
|
42
|
-
|
43
|
-
- address = "ip#{ip.first.gsub(".", "_")}"
|
44
|
-
- v = ip.last
|
45
|
-
%div{class: "ip", id: "#{address}", ip: "#{ip.first}" }
|
46
|
-
%a{name: "#{address}"}
|
47
|
-
%h3= "#{ip.first}"
|
48
|
-
%p= "last visit: #{v[:last_visit]} No. of visits: #{v[:visits]}"
|
49
|
-
= partial( :geo, :locals => { geo: v[:geo] } )
|
50
|
-
|
51
|
-
%a{href: "##{address}", class: "records", ip: "#{ip.first}"}
|
52
|
-
Show/hide records
|
53
|
-
%div{class: "records", ip: "#{ip.first}"}
|
54
|
-
<h4>Records:</h4>
|
55
|
-
= partial( :record, collection: v[:records] )
|
56
|
-
|
57
|
-
Yes, a tad confusing, but I can't be bothered to cook up a simple example - just look at the _partials_ and how they're used. Concentration brings insight (so it's said!)
|
58
|
-
|
59
|
-
Another quick note: Since the locals hash was getting clobbered when using a collection, I've changed the code so that you can pass in a collection _and_ locals that will be accessible too.
|
60
|
-
|
61
|
-
= partial( :record, collection: v[:records], :locals => {something_else: 243} )
|
62
|
-
|
63
|
-
Now you'll be able to access the local `something_else` within the :record partial.
|
64
|
-
|
65
|
-
You can also decide to do some clobbering of your own and HULK SMASH! the defaults for the collection, which in this case would be :record (as it's the template name) and :layout => false. Perhaps you might need this so I've left it up to you, but probably best to leave it alone and call your locals something else. It's your code.
|
66
|
-
|
67
|
-
Thanks to Chris Schneider and Sam Elliott for sharing their code, I just made it into this gem.
|
68
|
-
|
69
|
-
MIT Licence (i.e. be good!)
|