mustache 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -273,12 +273,19 @@ Sinatra
273
273
 
274
274
  Mustache ships with Sinatra integration. Please see
275
275
  `lib/mustache/sinatra.rb` or
276
- <http://defunkt.github.com/mustache/classes/Mustache/Sinatra.html> for
277
- complete documentation.
276
+ <http://github.com/defunkt/mustache/blob/master/lib/mustache/sinatra.rb>
277
+ for complete documentation.
278
278
 
279
279
  An example Sinatra application is also provided:
280
280
  <http://github.com/defunkt/mustache-sinatra-example>
281
281
 
282
+ If you are upgrading to Sinatra 1.0 and Mustache 0.9.0+ from Mustache
283
+ 0.7.0 or lower, the settings have changed. But not that much.
284
+
285
+ See [this diff](http://gist.github.com/345490) for what you need to
286
+ do. Basically, things are named properly now and all should be
287
+ contained in a hash set using `set :mustache, hash`.
288
+
282
289
 
283
290
  [Rack::Bug][4]
284
291
  --------------
@@ -305,6 +312,7 @@ is included under the contrib/ directory.
305
312
 
306
313
  See <http://gist.github.com/323622> for installation instructions.
307
314
 
315
+
308
316
  Emacs
309
317
  -----
310
318
 
@@ -323,6 +331,7 @@ TextMate
323
331
 
324
332
  See <http://gist.github.com/323624> for installation instructions.
325
333
 
334
+
326
335
  Command Line
327
336
  ------------
328
337
 
@@ -330,6 +339,7 @@ See `mustache(1)` man page or
330
339
  <http://defunkt.github.com/mustache/mustache.1.html>
331
340
  for command line docs.
332
341
 
342
+
333
343
  Installation
334
344
  ------------
335
345
 
@@ -341,12 +351,16 @@ Installation
341
351
 
342
352
  $ rip install git://github.com/defunkt/mustache.git
343
353
 
354
+
344
355
  Acknowledgements
345
356
  ----------------
346
357
 
347
358
  Thanks to [Tom Preston-Werner](http://github.com/mojombo) for showing
348
359
  me ctemplate and [Leah Culver](http://github.com/leah) for the name "Mustache."
349
360
 
361
+ Special thanks to [Magnus Holm](http://judofyr.net/) for all his
362
+ awesome work on Mustache's parser.
363
+
350
364
 
351
365
  Meta
352
366
  ----
@@ -92,6 +92,12 @@ class Mustache
92
92
  render(data, context)
93
93
  end
94
94
 
95
+ # Given a file name and an optional context, attempts to load and
96
+ # render the file as a template.
97
+ def render_file(name, context = {})
98
+ self.class.render_file(name, context)
99
+ end
100
+
95
101
  # The template path informs your Mustache subclass where to look for its
96
102
  # corresponding template. By default it's the current directory (".")
97
103
  def self.template_path
@@ -14,8 +14,7 @@ class Mustache
14
14
  class Context
15
15
  # Expect to be passed an instance of `Mustache`.
16
16
  def initialize(mustache)
17
- @mustache = mustache
18
- @stack = [@mustache]
17
+ @stack = [mustache]
19
18
  end
20
19
 
21
20
  # A {{>partial}} tag translates into a call to the context's
@@ -26,10 +25,27 @@ class Mustache
26
25
  # to `partial`, we call it and use the result. Otherwise we render
27
26
  # and compile the partial as its own view and return the result.
28
27
  def partial(name)
29
- if @mustache.respond_to? :partial
30
- @mustache.partial(name)
28
+ # Look for any Mustaches in the stack.
29
+ mustache = mustache_in_stack
30
+
31
+ if mustache.respond_to? :partial
32
+ # We found a mustache and it responds to `partial`, send it.
33
+ mustache.render(mustache.partial(name), self)
34
+
35
+ elsif mustache
36
+ # We found a mustache without `partial`, use it to render.
37
+ mustache.render_file(name, self)
31
38
  else
32
- @mustache.class.render_file(name, self)
39
+ # Can't find any staches, abort and use whatever we can..
40
+ raise "No Mustache views in stack."
41
+ end
42
+ end
43
+
44
+ # Find the first Mustache in the stack. If we're being rendered
45
+ # inside a Mustache object as a context, we'll use that one.
46
+ def mustache_in_stack
47
+ @stack.detect do |frame|
48
+ frame.is_a?(Mustache)
33
49
  end
34
50
  end
35
51
 
@@ -79,6 +95,10 @@ class Mustache
79
95
  # set to true), will raise a ContextMiss exception on miss.
80
96
  def fetch(name, default = :__raise)
81
97
  @stack.each do |frame|
98
+ # Prevent infinite recursion.
99
+ next if frame == self
100
+
101
+ # Is this frame a hash?
82
102
  hash = frame.respond_to?(:has_key?)
83
103
 
84
104
  if hash && frame.has_key?(name)
@@ -90,7 +110,7 @@ class Mustache
90
110
  end
91
111
  end
92
112
 
93
- if default == :__raise || @mustache.raise_on_context_miss?
113
+ if default == :__raise || mustache_in_stack.raise_on_context_miss?
94
114
  raise ContextMiss.new("Can't find #{name} in #{@stack.inspect}")
95
115
  else
96
116
  default
@@ -1,3 +1,3 @@
1
1
  class Mustache
2
- Version = '0.9.0'
2
+ Version = '0.9.1'
3
3
  end
@@ -150,7 +150,7 @@ gem install mustache
150
150
  $ mustache data.yml template.mustache
151
151
  $ cat data.yml | mustache \- template.mustache
152
152
  $ mustache \-c template.mustache
153
- $ cat <<data | druby mustache \- template.mustache
153
+ $ cat <<data | ruby mustache \- template.mustache
154
154
  \-\-\-
155
155
  name: Bob
156
156
  age: 30
@@ -173,7 +173,7 @@ identified by examining the tokens produced.</p></dd>
173
173
  <pre><code>$ mustache data.yml template.mustache
174
174
  $ cat data.yml | mustache - template.mustache
175
175
  $ mustache -c template.mustache
176
- $ cat &lt;&lt;data | druby mustache - template.mustache
176
+ $ cat &lt;&lt;data | ruby mustache - template.mustache
177
177
  ---
178
178
  name: Bob
179
179
  age: 30
@@ -363,4 +363,19 @@ start
363
363
  end
364
364
  expected
365
365
  end
366
+
367
+ def test_id_with_nested_context
368
+ html = %(<div>{{id}}</div>\n<div>{{# has_a? }}{{id}}{{/ has_a? }}</div>\n<div>{{# has_b? }}{{id}}{{/ has_b? }}</div>)
369
+
370
+ instance = Mustache.new
371
+ instance.template = html
372
+ instance[:id] = 3
373
+ instance[:has_a?] = true
374
+ instance[:has_b?] = true
375
+ assert_equal <<-rendered.strip, instance.render
376
+ <div>3</div>
377
+ <div>3</div>
378
+ <div>3</div>
379
+ rendered
380
+ end
366
381
  end
@@ -124,4 +124,36 @@ end_partial
124
124
  </html>
125
125
  end_partial
126
126
  end
127
+
128
+ def test_partials_use_proper_context
129
+ assert_equal "OuterThing OuterThing", OuterThing.render('{{name}} {{> p}}')
130
+
131
+ assert_equal "InnerThing InnerThing", InnerThing.render('{{name}} {{> p}}')
132
+
133
+ assert_equal "OuterThing InnerThing InnerThing",
134
+ OuterThing.render('{{name}} {{#inner}}{{name}} {{> p}}{{/inner}}')
135
+ end
136
+
137
+ def test_partials_render_returned_strings
138
+ assert_equal "ok", MiddleThing.render('{{> some_partial }}')
139
+ end
140
+ end
141
+
142
+ class InnerThing < Mustache
143
+ def partial(p) self.class end
144
+ def name; self.class end
145
+ end
146
+
147
+ class OuterThing < Mustache
148
+ def inner
149
+ InnerThing.new
150
+ end
151
+
152
+ def partial(p) self.class end
153
+ def name; self.class end
154
+ end
155
+
156
+ class MiddleThing < Mustache
157
+ def partial(name) "{{#{name}}}" end
158
+ def some_partial; "ok" end
127
159
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mustache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wanstrath
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-26 00:00:00 -07:00
12
+ date: 2010-03-27 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15