sinatra-partial 0.1.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +3 -1
- data/README.markdown +34 -15
- data/examples/app_no_underscores/app.rb +10 -0
- data/examples/app_no_underscores/views/home.haml +2 -0
- data/examples/app_no_underscores/views/layout.haml +11 -0
- data/examples/app_no_underscores/views/locality.haml +4 -0
- data/examples/app_no_underscores/views/meta.haml +1 -0
- data/examples/app_no_underscores/views/news.haml +2 -0
- data/examples/app_with_underscores/app.rb +12 -0
- data/examples/app_with_underscores/views/_locality.haml +4 -0
- data/examples/app_with_underscores/views/_meta.haml +1 -0
- data/examples/app_with_underscores/views/_news.haml +2 -0
- data/examples/app_with_underscores/views/home.haml +2 -0
- data/examples/app_with_underscores/views/layout.haml +11 -0
- data/examples/app_with_underscores_and_erb/app.rb +14 -0
- data/examples/app_with_underscores_and_erb/views/_locality.erb +4 -0
- data/examples/app_with_underscores_and_erb/views/_meta.erb +1 -0
- data/examples/app_with_underscores_and_erb/views/_news.erb +3 -0
- data/examples/app_with_underscores_and_erb/views/home.erb +3 -0
- data/examples/app_with_underscores_and_erb/views/layout.erb +14 -0
- data/examples/app_with_underscores_and_erb_and_subdirs/app.rb +14 -0
- data/examples/app_with_underscores_and_erb_and_subdirs/views/home.erb +3 -0
- data/examples/app_with_underscores_and_erb_and_subdirs/views/layout.erb +14 -0
- data/examples/app_with_underscores_and_erb_and_subdirs/views/partials/_locality.erb +4 -0
- data/examples/app_with_underscores_and_erb_and_subdirs/views/partials/_meta.erb +1 -0
- data/examples/app_with_underscores_and_erb_and_subdirs/views/partials/_news.erb +3 -0
- data/lib/sinatra/partial.rb +67 -21
- data/lib/sinatra/partial/version.rb +1 -1
- data/sinatra-partial.gemspec +1 -1
- metadata +31 -8
data/CHANGES
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
v0.2.1 Sam Elliot provided a lot of helpful code to add in features that were lost from his gist. Further to these, the lib is now a Sinatra Extension so it can add in some useful defaults. Different template engines other than Haml may be specified, and leading underscores (a la Rails) can be specified for partials too. Examples have been added to the examples directory, and the code docs are much more extensive.
|
2
|
+
|
1
3
|
v0.1.1 Improved the examples in the Readme file. A lot.
|
2
4
|
|
3
|
-
v0.1.0 The locals hash doesn't get clobbered if passing a collection now, but you do get the chance to clobber the default of passing the collection local if you so wish. With great power comes great responsibility!
|
5
|
+
v0.1.0 The locals hash doesn't get clobbered if passing a collection now, but you do get the chance to clobber the default of passing the collection local if you so wish. With great power comes great responsibility!
|
data/README.markdown
CHANGED
@@ -24,15 +24,42 @@ So here it is, partials, and that's it.
|
|
24
24
|
|
25
25
|
gem install sinatra-partial
|
26
26
|
|
27
|
-
###
|
27
|
+
### Configuration options ###
|
28
|
+
|
29
|
+
The default templating engine is haml. If you wish to use something else, you can set in the config options:
|
30
|
+
|
31
|
+
set :partial_template_engine, :erb
|
32
|
+
|
33
|
+
If you wish, you can also pass this in with the options hash to partial (if you do, it will override the above setting for that call):
|
34
|
+
|
35
|
+
partial(:"meta/news", :template_engine => :erb)
|
36
|
+
|
37
|
+
If you like the Rails convention of adding an underscore to the beginning of a partial, set it here:
|
38
|
+
|
39
|
+
enable :partial_underscores
|
40
|
+
|
41
|
+
Otherwise, the default is for no underscore (if you like Rails you know where to get it;)
|
42
|
+
|
43
|
+
### Getting started ###
|
28
44
|
|
29
45
|
At the top of your app.rb:
|
30
46
|
|
31
47
|
require 'sinatra/partial'
|
32
48
|
|
49
|
+
For a classic app, that's all you need to do. For a modular app you should register it too:
|
50
|
+
|
33
51
|
class Blah < Sinatra::Base
|
34
|
-
|
35
|
-
|
52
|
+
register Sinatra::Partial
|
53
|
+
|
54
|
+
|
55
|
+
### Some examples ###
|
56
|
+
|
57
|
+
The docs are good to look at (big thanks to Sam Elliot for improving them a lot), just follow the docs link from this page if you can't find them:
|
58
|
+
|
59
|
+
https://rubygems.org/gems/sinatra-partial
|
60
|
+
|
61
|
+
or use yard/rdoc to generate them.
|
62
|
+
|
36
63
|
|
37
64
|
#### Inside a route ####
|
38
65
|
|
@@ -144,29 +171,21 @@ Here's how to use a collection, in this case to render a menu:
|
|
144
171
|
|
145
172
|
|
146
173
|
You'll get a menu built for you.
|
147
|
-
|
148
|
-
|
149
174
|
|
150
|
-
### Another quick note ###
|
151
175
|
|
152
|
-
|
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.
|
176
|
+
### Examples ###
|
157
177
|
|
158
|
-
|
178
|
+
Look in the examples directory for some very simple examples.
|
159
179
|
|
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
180
|
|
162
181
|
### Thanks ###
|
163
182
|
|
164
|
-
Thanks to Chris Schneider and Sam Elliott for sharing their code,
|
183
|
+
Thanks to Chris Schneider and Sam Elliott for sharing their code, and for sharing further updates.
|
165
184
|
|
166
185
|
|
167
186
|
### Licence ###
|
168
187
|
|
169
|
-
Copyright (c)
|
188
|
+
Copyright (c) 2012 Iain Barnett
|
170
189
|
|
171
190
|
MIT Licence
|
172
191
|
|
@@ -0,0 +1 @@
|
|
1
|
+
Time is #{Time.now.localtime}
|
@@ -0,0 +1 @@
|
|
1
|
+
Time is #{Time.now.localtime}
|
@@ -0,0 +1 @@
|
|
1
|
+
Time is <%= Time.now.localtime %>
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<html>
|
2
|
+
<head></head>
|
3
|
+
<body>
|
4
|
+
<%= partial :"partials/meta" %>
|
5
|
+
|
6
|
+
<ul>
|
7
|
+
<%= partial :"partials/news", :collection => News %>
|
8
|
+
</ul>
|
9
|
+
|
10
|
+
<%= partial( :"partials/locality", :locals => Hash[%w{a b c d}.zip(%w{A B C D})]) %>
|
11
|
+
|
12
|
+
<%= yield %>
|
13
|
+
</body>
|
14
|
+
</html>
|
@@ -0,0 +1 @@
|
|
1
|
+
Time is <%= Time.now.localtime %>
|
data/lib/sinatra/partial.rb
CHANGED
@@ -4,28 +4,74 @@ require 'sinatra/base'
|
|
4
4
|
|
5
5
|
module Sinatra
|
6
6
|
module Partial
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
if
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
7
|
+
module Helpers
|
8
|
+
# Renders a partial to a string.
|
9
|
+
#
|
10
|
+
# @param [#to_s] partial The partial to render.
|
11
|
+
# @param [Hash] options The options to render the partial with.
|
12
|
+
# @option options [Hash] :locals Local variables to render with
|
13
|
+
# @option options [Array] :collection Renders the template once per object in this array.
|
14
|
+
# @option options [Symbol] :template_engine The template engine to use. Haml by default.
|
15
|
+
# @option options [true,false] :underscores Set to true if you wish to follow the Rails convention of partial files having a leading underscore.
|
16
|
+
#
|
17
|
+
# @return [String] The rendered template contents.
|
18
|
+
#
|
19
|
+
# @example simply render a partial
|
20
|
+
# partial(:meta, :locals => {meta: meta})
|
21
|
+
# # => renders views/_meta.haml
|
22
|
+
#
|
23
|
+
# @example render a partial in a subfolder
|
24
|
+
# partial("meta/news", :locals => {news: [<News>]})
|
25
|
+
# # => renders views/meta/_news.haml
|
26
|
+
#
|
27
|
+
# @example render a collection of objects with one partial
|
28
|
+
# partial(:"meta/news", :collection => [<News>])
|
29
|
+
# # => renders views/meta/_news.haml once per item in :collection,
|
30
|
+
# with the local variable `news` being the current item in the iteration
|
31
|
+
def partial(partial, options={})
|
32
|
+
partial_location = partial.to_s
|
33
|
+
|
34
|
+
locals = options.fetch(:locals, {})
|
35
|
+
engine = options.fetch(:template_engine, settings.partial_template_engine)
|
36
|
+
underscores = options.fetch(:underscores, settings.partial_underscores)
|
37
|
+
|
38
|
+
template = partial_expand_path(partial_location, underscores)
|
39
|
+
|
40
|
+
if collection = options.delete(:collection)
|
41
|
+
member_local = partial_local(partial_location)
|
42
|
+
|
43
|
+
collection.inject([]) do |buffer, member|
|
44
|
+
new_locals = {member_local => member}.merge(locals)
|
45
|
+
buffer << self.method(engine).call(template, options.merge(:locals => new_locals))
|
46
|
+
end.join("\n")
|
47
|
+
else
|
48
|
+
self.method(engine).call(template, options)
|
49
|
+
end
|
24
50
|
end
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def partial_expand_path(partial_path, underscores=false)
|
55
|
+
dirs, base = [File.dirname(partial_path),File.basename(partial_path)]
|
56
|
+
base.insert(0, "_") if underscores
|
57
|
+
File.join(dirs, base).to_sym
|
58
|
+
end
|
59
|
+
|
60
|
+
def partial_local(partial_path)
|
61
|
+
File.basename(partial_path).to_sym
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.registered(app)
|
66
|
+
app.helpers(Partial::Helpers)
|
67
|
+
|
68
|
+
# Configuration
|
69
|
+
app.set :partial_underscores, false
|
70
|
+
app.set :partial_template_engine, :haml
|
71
|
+
end
|
72
|
+
|
73
|
+
end # Partial
|
74
|
+
register(Sinatra::Partial)
|
29
75
|
end
|
30
76
|
|
31
77
|
|
data/sinatra-partial.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.authors = ["Chris Schneider", "Sam Elliott", "Iain Barnett"]
|
10
10
|
s.email = ["iainspeed@gmail.com"]
|
11
11
|
s.homepage = "https://github.com/yb66/Sinatra-Partial"
|
12
|
-
s.summary = %q{
|
12
|
+
s.summary = %q{A sinatra extension for render partials.}
|
13
13
|
s.description = %q{Just the partials helper in a gem. That is all.}
|
14
14
|
s.license = 'MIT'
|
15
15
|
|
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.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,12 +11,11 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
15
|
-
default_executable:
|
14
|
+
date: 2012-04-10 00:00:00.000000000 Z
|
16
15
|
dependencies:
|
17
16
|
- !ruby/object:Gem::Dependency
|
18
17
|
name: sinatra
|
19
|
-
requirement: &
|
18
|
+
requirement: &2152441740 !ruby/object:Gem::Requirement
|
20
19
|
none: false
|
21
20
|
requirements:
|
22
21
|
- - ! '>='
|
@@ -24,7 +23,7 @@ dependencies:
|
|
24
23
|
version: '0'
|
25
24
|
type: :runtime
|
26
25
|
prerelease: false
|
27
|
-
version_requirements: *
|
26
|
+
version_requirements: *2152441740
|
28
27
|
description: Just the partials helper in a gem. That is all.
|
29
28
|
email:
|
30
29
|
- iainspeed@gmail.com
|
@@ -35,10 +34,33 @@ files:
|
|
35
34
|
- .gitignore
|
36
35
|
- CHANGES
|
37
36
|
- README.markdown
|
37
|
+
- examples/app_no_underscores/app.rb
|
38
|
+
- examples/app_no_underscores/views/home.haml
|
39
|
+
- examples/app_no_underscores/views/layout.haml
|
40
|
+
- examples/app_no_underscores/views/locality.haml
|
41
|
+
- examples/app_no_underscores/views/meta.haml
|
42
|
+
- examples/app_no_underscores/views/news.haml
|
43
|
+
- examples/app_with_underscores/app.rb
|
44
|
+
- examples/app_with_underscores/views/_locality.haml
|
45
|
+
- examples/app_with_underscores/views/_meta.haml
|
46
|
+
- examples/app_with_underscores/views/_news.haml
|
47
|
+
- examples/app_with_underscores/views/home.haml
|
48
|
+
- examples/app_with_underscores/views/layout.haml
|
49
|
+
- examples/app_with_underscores_and_erb/app.rb
|
50
|
+
- examples/app_with_underscores_and_erb/views/_locality.erb
|
51
|
+
- examples/app_with_underscores_and_erb/views/_meta.erb
|
52
|
+
- examples/app_with_underscores_and_erb/views/_news.erb
|
53
|
+
- examples/app_with_underscores_and_erb/views/home.erb
|
54
|
+
- examples/app_with_underscores_and_erb/views/layout.erb
|
55
|
+
- examples/app_with_underscores_and_erb_and_subdirs/app.rb
|
56
|
+
- examples/app_with_underscores_and_erb_and_subdirs/views/home.erb
|
57
|
+
- examples/app_with_underscores_and_erb_and_subdirs/views/layout.erb
|
58
|
+
- examples/app_with_underscores_and_erb_and_subdirs/views/partials/_locality.erb
|
59
|
+
- examples/app_with_underscores_and_erb_and_subdirs/views/partials/_meta.erb
|
60
|
+
- examples/app_with_underscores_and_erb_and_subdirs/views/partials/_news.erb
|
38
61
|
- lib/sinatra/partial.rb
|
39
62
|
- lib/sinatra/partial/version.rb
|
40
63
|
- sinatra-partial.gemspec
|
41
|
-
has_rdoc: true
|
42
64
|
homepage: https://github.com/yb66/Sinatra-Partial
|
43
65
|
licenses:
|
44
66
|
- MIT
|
@@ -60,8 +82,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
82
|
version: '0'
|
61
83
|
requirements: []
|
62
84
|
rubyforge_project:
|
63
|
-
rubygems_version: 1.
|
85
|
+
rubygems_version: 1.8.10
|
64
86
|
signing_key:
|
65
87
|
specification_version: 3
|
66
|
-
summary:
|
88
|
+
summary: A sinatra extension for render partials.
|
67
89
|
test_files: []
|
90
|
+
has_rdoc:
|