curtain 0.1.2 → 0.1.3
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/README.md +83 -51
- data/curtain.gemspec +2 -2
- data/lib/curtain/version.rb +1 -1
- metadata +4 -3
data/README.md
CHANGED
@@ -20,82 +20,114 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
To use Curtain, you define a view and then have that view render templates:
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
23
|
+
### hello.erb
|
24
|
+
``` erb
|
25
|
+
<h1><%= msg %></h1>
|
26
|
+
```
|
27
|
+
|
28
|
+
### my_view.rb
|
29
|
+
``` ruby
|
30
|
+
class MyView
|
31
|
+
include Curtain
|
32
|
+
|
33
|
+
attr_accessor :msg
|
34
|
+
|
35
|
+
def initialize(msg)
|
36
|
+
@msg = msg
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
``` ruby
|
42
|
+
view = MyView.new("Hello, World!")
|
43
|
+
view.render("hello") # => <h1>Hello, World!</h1>
|
44
|
+
```
|
33
45
|
|
34
46
|
The template is rendered in the scope of the view object, so any methods defined in the view are available to the template. You don't have to create a subclass if you don't need to:
|
35
47
|
|
36
|
-
|
48
|
+
``` ruby
|
49
|
+
Curtain::View.new.render("hello", :msg => "Hello, World!")
|
50
|
+
```
|
37
51
|
|
38
52
|
There is an equivalent shortcut available:
|
39
53
|
|
40
|
-
|
54
|
+
``` ruby
|
55
|
+
Curtain.render("hello", :msg => "Hello, World!")
|
56
|
+
```
|
41
57
|
|
42
58
|
Curtain includes many useful methods. Here's a more realistic example that shows some of the built-in methods. If you have templates like this:
|
43
59
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
60
|
+
### friends.erb
|
61
|
+
``` erb
|
62
|
+
<% cache "friends-#{current_user.id}", ttl: 5.minutes do %>
|
63
|
+
<% friends.each do |friend| %>
|
64
|
+
<%= render "profile", :profile => friend %>
|
65
|
+
<% end %>
|
66
|
+
<% end %>
|
67
|
+
```
|
68
|
+
|
69
|
+
### profile.erb
|
70
|
+
``` erb
|
71
|
+
<ul>
|
72
|
+
<li><%= link_to profile.name, path(:profile, :id => profile.id) %></li>
|
73
|
+
</ul>
|
74
|
+
```
|
55
75
|
|
56
76
|
You can use them in this way:
|
57
77
|
|
58
|
-
|
59
|
-
|
60
|
-
|
78
|
+
``` ruby
|
79
|
+
class ApplicationView < Curtain::View
|
80
|
+
attr_accessor :current_user
|
81
|
+
end
|
61
82
|
|
62
|
-
|
63
|
-
|
64
|
-
|
83
|
+
class FriendsView < Curtain::View
|
84
|
+
delegate :friends, :to => :current_user
|
85
|
+
end
|
65
86
|
|
66
|
-
|
87
|
+
view = FriendsView.new(:current_user => User.first)
|
67
88
|
|
68
|
-
|
69
|
-
|
89
|
+
# The default template name is based on the name of the class of the view
|
90
|
+
view.render
|
91
|
+
```
|
70
92
|
|
71
93
|
### Variables
|
72
94
|
|
73
95
|
If you don't want to define a subclass of `Curtain::View` and add attributes to it, you can also use variables. `Curtain::View` supports the hash-like Ruby method `[]` and `[]=` to define variables that will act as locals in when the template is rendered:
|
74
96
|
|
75
|
-
|
76
|
-
|
97
|
+
### hello.erb
|
98
|
+
``` erb
|
99
|
+
<h1><%= msg %></h1>
|
100
|
+
```
|
77
101
|
|
78
|
-
|
79
|
-
|
80
|
-
|
102
|
+
``` ruby
|
103
|
+
view = Curtain::View.new
|
104
|
+
view[:msg] = "Hello"
|
105
|
+
view.render(:hello) # => "<h1>Hello</h1>"
|
106
|
+
```
|
81
107
|
|
82
108
|
Note that unlike locals, variables exist throughout nested scope of render calls:
|
83
109
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
foo: <%= foo %>
|
91
|
-
bar: <%= bar %>
|
92
|
-
|
93
|
-
class MainView < Curtain::View
|
94
|
-
end
|
110
|
+
### main.erb
|
111
|
+
``` erb
|
112
|
+
foo: <%= foo %>
|
113
|
+
bar: <%= bar %>
|
114
|
+
<%= render "partial" %>
|
115
|
+
```
|
95
116
|
|
96
|
-
|
97
|
-
|
98
|
-
|
117
|
+
### partial.erb
|
118
|
+
``` erb
|
119
|
+
foo: <%= foo %>
|
120
|
+
bar: <%= bar %>
|
121
|
+
```
|
122
|
+
|
123
|
+
``` ruby
|
124
|
+
class MainView < Curtain::View
|
125
|
+
end
|
126
|
+
|
127
|
+
view = MainView.new
|
128
|
+
view[:foo] = "foo"
|
129
|
+
view.render :bar => "bar"
|
130
|
+
```
|
99
131
|
|
100
132
|
This example would result in an error. As the main template is first rendered, foo is defined as "foo" because it is a variable, the bar is "bar" because it is passed in as a local. Then the partial template is rendered, and foo is still defined as "foo" because it is a variable, but since bar was a local passed to the rendering of main, it doesn't carry through to the rendering of partial.
|
101
133
|
|
data/curtain.gemspec
CHANGED
@@ -5,14 +5,14 @@ Gem::Specification.new do |gem|
|
|
5
5
|
gem.email = ["mail@paulbarry.com"]
|
6
6
|
gem.description = %q{A template rendering framework}
|
7
7
|
gem.summary = %q{A template rendering framework}
|
8
|
-
gem.homepage = ""
|
8
|
+
gem.homepage = "http://github.com/pjb3/curtain"
|
9
9
|
|
10
10
|
gem.files = `git ls-files`.split($\)
|
11
11
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
12
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
13
|
gem.name = "curtain"
|
14
14
|
gem.require_paths = ["lib"]
|
15
|
-
gem.version = "0.1.
|
15
|
+
gem.version = "0.1.3"
|
16
16
|
|
17
17
|
gem.add_runtime_dependency "activesupport"
|
18
18
|
gem.add_runtime_dependency "tilt"
|
data/lib/curtain/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: curtain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -129,7 +129,7 @@ files:
|
|
129
129
|
- test/html_helpers_test.rb
|
130
130
|
- test/test_helper.rb
|
131
131
|
- test/url_helpers_test.rb
|
132
|
-
homepage:
|
132
|
+
homepage: http://github.com/pjb3/curtain
|
133
133
|
licenses: []
|
134
134
|
post_install_message:
|
135
135
|
rdoc_options: []
|
@@ -166,3 +166,4 @@ test_files:
|
|
166
166
|
- test/html_helpers_test.rb
|
167
167
|
- test/test_helper.rb
|
168
168
|
- test/url_helpers_test.rb
|
169
|
+
has_rdoc:
|