mustache 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,10 +1,15 @@
1
1
  require 'rake/testtask'
2
2
  require 'rake/rdoctask'
3
3
 
4
+ #
5
+ # Helpers
6
+ #
7
+
4
8
  def command?(command)
5
9
  system("type #{command} > /dev/null")
6
10
  end
7
11
 
12
+
8
13
  #
9
14
  # Tests
10
15
  #
@@ -25,6 +30,15 @@ else
25
30
  end
26
31
  end
27
32
 
33
+ if command? :kicker
34
+ desc "Launch Kicker (like autotest)"
35
+ task :kicker do
36
+ puts "Kicking... (ctrl+c to cancel)"
37
+ exec "kicker -e rake test lib examples"
38
+ end
39
+ end
40
+
41
+
28
42
  #
29
43
  # Ron
30
44
  #
@@ -41,13 +55,6 @@ if command? :ronn
41
55
  end
42
56
  end
43
57
 
44
- if command? :kicker
45
- desc "Launch Kicker (like autotest)"
46
- task :kicker do
47
- puts "Kicking... (ctrl+c to cancel)"
48
- exec "kicker -e rake test lib examples"
49
- end
50
- end
51
58
 
52
59
  #
53
60
  # Gems
@@ -57,14 +64,8 @@ begin
57
64
  require 'mg'
58
65
  MG.new("mustache.gemspec")
59
66
 
60
- desc "Build a gem."
61
- task :gem => :package
62
-
63
- # Ensure tests pass before pushing a gem.
64
- task :gemcutter => :test
65
-
66
67
  desc "Push a new version to Gemcutter and publish docs."
67
- task :publish => :gemcutter do
68
+ task :publish => "gem:publish" do
68
69
  require File.dirname(__FILE__) + '/lib/mustache/version'
69
70
 
70
71
  system "git tag v#{Mustache::Version}"
@@ -81,11 +82,11 @@ end
81
82
  # Documentation
82
83
  #
83
84
 
84
- # begin
85
- # require 'sdoc_helpers'
86
- # rescue LoadError
87
- # warn "sdoc support not enabled. Please gem install sdoc-helpers."
88
- # end
85
+ begin
86
+ require 'sdoc_helpers'
87
+ rescue LoadError
88
+ warn "sdoc support not enabled. Please gem install sdoc-helpers."
89
+ end
89
90
 
90
91
  desc "Publish to GitHub Pages"
91
92
  task :pages => [ "man:build" ] do
@@ -116,7 +116,7 @@ class Mustache
116
116
  # The template path informs your Mustache subclass where to look for its
117
117
  # corresponding template. By default it's the current directory (".")
118
118
  def self.template_path
119
- @template_path ||= '.'
119
+ @template_path ||= inheritable_config_for :template_path, '.'
120
120
  end
121
121
 
122
122
  def self.template_path=(path)
@@ -136,7 +136,7 @@ class Mustache
136
136
 
137
137
  # A Mustache template's default extension is 'mustache'
138
138
  def self.template_extension
139
- @template_extension ||= 'mustache'
139
+ @template_extension ||= inheritable_config_for :template_extension, 'mustache'
140
140
  end
141
141
 
142
142
  def self.template_extension=(template_extension)
@@ -182,7 +182,7 @@ class Mustache
182
182
  # `Object`, but it might be nice to set it to something like `Hurl::Views` if
183
183
  # your app's main namespace is `Hurl`.
184
184
  def self.view_namespace
185
- @view_namespace || Object
185
+ @view_namespace ||= inheritable_config_for(:view_namespace, Object)
186
186
  end
187
187
 
188
188
  def self.view_namespace=(namespace)
@@ -192,7 +192,7 @@ class Mustache
192
192
  # Mustache searches the view path for .rb files to require when asked to find a
193
193
  # view class. Defaults to "."
194
194
  def self.view_path
195
- @view_path ||= '.'
195
+ @view_path ||= inheritable_config_for(:view_path, '.')
196
196
  end
197
197
 
198
198
  def self.view_path=(path)
@@ -288,6 +288,17 @@ class Mustache
288
288
  end
289
289
  end
290
290
 
291
+ # Return the value of the configuration setting on the superclass, or return
292
+ # the default.
293
+ #
294
+ # attr_name - Symbol name of the attribute. It should match the instance variable.
295
+ # default - Default value to use if the superclass does not respond.
296
+ #
297
+ # Returns the inherited or default configuration setting.
298
+ def self.inheritable_config_for(attr_name, default)
299
+ superclass.respond_to?(attr_name) ? superclass.send(attr_name) : default
300
+ end
301
+
291
302
  def templateify(obj)
292
303
  self.class.templateify(obj)
293
304
  end
