tiltout 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tiltout (1.1.0)
4
+ tiltout (1.3.0)
5
5
  tilt (~> 1.3)
6
6
 
7
7
  GEM
@@ -9,7 +9,7 @@ GEM
9
9
  specs:
10
10
  metaclass (0.0.1)
11
11
  minitest (2.12.1)
12
- mocha (0.12.6)
12
+ mocha (0.12.7)
13
13
  metaclass (~> 0.0.1)
14
14
  rake (0.9.2.2)
15
15
  tilt (1.3.3)
@@ -24,6 +24,7 @@
24
24
  #++
25
25
  require "tilt"
26
26
  require "tiltout/version"
27
+ require "tiltout/partials"
27
28
 
28
29
  class Tiltout
29
30
  def initialize(template_root, opt = {})
@@ -42,16 +43,22 @@ class Tiltout
42
43
 
43
44
  def render(template, locals = {}, options = {})
44
45
  context = @context_class.new
45
- content = load(template).render(context, locals)
46
+ context.renderer = self if context.respond_to?(:renderer=)
47
+ content = render_in_context(context, template, locals)
46
48
  layout_tpl = options.key?(:layout) ? options[:layout] : @layout
47
49
 
48
50
  if !layout_tpl.nil?
49
- content = load(layout_tpl).render(context, locals) { content }
51
+ content = render_in_context(context, layout_tpl, locals) { content }
52
+ #content = load(layout_tpl).render(context, locals) { content }
50
53
  end
51
54
 
52
55
  content
53
56
  end
54
57
 
58
+ def render_in_context(context, template, locals, &block)
59
+ load(template).render(context, locals, &block)
60
+ end
61
+
55
62
  private
56
63
  def load(name)
57
64
  file_name = find_file(name)
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ # --
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (C) 2012 Gitorious AS
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+ #++
25
+
26
+ module Tiltout::Partials
27
+ def renderer=(renderer)
28
+ @renderer = renderer
29
+ end
30
+
31
+ def partial(template, locals = {})
32
+ @renderer.render_in_context(self, template, locals)
33
+ end
34
+ end
@@ -24,5 +24,5 @@
24
24
  #++
25
25
 
26
26
  class Tiltout
27
- VERSION = "1.1.0"
27
+ VERSION = "1.2.0"
28
28
  end
@@ -50,10 +50,12 @@ describe Tiltout do
50
50
  end
51
51
 
52
52
  it "renders template with locals" do
53
- fake_file("#{@root}/file.erb", "<%= name %>!")
53
+ fake_file("#{@root}/file.erb", <<-ERB)
54
+ <%= name %>!
55
+ ERB
54
56
  renderer = Tiltout.new(@root)
55
57
 
56
- assert_equal "Chris!", renderer.render(:file, { :name => "Chris"})
58
+ assert_equal "Chris!\n", renderer.render(:file, { :name => "Chris"})
57
59
  end
58
60
 
59
61
  it "caches template in memory" do
@@ -77,23 +79,29 @@ describe Tiltout do
77
79
  it "renders template with layout" do
78
80
  renderer = Tiltout.new("/", :layout => "layout")
79
81
  fake_file("/file.erb", "Template")
80
- fake_file("/layout.erb", "I give you: <%= yield %>")
82
+ fake_file("/layout.erb", <<-ERB)
83
+ I give you: <%= yield %>
84
+ ERB
81
85
 
82
- assert_equal "I give you: Template", renderer.render(:file)
86
+ assert_equal "I give you: Template\n", renderer.render(:file)
83
87
  end
84
88
 
85
89
  it "renders template with layout not in template root" do
86
90
  renderer = Tiltout.new("/somewhere", :layout => { :file => "/my/layout.erb" })
87
91
  fake_file("/somewhere/file.erb", "Template")
88
- fake_file("/my/layout.erb", "I give you: <%= yield %>")
92
+ fake_file("/my/layout.erb", <<-ERB)
93
+ I give you: <%= yield %>
94
+ ERB
89
95
 
90
- assert_equal "I give you: Template", renderer.render(:file)
96
+ assert_equal "I give you: Template\n", renderer.render(:file)
91
97
  end
92
98
 
93
99
  it "renders template once without layout" do
94
100
  renderer = Tiltout.new("/", :layout => "layout")
95
101
  fake_file("/file.erb", "Template")
96
- fake_file("/layout.erb", "I give you: <%= yield %>")
102
+ fake_file("/layout.erb", <<-ERB)
103
+ I give you: <%= yield %>
104
+ ERB
97
105
 
98
106
  assert_equal "Template", renderer.render(:file, {}, :layout => nil)
99
107
  end
@@ -101,12 +109,16 @@ describe Tiltout do
101
109
  it "renders template once with different layout" do
102
110
  renderer = Tiltout.new("/", :layout => "layout")
103
111
  fake_file("/file.erb", "Template")
104
- fake_file("/layout.erb", "I give you: <%= yield %>")
105
- fake_file("/layout2.erb", "I present you: <%= yield %>")
112
+ fake_file("/layout.erb", <<-ERB)
113
+ I give you: <%= yield %>
114
+ ERB
115
+ fake_file("/layout2.erb", <<-ERB)
116
+ I present you: <%= yield %>
117
+ ERB
106
118
 
107
119
  html = renderer.render(:file, {}, :layout => "layout2")
108
120
 
109
- assert_equal "I present you: Template", html
121
+ assert_equal "I present you: Template\n", html
110
122
  end
111
123
 
112
124
  it "renders templates of default type" do
@@ -129,16 +141,20 @@ describe Tiltout do
129
141
  it "renders with helper object" do
130
142
  renderer = Tiltout.new("/")
131
143
  renderer.helper(ViewHelper)
132
- fake_file("/file.erb", "Say it: <%= say_it %>")
144
+ fake_file("/file.erb", <<-ERB)
145
+ Say it: <%= say_it %>
146
+ ERB
133
147
 
134
- assert_equal "Say it: YES", renderer.render(:file)
148
+ assert_equal "Say it: YES\n", renderer.render(:file)
135
149
  end
136
150
 
137
151
  it "creates with helper object" do
138
152
  renderer = Tiltout.new("/", :helpers => [ViewHelper])
139
- fake_file("/file.erb", "Say it: <%= say_it %>")
153
+ fake_file("/file.erb", <<-ERB)
154
+ Say it: <%= say_it %>
155
+ ERB
140
156
 
141
- assert_equal "Say it: YES", renderer.render(:file)
157
+ assert_equal "Say it: YES\n", renderer.render(:file)
142
158
  end
143
159
 
144
160
  it "does not leak state across render calls" do
@@ -160,4 +176,29 @@ describe Tiltout do
160
176
 
161
177
  assert_equal "<title>NO</title><h1>NO</h1>\n", renderer.render(:file)
162
178
  end
179
+
180
+ describe "partials" do
181
+ it "renders partial" do
182
+ renderer = Tiltout.new("/", :helpers => [Tiltout::Partials])
183
+ fake_file("/part.erb", "I'm partial")
184
+ fake_file("/file.erb", <<-TEMPLATE)
185
+ <%= partial(:part) %>!
186
+ TEMPLATE
187
+
188
+ assert_equal "I'm partial!\n", renderer.render(:file)
189
+ end
190
+
191
+ it "renders partial in template context" do
192
+ renderer = Tiltout.new("/", :helpers => [Tiltout::Partials])
193
+ fake_file("/part.erb", <<-ERB)
194
+ <%= @name %> is partial
195
+ ERB
196
+ fake_file("/file.erb", <<-TEMPLATE)
197
+ <% @name = "Chris" %>
198
+ <%= partial(:part) %>
199
+ TEMPLATE
200
+
201
+ assert_equal "Chris is partial\n", renderer.render(:file)
202
+ end
203
+ end
163
204
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiltout
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 1.1.0
10
+ version: 1.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Christian Johansen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-10-10 00:00:00 +02:00
18
+ date: 2012-10-15 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -96,6 +96,7 @@ files:
96
96
  - Rakefile
97
97
  - Readme.md
98
98
  - lib/tiltout.rb
99
+ - lib/tiltout/partials.rb
99
100
  - lib/tiltout/version.rb
100
101
  - test/tiltout_test.rb
101
102
  - tiltout.gemspec