@@ -101,6 +101,8 @@ class Mustache
101
101
  if v = ctx[#{name.to_sym.inspect}]
102
102
  if v == true
103
103
  #{code}
104
+ elsif v.is_a?(Proc)
105
+ v.call(#{code})
104
106
  else
105
107
  v = [v] unless v.is_a?(Array) # shortcut when passed non-array
106
108
  v.map { |h| ctx.push(h); r = #{code}; ctx.pop; r }.join
@@ -1,3 +1,3 @@
1
1
  class Mustache
2
- Version = '0.10.0'
2
+ Version = VERSION = '0.11.0'
3
3
  end
@@ -1,7 +1,7 @@
1
- .\" generated with Ronn/v0.4.1
1
+ .\" generated with Ronn/v0.5
2
2
  .\" http://github.com/rtomayko/ronn/
3
3
  .
4
- .TH "MUSTACHE" "1" "March 2010" "DEFUNKT" "Mustache Manual"
4
+ .TH "MUSTACHE" "1" "April 2010" "DEFUNKT" "Mustache Manual"
5
5
  .
6
6
  .SH "NAME"
7
7
  \fBmustache\fR \-\- Mustache processor
@@ -9,6 +9,7 @@
9
9
  .SH "SYNOPSIS"
10
10
  .
11
11
  .nf
12
+
12
13
  mustache <YAML> <FILE>
13
14
  mustache \-\-compile <FILE>
14
15
  mustache \-\-tokens <FILE>
@@ -31,6 +32,7 @@ ending with another \fB\-\-\-\fR on a single line, e.g.
31
32
  .IP "" 4
32
33
  .
33
34
  .nf
35
+
34
36
  \-\-\-
35
37
  names: [ {name: chris}, {name: mark}, {name: scott} ]
36
38
  \-\-\-
@@ -53,6 +55,7 @@ For example:
53
55
  .IP "" 4
54
56
  .
55
57
  .nf
58
+
56
59
  {{#names}}
57
60
  Hi {{name}}!
58
61
  {{/names}}
@@ -67,14 +70,17 @@ Now let's combine them.
67
70
  .IP "" 4
68
71
  .
69
72
  .nf
73
+
70
74
  $ cat data.yml
71
75
  \-\-\-
72
76
  names: [ {name: chris}, {name: mark}, {name: scott} ]
73
77
  \-\-\-
78
+
74
79
  $ cat template.mustache
75
80
  {{#names}}
76
81
  Hi {{name}}!
77
82
  {{/names}}
83
+
78
84
  $ cat data.yml template.mustache | mustache
79
85
  Hi chris!
80
86
  Hi mark!
@@ -94,6 +100,7 @@ For example:
94
100
  .IP "" 4
95
101
  .
96
102
  .nf
103
+
97
104
  $ cat data.yml
98
105
  \-\-\-
99
106
  name: chris
@@ -102,8 +109,10 @@ name: mark
102
109
  \-\-\-
103
110
  name: scott
104
111
  \-\-\-
112
+
105
113
  $ cat template.mustache
106
114
  Hi {{name}}!
115
+
107
116
  $ cat data.yml template.mustache | mustache
108
117
  Hi chris!
109
118
  Hi mark!
@@ -138,6 +147,7 @@ If you have RubyGems installed:
138
147
  .IP "" 4
139
148
  .
140
149
  .nf
150
+
141
151
  gem install mustache
142
152
  .
143
153
  .fi
@@ -147,6 +157,7 @@ gem install mustache
147
157
  .SH "EXAMPLES"
148
158
  .
149
159
  .nf
160
+
150
161
  $ mustache data.yml template.mustache
151
162
  $ cat data.yml | mustache \- template.mustache
152
163
  $ mustache \-c template.mustache
@@ -166,4 +177,4 @@ Mustache is Copyright (C) 2009 Chris Wanstrath
166
177
  Original CTemplate by Google
167
178
  .
168
179
  .SH "SEE ALSO"
169
- mustache(5), mustache(7), gem(1),\fIhttp://defunkt.github.com/mustache/\fR
180
+ mustache(5), mustache(7), gem(1), \fIhttp://mustache.github.com/\fR
@@ -2,7 +2,7 @@
2
2
  <html>
3
3
  <head>
4
4
  <meta http-equiv='content-type' value='text/html;charset=utf8'>
5
- <meta name='generator' value='Ronn/v0.4.1'>
5
+ <meta name='generator' value='Ronn/v0.5'>
6
6
  <title>mustache(1) -- Mustache processor</title>
7
7
  <style type='text/css'>
8
8
  body {margin:0}
@@ -190,12 +190,12 @@ data
190
190
  <h2>SEE ALSO</h2>
191
191
 
192
192
  <p>mustache(5), mustache(7), gem(1),
193
- <a href="http://defunkt.github.com/mustache/">http://defunkt.github.com/mustache/</a></p>
193
+ <a href="http://mustache.github.com/">http://mustache.github.com/</a></p>
194
194
 
195
195
 
196
196
  <ol class='foot man'>
197
197
  <li class='tl'>DEFUNKT</li>
198
- <li class='tc'>March 2010</li>
198
+ <li class='tc'>April 2010</li>
199
199
  <li class='tr'>mustache(1)</li>
200
200
  </ol>
201
201
 
@@ -1,7 +1,7 @@
1
- .\" generated with Ronn/v0.4.1
1
+ .\" generated with Ronn/v0.5
2
2
  .\" http://github.com/rtomayko/ronn/
3
3
  .
4
- .TH "MUSTACHE" "5" "March 2010" "DEFUNKT" "Mustache Manual"
4
+ .TH "MUSTACHE" "5" "April 2010" "DEFUNKT" "Mustache Manual"
5
5
  .
6
6
  .SH "NAME"
7
7
  \fBmustache\fR \-\- Logic\-less templates.
@@ -12,6 +12,7 @@ A typical Mustache template:
12
12
  .IP "" 4
13
13
  .
14
14
  .nf
15
+
15
16
  Hello {{name}}
16
17
  You have just won ${{value}}!
17
18
  {{#in_ca}}
@@ -28,6 +29,7 @@ Given the following hash:
28
29
  .IP "" 4
29
30
  .
30
31
  .nf
32
+
31
33
  {
32
34
  "name": "Chris",
33
35
  "value": 10000,
@@ -45,6 +47,7 @@ Will produce the following:
45
47
  .IP "" 4
46
48
  .
47
49
  .nf
50
+
48
51
  Hello Chris
49
52
  You have just won $10000!
50
53
  Well, $6000.0, after taxes.
@@ -65,12 +68,14 @@ replaced with a value, some nothing, and others a series of
65
68
  values. This document explains the different types of Mustache tags.
66
69
  .
67
70
  .SH "TAG TYPES"
68
- Tags are indicated by the double mustaches. \fB{{name}}\fR is a tag, as is \fB{{#name}}\fR. Let's talk about the different types of tags.
71
+ Tags are indicated by the double mustaches. \fB{{person}}\fR is a tag, as
72
+ is \fB{{#person}}\fR. In both examples, we'd refer to \fBperson\fR as the key
73
+ or tag key. Let's talk about the different types of tags.
69
74
  .
70
75
  .SS "Variables"
71
- The most basic tag is the variable. A \fB{{name}}\fR tag in a basic
72
- template will try to find the \fBname\fR key or method on your view. If
73
- there is no \fBname\fR method, nothing will be rendered.
76
+ The most basic tag type is the variable. A \fB{{name}}\fR tag in a basic
77
+ template will try to find the \fBname\fR key in the current context. If
78
+ there is no \fBname\fR key, nothing will be rendered.
74
79
  .
75
80
  .P
76
81
  All variables are HTML escaped by default. If you want to return
@@ -78,7 +83,7 @@ unescaped HTML, use the triple mustache: \fB{{{name}}}\fR.
78
83
  .
79
84
  .P
80
85
  You can also use \fB&\fR to unescape a variable: \fB{{& name}}\fR. This may be
81
- useful when changing delimiters (see "Set Delimter" below).
86
+ useful when changing delimiters (see "Set Delimiter" below).
82
87
  .
83
88
  .P
84
89
  By default a variable "miss" returns an empty string. This can usually
@@ -91,6 +96,7 @@ Template:
91
96
  .IP "" 4
92
97
  .
93
98
  .nf
99
+
94
100
  * {{name}}
95
101
  * {{age}}
96
102
  * {{company}}
@@ -106,6 +112,7 @@ Hash:
106
112
  .IP "" 4
107
113
  .
108
114
  .nf
115
+
109
116
  {
110
117
  "name": "Chris",
111
118
  "company": "<b>GitHub</b>"
@@ -121,6 +128,7 @@ Output:
121
128
  .IP "" 4
122
129
  .
123
130
  .nf
131
+
124
132
  * Chris
125
133
  *
126
134
  * &lt;b&gt;GitHub&lt;/b&gt;
@@ -132,25 +140,20 @@ Output:
132
140
  .
133
141
  .SS "Sections"
134
142
  Sections render blocks of text one or more times, depending on the
135
- value of the referenced tag.
143
+ value of the key in the current context.
136
144
  .
137
145
  .P
138
- A section begins with a pound and ends with a slash. That is,\fB{{#person}}\fR begins a "person" section while \fB{{/person}}\fR ends it.
146
+ A section begins with a pound and ends with a slash. That is, \fB{{#person}}\fR begins a "person" section while \fB{{/person}}\fR ends it.
139
147
  .
140
148
  .P
141
- If the \fBperson\fR key exists and calling it returns false or an empty
142
- list, the HTML between the pound and slash will not be displayed.
149
+ The behavior of the section is determined by the value of the key.
143
150
  .
144
151
  .P
145
- If the \fBperson\fR method exists and calling it returns true or an
146
- object, the HTML between the pound and slash will be rendered and
147
- displayed exactly one time. The object that was returned by the \fBperson\fR method will become the context of the block, as well.
152
+ \fBFalse Values or Empty Lists\fR
148
153
  .
149
154
  .P
150
- If the \fBperson\fR method exists and calling it returns a non\-empty list,
151
- the text in the block will be displayed once for each item in the
152
- list. The context of the block will be set to the current item for
153
- each iteration. In this way we can loop over collections.
155
+ If the \fBperson\fR key exists and has a value of false or an empty
156
+ list, the HTML between the pound and slash will not be displayed.
154
157
  .
155
158
  .P
156
159
  Template:
@@ -158,12 +161,64 @@ Template:
158
161
  .IP "" 4
159
162
  .
160
163
  .nf
161
- {{#person}}
162
- Shown!
163
- {{/person}}
164
- {{#anything_else}}
164
+
165
+ Shown.
166
+ {{#nothin}}
165
167
  Never shown!
166
- {{/anything_else}}
168
+ {{/nothin}}
169
+ .
170
+ .fi
171
+ .
172
+ .IP "" 0
173
+ .
174
+ .P
175
+ Hash:
176
+ .
177
+ .IP "" 4
178
+ .
179
+ .nf
180
+
181
+ {
182
+ "person": true,
183
+ }
184
+ .
185
+ .fi
186
+ .
187
+ .IP "" 0
188
+ .
189
+ .P
190
+ Output:
191
+ .
192
+ .IP "" 4
193
+ .
194
+ .nf
195
+
196
+ Shown.
197
+ .
198
+ .fi
199
+ .
200
+ .IP "" 0
201
+ .
202
+ .P
203
+ \fBNon\-Empty Lists\fR
204
+ .
205
+ .P
206
+ If the \fBperson\fR key exists and has a non\-false value, the HTML between
207
+ the pound and slash will be rendered and displayed one or more times.
208
+ .
209
+ .P
210
+ When the value is a non\-empty list, the text in the block will be
211
+ displayed once for each item in the list. The context of the block
212
+ will be set to the current item for each iteration. In this way we can
213
+ loop over collections.
214
+ .
215
+ .P
216
+ Template:
217
+ .
218
+ .IP "" 4
219
+ .
220
+ .nf
221
+
167
222
  {{#repo}}
168
223
  <b>{{name}}</b>
169
224
  {{/repo}}
@@ -178,8 +233,8 @@ Hash:
178
233
  .IP "" 4
179
234
  .
180
235
  .nf
236
+
181
237
  {
182
- "person": true,
183
238
  "repo": [
184
239
  { "name": "resque" },
185
240
  { "name": "hub" },
@@ -197,7 +252,7 @@ Output:
197
252
  .IP "" 4
198
253
  .
199
254
  .nf
200
- Shown!
255
+
201
256
  <b>resque</b>
202
257
  <b>hub</b>
203
258
  <b>rip</b>
@@ -206,14 +261,122 @@ Shown!
206
261
  .
207
262
  .IP "" 0
208
263
  .
264
+ .P
265
+ \fBLambdas\fR
266
+ .
267
+ .P
268
+ When the value is a callable object, such as a function or lambda, the
269
+ object will be invoked and passed the block of text. The text passed
270
+ is the literal block, unrendered. \fB{{tags}}\fR will not have been expanded
271
+ \- the lambda should do that on its own. In this way you can implement
272
+ filters or caching.
273
+ .
274
+ .P
275
+ Template:
276
+ .
277
+ .IP "" 4
278
+ .
279
+ .nf
280
+
281
+ {{#wrapped}}
282
+ {{name}} is awesome.
283
+ {{/wrapped}}
284
+ .
285
+ .fi
286
+ .
287
+ .IP "" 0
288
+ .
289
+ .P
290
+ Hash:
291
+ .
292
+ .IP "" 4
293
+ .
294
+ .nf
295
+
296
+ {
297
+ "name": "Willy",
298
+ "wrapped": function() {
299
+ return function(text) {
300
+ return "<b>" + render(text) + "</b>"
301
+ }
302
+ }
303
+ }
304
+ .
305
+ .fi
306
+ .
307
+ .IP "" 0
308
+ .
309
+ .P
310
+ Output:
311
+ .
312
+ .IP "" 4
313
+ .
314
+ .nf
315
+
316
+ <b>Willy is awesome.</b>
317
+ .
318
+ .fi
319
+ .
320
+ .IP "" 0
321
+ .
322
+ .P
323
+ \fBNon\-False Values\fR
324
+ .
325
+ .P
326
+ When the value is non\-false but not a list, it will be used as the
327
+ context for a single rendering of the block.
328
+ .
329
+ .P
330
+ Template:
331
+ .
332
+ .IP "" 4
333
+ .
334
+ .nf
335
+
336
+ {{#person?}}
337
+ Hi {{name}}!
338
+ {{/person?}}
339
+ .
340
+ .fi
341
+ .
342
+ .IP "" 0
343
+ .
344
+ .P
345
+ Hash:
346
+ .
347
+ .IP "" 4
348
+ .
349
+ .nf
350
+
351
+ {
352
+ "person?": { "name": "Jon" }
353
+ }
354
+ .
355
+ .fi
356
+ .
357
+ .IP "" 0
358
+ .
359
+ .P
360
+ Output:
361
+ .
362
+ .IP "" 4
363
+ .
364
+ .nf
365
+
366
+ Hi Jon!
367
+ .
368
+ .fi
369
+ .
370
+ .IP "" 0
371
+ .
209
372
  .SS "Inverted Sections"
210
373
  An inverted section begins with a caret (hat) and ends with a
211
374
  slash. That is \fB{{^person}}\fR begins a "person" inverted section while \fB{{/person}}\fR ends it.
212
375
  .
213
376
  .P
214
377
  While sections can be used to render text one or more times based on the
215
- value of the key given, inverted sections may render text once based
216
- on the inverse value of the key given. That is, they will be rendered
378
+ value of the key, inverted sections may render text once based
379
+ on the inverse value of the key. That is, they will be rendered
217
380
  if the key doesn't exist, is false, or is an empty list.
218
381
  .
219
382
  .P
@@ -222,6 +385,7 @@ Template:
222
385
  .IP "" 4
223
386
  .
224
387
  .nf
388
+
225
389
  {{#repo}}
226
390
  <b>{{name}}</b>
227
391
  {{/repo}}
@@ -239,6 +403,7 @@ Hash:
239
403
  .IP "" 4
240
404
  .
241
405
  .nf
406
+
242
407
  {
243
408
  "repo": []
244
409
  }
@@ -253,6 +418,7 @@ Output:
253
418
  .IP "" 4
254
419
  .
255
420
  .nf
421
+
256
422
  No repos :(
257
423
  .
258
424
  .fi
@@ -265,6 +431,7 @@ Comments begin with a bang and are ignored. The following template:
265
431
  .IP "" 4
266
432
  .
267
433
  .nf
434
+
268
435
  <h1>Today{{! ignore me }}.</h1>
269
436
  .
270
437
  .fi
@@ -277,12 +444,16 @@ Will render as follows:
277
444
  .IP "" 4
278
445
  .
279
446
  .nf
447
+
280
448
  <h1>Today.</h1>
281
449
  .
282
450
  .fi
283
451
  .
284
452
  .IP "" 0
285
453
  .
454
+ .P
455
+ Comments may contain newlines.
456
+ .
286
457
  .SS "Partials"
287
458
  Partials begin with a greater than sign, like \fB{{> box}}\fR.
288
459
  .
@@ -297,6 +468,7 @@ this:
297
468
  .IP "" 4
298
469
  .
299
470
  .nf
471
+
300
472
  <%= partial :next_more, :start => start, :size => size %>
301
473
  .
302
474
  .fi
@@ -309,6 +481,7 @@ Mustache requires only this:
309
481
  .IP "" 4
310
482
  .
311
483
  .nf
484
+
312
485
  {{> next_more}}
313
486
  .
314
487
  .fi
@@ -328,13 +501,15 @@ For example, this template and partial:
328
501
  .IP "" 4
329
502
  .
330
503
  .nf
504
+
331
505
  base.mustache:
332
506
  <h2>Names</h2>
333
- {{# names }}
334
- {{> user }}
335
- {{/ names }}
507
+ {{#names}}
508
+ {{> user}}
509
+ {{/names}}
510
+
336
511
  user.mustache:
337
- <strong>{{ name }}</strong>
512
+ <strong>{{name}}</strong>
338
513
  .
339
514
  .fi
340
515
  .
@@ -346,10 +521,11 @@ Can be thought of as a single, expanded template:
346
521
  .IP "" 4
347
522
  .
348
523
  .nf
524
+
349
525
  <h2>Names</h2>
350
- {{# names }}
351
- <strong>{{ name }}</strong>
352
- {{/ names }}
526
+ {{#names}}
527
+ <strong>{{name}}</strong>
528
+ {{/names}}
353
529
  .
354
530
  .fi
355
531
  .
@@ -357,7 +533,7 @@ Can be thought of as a single, expanded template:
357
533
  .
358
534
  .SS "Set Delimiter"
359
535
  Set Delimiter tags start with an equal sign and change the tag
360
- delimiters from {{ and }} to custom strings.
536
+ delimiters from \fB{{\fR and \fB}}\fR to custom strings.
361
537
  .
362
538
  .P
363
539
  Consider the following contrived example:
@@ -365,7 +541,8 @@ Consider the following contrived example:
365
541
  .IP "" 4
366
542
  .
367
543
  .nf
368
- * {{ default_tags }}
544
+
545
+ * {{default_tags}}
369
546
  {{=<% %>=}}
370
547
  * <% erb_style_tags %>
371
548
  <%={{ }}=%>
@@ -396,4 +573,4 @@ Mustache is Copyright (C) 2009 Chris Wanstrath
396
573
  Original CTemplate by Google
397
574
  .
398
575
  .SH "SEE ALSO"
399
- mustache(1), mustache(7), gem(1),\fIhttp://defunkt.github.com/mustache/\fR
576
+ mustache(1), mustache(7), \fIhttp://mustache.github.com/\fR
@@ -2,7 +2,7 @@
2
2
  <html>
3
3
  <head>
4
4
  <meta http-equiv='content-type' value='text/html;charset=utf8'>
5
- <meta name='generator' value='Ronn/v0.4.1'>
5
+ <meta name='generator' value='Ronn/v0.5'>
6
6
  <title>mustache(5) -- Logic-less templates.</title>
7
7
  <style type='text/css'>
8
8
  body {margin:0}
@@ -106,20 +106,21 @@ values. This document explains the different types of Mustache tags.</p>
106
106
 
107
107
  <h2>TAG TYPES</h2>
108
108
 
109
- <p>Tags are indicated by the double mustaches. <code>{{name}}</code> is a tag, as is
110
- <code>{{#name}}</code>. Let's talk about the different types of tags.</p>
109
+ <p>Tags are indicated by the double mustaches. <code>{{person}}</code> is a tag, as
110
+ is <code>{{#person}}</code>. In both examples, we'd refer to <code>person</code> as the key
111
+ or tag key. Let's talk about the different types of tags.</p>
111
112
 
112
113
  <h3>Variables</h3>
113
114
 
114
- <p>The most basic tag is the variable. A <code>{{name}}</code> tag in a basic
115
- template will try to find the <code>name</code> key or method on your view. If
116
- there is no <code>name</code> method, nothing will be rendered.</p>
115
+ <p>The most basic tag type is the variable. A <code>{{name}}</code> tag in a basic
116
+ template will try to find the <code>name</code> key in the current context. If
117
+ there is no <code>name</code> key, nothing will be rendered.</p>
117
118
 
118
119
  <p>All variables are HTML escaped by default. If you want to return
119
120
  unescaped HTML, use the triple mustache: <code>{{{name}}}</code>.</p>
120
121
 
121
122
  <p>You can also use <code>&amp;</code> to unescape a variable: <code>{{&amp; name}}</code>. This may be
122
- useful when changing delimiters (see "Set Delimter" below).</p>
123
+ useful when changing delimiters (see "Set Delimiter" below).</p>
123
124
 
124
125
  <p>By default a variable "miss" returns an empty string. This can usually
125
126
  be configured in your Mustache library. The Ruby version of Mustache
@@ -152,33 +153,51 @@ supports raising an exception in this situation, for instance.</p>
152
153
  <h3>Sections</h3>
153
154
 
154
155
  <p>Sections render blocks of text one or more times, depending on the
155
- value of the referenced tag.</p>
156
+ value of the key in the current context.</p>
156
157
 
157
158
  <p>A section begins with a pound and ends with a slash. That is,
158
159
  <code>{{#person}}</code> begins a "person" section while <code>{{/person}}</code> ends it.</p>
159
160
 
160
- <p>If the <code>person</code> key exists and calling it returns false or an empty
161
- list, the HTML between the pound and slash will not be displayed.</p>
161
+ <p>The behavior of the section is determined by the value of the key.</p>
162
162
 
163
- <p>If the <code>person</code> method exists and calling it returns true or an
164
- object, the HTML between the pound and slash will be rendered and
165
- displayed exactly one time. The object that was returned by the
166
- <code>person</code> method will become the context of the block, as well.</p>
163
+ <p><strong>False Values or Empty Lists</strong></p>
167
164
 
168
- <p>If the <code>person</code> method exists and calling it returns a non-empty list,
169
- the text in the block will be displayed once for each item in the
170
- list. The context of the block will be set to the current item for
171
- each iteration. In this way we can loop over collections.</p>
165
+ <p>If the <code>person</code> key exists and has a value of false or an empty
166
+ list, the HTML between the pound and slash will not be displayed.</p>
172
167
 
173
168
  <p>Template:</p>
174
169
 
175
- <pre><code>{{#person}}
176
- Shown!
177
- {{/person}}
178
- {{#anything_else}}
170
+ <pre><code>Shown.
171
+ {{#nothin}}
179
172
  Never shown!
180
- {{/anything_else}}
181
- {{#repo}}
173
+ {{/nothin}}
174
+ </code></pre>
175
+
176
+ <p>Hash:</p>
177
+
178
+ <pre><code>{
179
+ "person": true,
180
+ }
181
+ </code></pre>
182
+
183
+ <p>Output:</p>
184
+
185
+ <pre><code>Shown.
186
+ </code></pre>
187
+
188
+ <p><strong>Non-Empty Lists</strong></p>
189
+
190
+ <p>If the <code>person</code> key exists and has a non-false value, the HTML between
191
+ the pound and slash will be rendered and displayed one or more times.</p>
192
+
193
+ <p>When the value is a non-empty list, the text in the block will be
194
+ displayed once for each item in the list. The context of the block
195
+ will be set to the current item for each iteration. In this way we can
196
+ loop over collections.</p>
197
+
198
+ <p>Template:</p>
199
+
200
+ <pre><code>{{#repo}}
182
201
  &lt;b&gt;{{name}}&lt;/b&gt;
183
202
  {{/repo}}
184
203
  </code></pre>
@@ -186,7 +205,6 @@ each iteration. In this way we can loop over collections.</p>
186
205
  <p>Hash:</p>
187
206
 
188
207
  <pre><code>{
189
- "person": true,
190
208
  "repo": [
191
209
  { "name": "resque" },
192
210
  { "name": "hub" },
@@ -197,12 +215,67 @@ each iteration. In this way we can loop over collections.</p>
197
215
 
198
216
  <p>Output:</p>
199
217
 
200
- <pre><code>Shown!
201
- &lt;b&gt;resque&lt;/b&gt;
218
+ <pre><code>&lt;b&gt;resque&lt;/b&gt;
202
219
  &lt;b&gt;hub&lt;/b&gt;
203
220
  &lt;b&gt;rip&lt;/b&gt;
204
221
  </code></pre>
205
222
 
223
+ <p><strong>Lambdas</strong></p>
224
+
225
+ <p>When the value is a callable object, such as a function or lambda, the
226
+ object will be invoked and passed the block of text. The text passed
227
+ is the literal block, unrendered. <code>{{tags}}</code> will not have been expanded
228
+ - the lambda should do that on its own. In this way you can implement
229
+ filters or caching.</p>
230
+
231
+ <p>Template:</p>
232
+
233
+ <pre><code>{{#wrapped}}
234
+ {{name}} is awesome.
235
+ {{/wrapped}}
236
+ </code></pre>
237
+
238
+ <p>Hash:</p>
239
+
240
+ <pre><code>{
241
+ "name": "Willy",
242
+ "wrapped": function() {
243
+ return function(text) {
244
+ return "&lt;b&gt;" + render(text) + "&lt;/b&gt;"
245
+ }
246
+ }
247
+ }
248
+ </code></pre>
249
+
250
+ <p>Output:</p>
251
+
252
+ <pre><code>&lt;b&gt;Willy is awesome.&lt;/b&gt;
253
+ </code></pre>
254
+
255
+ <p><strong>Non-False Values</strong></p>
256
+
257
+ <p>When the value is non-false but not a list, it will be used as the
258
+ context for a single rendering of the block.</p>
259
+
260
+ <p>Template:</p>
261
+
262
+ <pre><code>{{#person?}}
263
+ Hi {{name}}!
264
+ {{/person?}}
265
+ </code></pre>
266
+
267
+ <p>Hash:</p>
268
+
269
+ <pre><code>{
270
+ "person?": { "name": "Jon" }
271
+ }
272
+ </code></pre>
273
+
274
+ <p>Output:</p>
275
+
276
+ <pre><code>Hi Jon!
277
+ </code></pre>
278
+
206
279
  <h3>Inverted Sections</h3>
207
280
 
208
281
  <p>An inverted section begins with a caret (hat) and ends with a
@@ -210,8 +283,8 @@ slash. That is <code>{{^person}}</code> begins a "person" inverted section while
210
283
  <code>{{/person}}</code> ends it.</p>
211
284
 
212
285
  <p>While sections can be used to render text one or more times based on the
213
- value of the key given, inverted sections may render text once based
214
- on the inverse value of the key given. That is, they will be rendered
286
+ value of the key, inverted sections may render text once based
287
+ on the inverse value of the key. That is, they will be rendered
215
288
  if the key doesn't exist, is false, or is an empty list.</p>
216
289
 
217
290
  <p>Template:</p>
@@ -248,6 +321,8 @@ if the key doesn't exist, is false, or is an empty list.</p>
248
321
  <pre><code>&lt;h1&gt;Today.&lt;/h1&gt;
249
322
  </code></pre>
250
323
 
324
+ <p>Comments may contain newlines.</p>
325
+
251
326
  <h3>Partials</h3>
252
327
 
253
328
  <p>Partials begin with a greater than sign, like <code>{{&gt; box}}</code>.</p>
@@ -276,30 +351,30 @@ expansion, even though it's not literally true.</p>
276
351
 
277
352
  <pre><code>base.mustache:
278
353
  &lt;h2&gt;Names&lt;/h2&gt;
279
- {{# names }}
280
- {{&gt; user }}
281
- {{/ names }}
354
+ {{#names}}
355
+ {{&gt; user}}
356
+ {{/names}}
282
357
 
283
358
  user.mustache:
284
- &lt;strong&gt;{{ name }}&lt;/strong&gt;
359
+ &lt;strong&gt;{{name}}&lt;/strong&gt;
285
360
  </code></pre>
286
361
 
287
362
  <p>Can be thought of as a single, expanded template:</p>
288
363
 
289
364
  <pre><code>&lt;h2&gt;Names&lt;/h2&gt;
290
- {{# names }}
291
- &lt;strong&gt;{{ name }}&lt;/strong&gt;
292
- {{/ names }}
365
+ {{#names}}
366
+ &lt;strong&gt;{{name}}&lt;/strong&gt;
367
+ {{/names}}
293
368
  </code></pre>
294
369
 
295
370
  <h3>Set Delimiter</h3>
296
371
 
297
372
  <p>Set Delimiter tags start with an equal sign and change the tag
298
- delimiters from {{ and }} to custom strings.</p>
373
+ delimiters from <code>{{</code> and <code>}}</code> to custom strings.</p>
299
374
 
300
375
  <p>Consider the following contrived example:</p>
301
376
 
302
- <pre><code>* {{ default_tags }}
377
+ <pre><code>* {{default_tags}}
303
378
  {{=&lt;% %>=}}
304
379
  * &lt;% erb_style_tags %>
305
380
  &lt;%={{ }}=%>
@@ -325,13 +400,13 @@ markup."</p>
325
400
 
326
401
  <h2>SEE ALSO</h2>
327
402
 
328
- <p>mustache(1), mustache(7), gem(1),
329
- <a href="http://defunkt.github.com/mustache/">http://defunkt.github.com/mustache/</a></p>
403
+ <p>mustache(1), mustache(7),
404
+ <a href="http://mustache.github.com/">http://mustache.github.com/</a></p>
330
405
 
331
406
 
332
407
  <ol class='foot man'>
333
408
  <li class='tl'>DEFUNKT</li>
334
- <li class='tc'>March 2010</li>
409
+ <li class='tc'>April 2010</li>
335
410
  <li class='tr'>mustache(5)</li>
336
411
  </ol>
337
412
 
@@ -41,21 +41,22 @@ values. This document explains the different types of Mustache tags.
41
41
 
42
42
  ## TAG TYPES
43
43
 
44
- Tags are indicated by the double mustaches. `{{name}}` is a tag, as is
45
- `{{#name}}`. Let's talk about the different types of tags.
44
+ Tags are indicated by the double mustaches. `{{person}}` is a tag, as
45
+ is `{{#person}}`. In both examples, we'd refer to `person` as the key
46
+ or tag key. Let's talk about the different types of tags.
46
47
 
47
48
 
48
49
  ### Variables
49
50
 
50
- The most basic tag is the variable. A `{{name}}` tag in a basic
51
- template will try to find the `name` key or method on your view. If
52
- there is no `name` method, nothing will be rendered.
51
+ The most basic tag type is the variable. A `{{name}}` tag in a basic
52
+ template will try to find the `name` key in the current context. If
53
+ there is no `name` key, nothing will be rendered.
53
54
 
54
55
  All variables are HTML escaped by default. If you want to return
55
56
  unescaped HTML, use the triple mustache: `{{{name}}}`.
56
57
 
57
58
  You can also use `&` to unescape a variable: `{{& name}}`. This may be
58
- useful when changing delimiters (see "Set Delimter" below).
59
+ useful when changing delimiters (see "Set Delimiter" below).
59
60
 
60
61
  By default a variable "miss" returns an empty string. This can usually
61
62
  be configured in your Mustache library. The Ruby version of Mustache
@@ -86,32 +87,47 @@ Output:
86
87
  ### Sections
87
88
 
88
89
  Sections render blocks of text one or more times, depending on the
89
- value of the referenced tag.
90
+ value of the key in the current context.
90
91
 
91
92
  A section begins with a pound and ends with a slash. That is,
92
93
  `{{#person}}` begins a "person" section while `{{/person}}` ends it.
93
94
 
94
- If the `person` key exists and calling it returns false or an empty
95
- list, the HTML between the pound and slash will not be displayed.
95
+ The behavior of the section is determined by the value of the key.
96
96
 
97
- If the `person` method exists and calling it returns true or an
98
- object, the HTML between the pound and slash will be rendered and
99
- displayed exactly one time. The object that was returned by the
100
- `person` method will become the context of the block, as well.
97
+ **False Values or Empty Lists**
101
98
 
102
- If the `person` method exists and calling it returns a non-empty list,
103
- the text in the block will be displayed once for each item in the
104
- list. The context of the block will be set to the current item for
105
- each iteration. In this way we can loop over collections.
99
+ If the `person` key exists and has a value of false or an empty
100
+ list, the HTML between the pound and slash will not be displayed.
106
101
 
107
102
  Template:
108
103
 
109
- {{#person}}
110
- Shown!
111
- {{/person}}
112
- {{#anything_else}}
104
+ Shown.
105
+ {{#nothin}}
113
106
  Never shown!
114
- {{/anything_else}}
107
+ {{/nothin}}
108
+
109
+ Hash:
110
+
111
+ {
112
+ "person": true,
113
+ }
114
+
115
+ Output:
116
+
117
+ Shown.
118
+
119
+ **Non-Empty Lists**
120
+
121
+ If the `person` key exists and has a non-false value, the HTML between
122
+ the pound and slash will be rendered and displayed one or more times.
123
+
124
+ When the value is a non-empty list, the text in the block will be
125
+ displayed once for each item in the list. The context of the block
126
+ will be set to the current item for each iteration. In this way we can
127
+ loop over collections.
128
+
129
+ Template:
130
+
115
131
  {{#repo}}
116
132
  <b>{{name}}</b>
117
133
  {{/repo}}
@@ -119,7 +135,6 @@ Template:
119
135
  Hash:
120
136
 
121
137
  {
122
- "person": true,
123
138
  "repo": [
124
139
  { "name": "resque" },
125
140
  { "name": "hub" },
@@ -129,11 +144,60 @@ Hash:
129
144
 
130
145
  Output:
131
146
 
132
- Shown!
133
147
  <b>resque</b>
134
148
  <b>hub</b>
135
149
  <b>rip</b>
136
150
 
151
+ **Lambdas**
152
+
153
+ When the value is a callable object, such as a function or lambda, the
154
+ object will be invoked and passed the block of text. The text passed
155
+ is the literal block, unrendered. `{{tags}}` will not have been expanded
156
+ - the lambda should do that on its own. In this way you can implement
157
+ filters or caching.
158
+
159
+ Template:
160
+
161
+ {{#wrapped}}
162
+ {{name}} is awesome.
163
+ {{/wrapped}}
164
+
165
+ Hash:
166
+
167
+ {
168
+ "name": "Willy",
169
+ "wrapped": function() {
170
+ return function(text) {
171
+ return "<b>" + render(text) + "</b>"
172
+ }
173
+ }
174
+ }
175
+
176
+ Output:
177
+
178
+ <b>Willy is awesome.</b>
179
+
180
+ **Non-False Values**
181
+
182
+ When the value is non-false but not a list, it will be used as the
183
+ context for a single rendering of the block.
184
+
185
+ Template:
186
+
187
+ {{#person?}}
188
+ Hi {{name}}!
189
+ {{/person?}}
190
+
191
+ Hash:
192
+
193
+ {
194
+ "person?": { "name": "Jon" }
195
+ }
196
+
197
+ Output:
198
+
199
+ Hi Jon!
200
+
137
201
 
138
202
  ### Inverted Sections
139
203
 
@@ -142,8 +206,8 @@ slash. That is `{{^person}}` begins a "person" inverted section while
142
206
  `{{/person}}` ends it.
143
207
 
144
208
  While sections can be used to render text one or more times based on the
145
- value of the key given, inverted sections may render text once based
146
- on the inverse value of the key given. That is, they will be rendered
209
+ value of the key, inverted sections may render text once based
210
+ on the inverse value of the key. That is, they will be rendered
147
211
  if the key doesn't exist, is false, or is an empty list.
148
212
 
149
213
  Template:
@@ -176,6 +240,8 @@ Will render as follows:
176
240
 
177
241
  <h1>Today.</h1>
178
242
 
243
+ Comments may contain newlines.
244
+
179
245
 
180
246
  ### Partials
181
247
 
@@ -203,29 +269,29 @@ For example, this template and partial:
203
269
 
204
270
  base.mustache:
205
271
  <h2>Names</h2>
206
- {{# names }}
207
- {{> user }}
208
- {{/ names }}
272
+ {{#names}}
273
+ {{> user}}
274
+ {{/names}}
209
275
 
210
276
  user.mustache:
211
- <strong>{{ name }}</strong>
277
+ <strong>{{name}}</strong>
212
278
 
213
279
  Can be thought of as a single, expanded template:
214
280
 
215
281
  <h2>Names</h2>
216
- {{# names }}
217
- <strong>{{ name }}</strong>
218
- {{/ names }}
282
+ {{#names}}
283
+ <strong>{{name}}</strong>
284
+ {{/names}}
219
285
 
220
286
 
221
287
  ### Set Delimiter
222
288
 
223
289
  Set Delimiter tags start with an equal sign and change the tag
224
- delimiters from {{ and }} to custom strings.
290
+ delimiters from `{{` and `}}` to custom strings.
225
291
 
226
292
  Consider the following contrived example:
227
293
 
228
- * {{ default_tags }}
294
+ * {{default_tags}}
229
295
  {{=<% %>=}}
230
296
  * <% erb_style_tags %>
231
297
  <%={{ }}=%>
@@ -254,5 +320,5 @@ Original CTemplate by Google
254
320
 
255
321
  ## SEE ALSO
256
322
 
257
- mustache(1), mustache(7), gem(1),
323
+ mustache(1), mustache(7),
258
324
  <http://mustache.github.com/>
@@ -0,0 +1,3 @@
1
+ {{#cached}}
2
+ Hi {{name}}.
3
+ {{/cached}}
@@ -0,0 +1,27 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'mustache'
3
+
4
+ class Lambda < Mustache
5
+ self.path = File.dirname(__FILE__)
6
+
7
+ attr_reader :calls
8
+
9
+ def initialize(*args)
10
+ super
11
+ @calls = 0
12
+ @cached = nil
13
+ end
14
+
15
+ def cached
16
+ lambda do |text|
17
+ return @cached if @cached
18
+
19
+ @calls += 1
20
+ @cached = render(text)
21
+ end
22
+ end
23
+ end
24
+
25
+ if $0 == __FILE__
26
+ puts Lambda.to_html(Lambda.template, :name => "Jonny")
27
+ end
@@ -323,6 +323,20 @@ data
323
323
  assert instance.compiled?
324
324
  end
325
325
 
326
+ def test_sections_returning_lambdas_get_called_with_text
327
+ view = Lambda.new
328
+ view[:name] = 'Chris'
329
+
330
+ assert_equal "Hi Chris.", view.render.chomp
331
+ assert_equal 1, view.calls
332
+
333
+ assert_equal "Hi Chris.", view.render.chomp
334
+ assert_equal 1, view.calls
335
+
336
+ assert_equal "Hi Chris.", view.render.chomp
337
+ assert_equal 1, view.calls
338
+ end
339
+
326
340
  def test_lots_of_staches
327
341
  template = "{{{{foo}}}}"
328
342
 
@@ -428,4 +442,19 @@ def indent
428
442
  end
429
443
  template
430
444
  end
445
+
446
+ def test_inherited_attributes
447
+ Object.const_set :TestNamespace, Module.new
448
+ base = Class.new(Mustache)
449
+ tmpl = Class.new(base)
450
+
451
+ {:template_path => File.expand_path('./foo'),
452
+ :template_extension => 'stache',
453
+ :view_namespace => TestNamespace,
454
+ :view_path => './foo'
455
+ }.each do |attr, value|
456
+ base.send("#{attr}=", value)
457
+ assert_equal value, tmpl.send(attr)
458
+ end
459
+ end
431
460
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mustache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 11
8
+ - 0
9
+ version: 0.11.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Chris Wanstrath
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-04-02 00:00:00 -07:00
17
+ date: 2010-04-18 00:00:00 -04:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -71,6 +76,8 @@ files:
71
76
  - test/fixtures/inner_partial.txt
72
77
  - test/fixtures/inverted_section.mustache
73
78
  - test/fixtures/inverted_section.rb
79
+ - test/fixtures/lambda.mustache
80
+ - test/fixtures/lambda.rb
74
81
  - test/fixtures/namespaced.mustache
75
82
  - test/fixtures/namespaced.rb
76
83
  - test/fixtures/nested_objects.mustache
@@ -108,18 +115,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
115
  requirements:
109
116
  - - ">="
110
117
  - !ruby/object:Gem::Version
118
+ segments:
119
+ - 0
111
120
  version: "0"
112
- version:
113
121
  required_rubygems_version: !ruby/object:Gem::Requirement
114
122
  requirements:
115
123
  - - ">="
116
124
  - !ruby/object:Gem::Version
125
+ segments:
126
+ - 0
117
127
  version: "0"
118
- version:
119
128
  requirements: []
120
129
 
121
130
  rubyforge_project:
122
- rubygems_version: 1.3.5
131
+ rubygems_version: 1.3.6
123
132
  signing_key:
124
133
  specification_version: 3
125
134
  summary: Mustache is a framework-agnostic way to render logic-free views.