inversion 1.3.1 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/Examples.md +134 -0
- data/GettingStarted.md +50 -0
- data/Guide.md +14 -0
- data/{History.rdoc → History.md} +59 -43
- data/{README.rdoc → README.md} +43 -34
- data/Tags.md +555 -0
- data/bin/inversion +4 -8
- data/lib/inversion/cli/api.rb +75 -0
- data/lib/inversion/cli/tagtokens.rb +34 -0
- data/lib/inversion/cli/tree.rb +70 -0
- data/lib/inversion/cli.rb +406 -0
- data/lib/inversion/exceptions.rb +0 -1
- data/lib/inversion/mixins.rb +10 -11
- data/lib/inversion/parser.rb +5 -6
- data/lib/inversion/refinements.rb +18 -0
- data/lib/inversion/renderstate.rb +24 -25
- data/lib/inversion/sinatra.rb +0 -1
- data/lib/inversion/template/attrtag.rb +7 -5
- data/lib/inversion/template/begintag.rb +0 -1
- data/lib/inversion/template/calltag.rb +0 -1
- data/lib/inversion/template/codetag.rb +37 -28
- data/lib/inversion/template/commenttag.rb +0 -1
- data/lib/inversion/template/configtag.rb +3 -4
- data/lib/inversion/template/containertag.rb +1 -2
- data/lib/inversion/template/defaulttag.rb +1 -2
- data/lib/inversion/template/elsetag.rb +0 -1
- data/lib/inversion/template/elsiftag.rb +0 -1
- data/lib/inversion/template/endtag.rb +2 -3
- data/lib/inversion/template/escapetag.rb +1 -2
- data/lib/inversion/template/fortag.rb +2 -3
- data/lib/inversion/template/fragmenttag.rb +1 -2
- data/lib/inversion/template/iftag.rb +3 -1
- data/lib/inversion/template/importtag.rb +2 -3
- data/lib/inversion/template/includetag.rb +1 -2
- data/lib/inversion/template/node.rb +4 -5
- data/lib/inversion/template/pptag.rb +1 -2
- data/lib/inversion/template/publishtag.rb +2 -3
- data/lib/inversion/template/rescuetag.rb +1 -2
- data/lib/inversion/template/subscribetag.rb +3 -4
- data/lib/inversion/template/tag.rb +3 -4
- data/lib/inversion/template/textnode.rb +1 -2
- data/lib/inversion/template/timedeltatag.rb +1 -2
- data/lib/inversion/template/unlesstag.rb +0 -1
- data/lib/inversion/template/uriencodetag.rb +1 -2
- data/lib/inversion/template/yieldtag.rb +0 -1
- data/lib/inversion/template.rb +18 -22
- data/lib/inversion/tilt.rb +1 -2
- data/lib/inversion.rb +2 -3
- data/lib/roda/plugins/inversion.rb +225 -0
- data/spec/helpers.rb +19 -13
- data/spec/inversion/mixins_spec.rb +1 -1
- data/spec/inversion/parser_spec.rb +1 -1
- data/spec/inversion/refinements_spec.rb +22 -0
- data/spec/inversion/renderstate_spec.rb +1 -1
- data/spec/inversion/sinatra_spec.rb +1 -1
- data/spec/inversion/template/attrtag_spec.rb +1 -1
- data/spec/inversion/template/begintag_spec.rb +1 -1
- data/spec/inversion/template/calltag_spec.rb +1 -1
- data/spec/inversion/template/codetag_spec.rb +24 -1
- data/spec/inversion/template/commenttag_spec.rb +1 -1
- data/spec/inversion/template/configtag_spec.rb +1 -1
- data/spec/inversion/template/containertag_spec.rb +1 -1
- data/spec/inversion/template/defaulttag_spec.rb +1 -1
- data/spec/inversion/template/elsetag_spec.rb +1 -1
- data/spec/inversion/template/elsiftag_spec.rb +1 -1
- data/spec/inversion/template/endtag_spec.rb +1 -1
- data/spec/inversion/template/escapetag_spec.rb +1 -1
- data/spec/inversion/template/fortag_spec.rb +1 -1
- data/spec/inversion/template/fragmenttag_spec.rb +1 -1
- data/spec/inversion/template/iftag_spec.rb +1 -1
- data/spec/inversion/template/importtag_spec.rb +1 -1
- data/spec/inversion/template/includetag_spec.rb +1 -1
- data/spec/inversion/template/node_spec.rb +1 -1
- data/spec/inversion/template/pptag_spec.rb +1 -1
- data/spec/inversion/template/publishtag_spec.rb +1 -1
- data/spec/inversion/template/rescuetag_spec.rb +1 -1
- data/spec/inversion/template/subscribetag_spec.rb +1 -1
- data/spec/inversion/template/tag_spec.rb +1 -1
- data/spec/inversion/template/textnode_spec.rb +1 -1
- data/spec/inversion/template/timedeltatag_spec.rb +1 -1
- data/spec/inversion/template/unlesstag_spec.rb +1 -1
- data/spec/inversion/template/uriencodetag_spec.rb +1 -1
- data/spec/inversion/template/yieldtag_spec.rb +1 -1
- data/spec/inversion/template_spec.rb +1 -1
- data/spec/inversion/tilt_spec.rb +1 -1
- data/spec/inversion_spec.rb +1 -1
- data/spec/roda/plugins/inversion_spec.rb +91 -0
- data.tar.gz.sig +0 -0
- metadata +90 -58
- metadata.gz.sig +0 -0
- data/Examples.rdoc +0 -134
- data/GettingStarted.rdoc +0 -44
- data/Guide.rdoc +0 -47
- data/Manifest.txt +0 -86
- data/Rakefile +0 -7
- data/Tags.rdoc +0 -560
- data/lib/inversion/command.rb +0 -278
- data/lib/inversion/monkeypatches.rb +0 -21
- data/spec/inversion/monkeypatches_spec.rb +0 -25
data/Tags.rdoc
DELETED
@@ -1,560 +0,0 @@
|
|
1
|
-
= Built-In Tags
|
2
|
-
|
3
|
-
Inversion's tags support the Pluggability[http://rubygems.org/gems/pluggability] API, allowing for the easy addition of {custom tags}[#label-Custom+Tags], but it comes with a number of built-in ones too.
|
4
|
-
|
5
|
-
|
6
|
-
== Tag Syntax
|
7
|
-
|
8
|
-
Tags can be in either of two formats:
|
9
|
-
|
10
|
-
- XML Pre-processing instruction style: <code><?tagname tagdata ?></code>
|
11
|
-
- or the same thing, but with square brackets instead: <code>[?tagname tagdata ?]</code>
|
12
|
-
|
13
|
-
The second form is especially useful if you're generating HTML and want to put an Inversion tag inside the attribute of an HTML tag, but still want the template to be well-formed:
|
14
|
-
|
15
|
-
|
16
|
-
<a href="[?call article.permalink ?]">Permalink</a>
|
17
|
-
|
18
|
-
You can mix tag forms in a single document.
|
19
|
-
|
20
|
-
|
21
|
-
== Placeholder Tags
|
22
|
-
|
23
|
-
Placeholder tags represent the main functionality of Inversion; they create a placeholder in the output text which can be filled in via a method on the template object with the same name.
|
24
|
-
|
25
|
-
|
26
|
-
=== attr
|
27
|
-
|
28
|
-
The +attr+ tag is the primary placeholder tag for injecting dynamic values into your templates. The most basic form is analogous to +attr_accessor+; it defines a method on the template object that, when set, replaces all occurrences of the tag in the template with the value:
|
29
|
-
|
30
|
-
Title: <?attr title ?>
|
31
|
-
|
32
|
-
Calling the template object's +#title=+ method will inject the stringified value into that part of the output when it's rendered, e.g.,
|
33
|
-
|
34
|
-
template.title = "How to Breed Kangaroos for Milk and Meat"
|
35
|
-
template.render
|
36
|
-
# => "Title: How to Breed Kangaroos for Milk and Meat"
|
37
|
-
|
38
|
-
The rendered values of an +attr+ tag can also be the result of calling methods on the attr value:
|
39
|
-
|
40
|
-
ISBN: <?attr book.isbn ?>
|
41
|
-
|
42
|
-
Attributes can be sprintf formatted using Ruby's String#% method:
|
43
|
-
|
44
|
-
Book price: <?attr "%0.2f" % book.price ?>
|
45
|
-
|
46
|
-
Attributes can also contain other template objects, which allows templates to be nested within each other easily.
|
47
|
-
|
48
|
-
layout = Inversion::Template.load( 'layout.tmpl' )
|
49
|
-
content = Inversion::Template.load( 'content.tmpl' )
|
50
|
-
content.caption = "Your kids will love their new Kangaroo family!"
|
51
|
-
layout.body = content
|
52
|
-
layout.render
|
53
|
-
|
54
|
-
|
55
|
-
=== call
|
56
|
-
|
57
|
-
+call+ is just an alias for +attr+. Use whichever strikes your fancy.
|
58
|
-
|
59
|
-
|
60
|
-
=== escape
|
61
|
-
|
62
|
-
+escape+ works just like +attr+, but it escapes the content inserted into the template, using the configured escaping behavior. The supported escaping behaviors are defined in a mixin called Inversion::Escaping. The behavior to use can be set using the {:escape_format}[Templates@Template+Options] option on the template or in a +config+ tag; it defaults to HTML escaping.
|
63
|
-
|
64
|
-
<p>Company name: <?escape company.name ?></p>
|
65
|
-
|
66
|
-
If the company was +"AT&T"+, the output would look like:
|
67
|
-
|
68
|
-
<p>Company name: AT&T</p>
|
69
|
-
|
70
|
-
|
71
|
-
=== uriencode
|
72
|
-
|
73
|
-
The +urlencode+ tag is another +attr+-like tag, but this one does URI encoding:
|
74
|
-
|
75
|
-
<nav>Edit <a href="/profile?name=[?uriencode person.name ?]">your profile</a></nav>
|
76
|
-
|
77
|
-
|
78
|
-
== Special Placeholders
|
79
|
-
|
80
|
-
=== timedelta
|
81
|
-
|
82
|
-
If you need to automatically generate a human-readable description of the interval between two times, you can use the +timedelta+ tag:
|
83
|
-
|
84
|
-
<article class="blogentry">
|
85
|
-
<header>
|
86
|
-
<p>Posted: <?timedelta entry.date_posted ?>.</p>
|
87
|
-
</header>
|
88
|
-
...
|
89
|
-
</article>
|
90
|
-
|
91
|
-
The tag supports any object which responds to the +#to_time+ method, so standard +Time+, +Date+, and +DateTime+ objects all work.
|
92
|
-
|
93
|
-
Dates are compared against the current time, and render to approximate descriptions of the interval, e.g.,
|
94
|
-
|
95
|
-
* 4 days ago
|
96
|
-
* about an hour from now
|
97
|
-
* 6 weeks ago
|
98
|
-
* less than a minute from now
|
99
|
-
|
100
|
-
|
101
|
-
== Inter-template Tags
|
102
|
-
|
103
|
-
These tags operate on nested templates, allowing you to selectively use or send attributes or content from other templates.
|
104
|
-
|
105
|
-
|
106
|
-
=== import
|
107
|
-
|
108
|
-
Occasionally, you'll want to compose output from several different templates by nesting them, but you don't want to have to set common objects on all of them from code. The +import+ tag lets you copy the values from a container template into one intended to be nested within it:
|
109
|
-
|
110
|
-
<!-- layout.tmpl -->
|
111
|
-
Logged in as: <?attr request.authenticated_user ?>
|
112
|
-
<?attr body ?>
|
113
|
-
|
114
|
-
<!-- body.tmpl -->
|
115
|
-
<?import request ?>
|
116
|
-
<p>You can check your balance using <a href="[?call request.path_info ?]/accounts">the
|
117
|
-
accounts tool</a>.</p>
|
118
|
-
|
119
|
-
When the content template is nested in the container, you only need to set the +request+ attribute on the container to set it in both places:
|
120
|
-
|
121
|
-
layout = Inversion::Template.load( 'layout.tmpl' )
|
122
|
-
body = Inversion::Template.load( 'body.tmpl' )
|
123
|
-
|
124
|
-
layout.body = body
|
125
|
-
layout.request = request
|
126
|
-
|
127
|
-
puts layout.render
|
128
|
-
|
129
|
-
Without the use of +import+, you'd need to similarly set the request attribute on the body template.
|
130
|
-
|
131
|
-
The imported attribute's value is determined at render time, so you can also use it to import values from an iteration.
|
132
|
-
|
133
|
-
<!-- Container template (table.tmpl)" -->
|
134
|
-
<table>
|
135
|
-
<thead>...</thead>
|
136
|
-
<tbody>
|
137
|
-
<?for user in assigned_users ?>
|
138
|
-
<?attr row ?>
|
139
|
-
<?end for ?>
|
140
|
-
</tbody>
|
141
|
-
</table>
|
142
|
-
<?end?>
|
143
|
-
|
144
|
-
<!-- Content template (row.tmpl)" -->
|
145
|
-
<?import user ?>
|
146
|
-
<tr>
|
147
|
-
<th>Username:</th><td><?escape user.username ?></td>
|
148
|
-
<th>UID:</th><td><?escape user.uid ?></td>
|
149
|
-
<th>GID:</th><td><?escape user.gid ?></td>
|
150
|
-
</tr>
|
151
|
-
<?end?>
|
152
|
-
|
153
|
-
and the code:
|
154
|
-
|
155
|
-
usertable = Inversion::Template.load( 'table.tmpl' )
|
156
|
-
userrow = Inversion::Template.load( 'row.tmpl' )
|
157
|
-
|
158
|
-
usertable.row = userrow
|
159
|
-
usertable.assigned_users = User.assigned.all
|
160
|
-
|
161
|
-
puts usertable.render
|
162
|
-
|
163
|
-
When the +row.tmpl+ is rendered each time, its imported +user+ is set to whatever the +user+ in the container is, in this case the next object in +assigned_users+.
|
164
|
-
|
165
|
-
You can import values into deeply-nested templates, provided each container imports it as well.
|
166
|
-
|
167
|
-
|
168
|
-
=== publish/subscribe
|
169
|
-
|
170
|
-
Often you'll want to set up a generic layout template to establish a global look-and-feel, and then modify it based on the content of an inner template.
|
171
|
-
|
172
|
-
==== Look and feel template (layout.tmpl)
|
173
|
-
<!DOCTYPE HTML>
|
174
|
-
<html lang="en">
|
175
|
-
<head>
|
176
|
-
<title><?subscribe title || Untitled ?></title>
|
177
|
-
<link rel="stylesheet" href="/css/base.css" type="text/css" media="screen"
|
178
|
-
title="Base Stylesheet" charset="utf-8" />
|
179
|
-
<?subscribe stylesheets ?>
|
180
|
-
|
181
|
-
<script defer="defer" src="/js/jquery-1.4.2.min.js"
|
182
|
-
type="text/javascript" charset="utf-8"></script>
|
183
|
-
<?subscribe scripts ?>
|
184
|
-
</head>
|
185
|
-
|
186
|
-
<body><?attr body ?></body>
|
187
|
-
</html>
|
188
|
-
|
189
|
-
|
190
|
-
==== A content template (content.tmpl)
|
191
|
-
<?publish title ?>I make stuff up<?end publish?>
|
192
|
-
|
193
|
-
<?publish stylesheets ?>
|
194
|
-
<link rel="stylesheet" href="/css/content.css" type="text/css" media="screen"
|
195
|
-
title="Content Style Overrides" charset="utf-8" />
|
196
|
-
<?end publish?>
|
197
|
-
|
198
|
-
<?publish scripts ?>
|
199
|
-
<script defer="defer" src="/js/content.js" type="text/javascript" charset="utf-8"></script>
|
200
|
-
<?end publish?>
|
201
|
-
|
202
|
-
<div>Hi, there.</div>
|
203
|
-
|
204
|
-
==== Template setup
|
205
|
-
|
206
|
-
layout = Inversion::Template.load( 'layout.tmpl' )
|
207
|
-
content = Inversion::Template.load( 'content.tmpl' )
|
208
|
-
|
209
|
-
layout.body = content
|
210
|
-
|
211
|
-
puts layout.render
|
212
|
-
|
213
|
-
+subscribe+ renders to an empty string if there is no matching +publish+, or to the value of a default if supplied (as in the HTML title example above.)
|
214
|
-
In this fashion, you can dynamically switch out different content pages, with each having the ability to optionally override various HTML elements.
|
215
|
-
|
216
|
-
=== include
|
217
|
-
|
218
|
-
The +include+ tag allows inclusion of other template files from within a template. This supports separation of a template into several reusable components. The included template becomes a part of the including template, along with any defaults, attributes and configuration.
|
219
|
-
|
220
|
-
==== Include setup
|
221
|
-
|
222
|
-
email = Inversion::Template.load( 'email.tmpl' )
|
223
|
-
|
224
|
-
email.greeting = "Kudos"
|
225
|
-
email.company = Company[ :spime_thorpe ]
|
226
|
-
email.user = User[ :jrandom ]
|
227
|
-
|
228
|
-
puts main.render
|
229
|
-
|
230
|
-
==== Including template (email.tmpl)
|
231
|
-
|
232
|
-
Subject: Great news, everybody!
|
233
|
-
From: <?attr company.email ?>
|
234
|
-
To: <?attr user.email ?>
|
235
|
-
|
236
|
-
<?attr greeting ?>, <?attr user.first_name ?>!
|
237
|
-
|
238
|
-
We are excited to inform you that you have been selected to participate
|
239
|
-
in a challenging and exciting career displacement opportunity!
|
240
|
-
|
241
|
-
Please attend the mandatory Man Overboard (tm) session we have scheduled
|
242
|
-
for you at 8:45AM on Thursday in the Sunshine Room. Light refreshments
|
243
|
-
and computer-aided aptitude testing will be provided.
|
244
|
-
|
245
|
-
<?include signature.tmpl ?>
|
246
|
-
|
247
|
-
==== Included template (signature.tmpl)
|
248
|
-
|
249
|
-
Sincerely,
|
250
|
-
Your Friends at <?attr company.name ?>!
|
251
|
-
|
252
|
-
==== The rendered output
|
253
|
-
|
254
|
-
Subject: Great news, everybody!
|
255
|
-
From: "Spime-Thorpe, Inc." <salesteam2@spime-thorpe.com>
|
256
|
-
To: "James Random" <jrandom@compusa.com>
|
257
|
-
|
258
|
-
Kudos, James!
|
259
|
-
|
260
|
-
We are excited to inform you that you have been selected to participate
|
261
|
-
in a challenging and exciting career displacement opportunity!
|
262
|
-
|
263
|
-
Please attend the mandatory Man Overboard (tm) session we have scheduled
|
264
|
-
for you at 8:45AM on Thursday in the Sunshine Room. Light refreshments
|
265
|
-
and computer-aided aptitude testing will be provided.
|
266
|
-
|
267
|
-
Sincerely,
|
268
|
-
Your Friends at Spime Thorpe!
|
269
|
-
|
270
|
-
|
271
|
-
=== fragment
|
272
|
-
|
273
|
-
A +fragment+ tag also sets an attribute from within the template, but under the
|
274
|
-
scope of the global template itself. A fragment can use other Inversion tags,
|
275
|
-
and the attribute is both usable elsewhere in the template, and accessible from
|
276
|
-
calling code after rendering.
|
277
|
-
|
278
|
-
template = Inversion::Template.new <<-TMPL
|
279
|
-
<?fragment subject ?>Your order status (Order #<?call order.number ?>)<?end ?>
|
280
|
-
|
281
|
-
Dear <?call order.customer.name ?>,
|
282
|
-
|
283
|
-
Your recent order was modified by our Order Fulfillment Team.
|
284
|
-
|
285
|
-
After careful deliberation, it was decided that no one should have need for that many hot dogs
|
286
|
-
with overnight shipping. Frankly, we're more than a little concerned for your health.
|
287
|
-
|
288
|
-
Sincerely,
|
289
|
-
Rowe's Meat Emporium
|
290
|
-
(Buy! Sell! Consignment!)
|
291
|
-
TMPL
|
292
|
-
|
293
|
-
template.order = order
|
294
|
-
template.render
|
295
|
-
|
296
|
-
template.fragments[ :subject ] #=> "Your order status (Order #3492)"
|
297
|
-
|
298
|
-
|
299
|
-
== Flow Control
|
300
|
-
|
301
|
-
The following tags are used to alter the flow of rendering from within templates.
|
302
|
-
|
303
|
-
|
304
|
-
=== for
|
305
|
-
|
306
|
-
The +for+ tag iterates over the objects in a collection, rendering its
|
307
|
-
template section once for each iteration. Its attribute can be set to anything
|
308
|
-
that responds to @#each@. The iteration variable(s) are scoped to the block,
|
309
|
-
and temporarily override any template attributes of the same name.
|
310
|
-
|
311
|
-
==== 'For' tag setup
|
312
|
-
|
313
|
-
overhead_list = Inversion::Template.load( 'employee_list.tmpl' )
|
314
|
-
overhead_list.users = User.
|
315
|
-
filter { start_date < 6.months.ago }.
|
316
|
-
filter { department = 'Information Technology' }
|
317
|
-
|
318
|
-
puts overhead_list.render
|
319
|
-
|
320
|
-
The +for+ tag's iteration works just like Ruby's +for+; if the enumerated
|
321
|
-
value has more than one value, you can give a list of iteration variables to
|
322
|
-
be assigned to.
|
323
|
-
|
324
|
-
==== Employee list using 'for'
|
325
|
-
|
326
|
-
<table>
|
327
|
-
<thead>...</thead>
|
328
|
-
<tbody>
|
329
|
-
<?for user, i in users.each_with_index ?>
|
330
|
-
<tr class="[?if i.even? ?]even[?else?]odd[?end if?]-row">
|
331
|
-
<td><?attr user.first_name ?></td>
|
332
|
-
<td><?attr user.last_name ?></td>
|
333
|
-
<td><?attr user.title ?></td>
|
334
|
-
<td><?attr user.start_date ?></td>
|
335
|
-
<td><?attr user.salary ?></td>
|
336
|
-
</tr>
|
337
|
-
<?end for ?>
|
338
|
-
</tbody>
|
339
|
-
</table>
|
340
|
-
|
341
|
-
The example above uses a Ruby enumerator for the +#each_with_index+ method to set the class of the row to +'even-row'+ or +'odd-row'+.
|
342
|
-
|
343
|
-
This works with the keys and values of Hashes, too:
|
344
|
-
|
345
|
-
==== Display hash of notes keyed by author using 'for'
|
346
|
-
|
347
|
-
<?for user, content in user.notes ?>
|
348
|
-
<section class="note">
|
349
|
-
<header>
|
350
|
-
Note by <?call user.username ?>
|
351
|
-
</header>
|
352
|
-
<p><?escape content ?></p>
|
353
|
-
</section>
|
354
|
-
|
355
|
-
<?end for ?>
|
356
|
-
|
357
|
-
Note that you can also use Ruby's "external iterator" syntax to iterate, too:
|
358
|
-
|
359
|
-
==== Iterate over each byte of a string with an index using 'for'
|
360
|
-
|
361
|
-
<section class="hexdump">
|
362
|
-
<?for byte, index in frame.header.each_byte.with_index ?>
|
363
|
-
<?if index.modulo(8).zero? ?>
|
364
|
-
<?if index.nonzero? ?>
|
365
|
-
</span><br />
|
366
|
-
<?end if ?>
|
367
|
-
<span class="row"><?attr "0x%08x" % index ?>:
|
368
|
-
<?end if ?>
|
369
|
-
<code><?attr "0x%02x" % byte ?></code>
|
370
|
-
<?end for ?>
|
371
|
-
</section>
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
=== if/elsif/else
|
376
|
-
|
377
|
-
The +if+ tag can be used to conditionally render a section of the template based on the value of an attribute or the value of a method called on it.
|
378
|
-
|
379
|
-
==== Conditional block
|
380
|
-
|
381
|
-
<?if user.has_stock_options? ?>
|
382
|
-
You will have 21 days to exercise your stock options.
|
383
|
-
<?else ?>
|
384
|
-
You have a week to optionally take home a handful of supplies from the
|
385
|
-
office cabinet.
|
386
|
-
<?end if ?>
|
387
|
-
|
388
|
-
|
389
|
-
=== unless
|
390
|
-
|
391
|
-
Unless is like the +if+ tag, but with inverted logic. Note that an +unless+ can have an +else+ tag, but cannot have any +elsif+ tags within it.
|
392
|
-
|
393
|
-
|
394
|
-
=== yield
|
395
|
-
|
396
|
-
The +yield+ tag is used to defer rendering of some part of the template to the code that is calling render[rdoc-ref:Inversion::Template#render] on it. If a block is passed to +#render+, then the +yield+ tag will call it with the Inversion::RenderState object that is currently in effect, and will render the return value in its place.
|
397
|
-
|
398
|
-
==== Using 'yield' to defer an expensive database lookup (report.tmpl)
|
399
|
-
|
400
|
-
<?if extra_details_enabled ?>
|
401
|
-
<?yield ?>
|
402
|
-
<?end if ?>
|
403
|
-
|
404
|
-
report = Inversion::Template.load( 'report.tmpl' )
|
405
|
-
report.extra_details_enabled = true if $DEBUG
|
406
|
-
puts report.render do
|
407
|
-
report_table = Inversion::Template.load( 'table.tmpl' )
|
408
|
-
report_table.rows = an_expensive_database_query()
|
409
|
-
report_table
|
410
|
-
end
|
411
|
-
|
412
|
-
This will insert the +report_table+ template in place of the yield, but only if $DEBUG is true.
|
413
|
-
|
414
|
-
|
415
|
-
=== begin/rescue
|
416
|
-
|
417
|
-
These tags work as you'd expect from their ruby counterparts.
|
418
|
-
|
419
|
-
The +begin+ section of the template to be rendered only if no exceptions are raised while it's
|
420
|
-
being rendered. If an exception is raised, it is checked against any +rescue+ sections, and the
|
421
|
-
first with a matching exception is rendered instead. If no +rescue+ block is found, the exception
|
422
|
-
is handled by the configured exception behavior for the template (see Inversion::Template@Template+Options).
|
423
|
-
|
424
|
-
<?begin ?><?call employees.length ?><?end?>
|
425
|
-
|
426
|
-
<?begin ?>
|
427
|
-
<?for employee in employees.all ?>
|
428
|
-
<?attr employee.name ?> --> <?attr employee.title ?>
|
429
|
-
<?end for?>
|
430
|
-
<?rescue DatabaseError => err ?>
|
431
|
-
Oh no!! I can't talk to the database for some reason. The
|
432
|
-
error was as follows:
|
433
|
-
<pre>
|
434
|
-
<?attr err.message ?>
|
435
|
-
</pre>
|
436
|
-
<?end?>
|
437
|
-
|
438
|
-
|
439
|
-
== Control Tags
|
440
|
-
|
441
|
-
There are a few tags that can be used to set values in the templating system itself.
|
442
|
-
|
443
|
-
=== config
|
444
|
-
|
445
|
-
The +config+ tag can be used to override template options[Templates@Template+Options]
|
446
|
-
on a per-template basis. It allows for convenient, inline settings from
|
447
|
-
within a template rather than from the code in which the template is
|
448
|
-
loaded.
|
449
|
-
|
450
|
-
For example, if you want to enable debugging comments on a single template:
|
451
|
-
|
452
|
-
<?config debugging_comments: true ?>
|
453
|
-
|
454
|
-
Multiple template options can be set simultaneously by using a YAML hash:
|
455
|
-
|
456
|
-
<?config
|
457
|
-
on_render_error: propagate
|
458
|
-
debugging_comments: true
|
459
|
-
comment_start: /*
|
460
|
-
comment_end: */
|
461
|
-
?>
|
462
|
-
|
463
|
-
Note that this also allows you to set multiple options on a single line, if you wrap them in braces:
|
464
|
-
|
465
|
-
<?config { comment_start: "/*", comment_end: "*/" } ?>
|
466
|
-
|
467
|
-
|
468
|
-
=== default
|
469
|
-
|
470
|
-
The +default+ tag sets an attribute from within the template,
|
471
|
-
and this value is used if the attribute is set to nil or otherwise unset.
|
472
|
-
|
473
|
-
template = Inversion::Template.new <<-TMPL
|
474
|
-
<?default adjective to "cruel" ?>
|
475
|
-
<?default noun to "world" ?>
|
476
|
-
Goodbye, <?attr adjective ?> <?attr noun ?>!
|
477
|
-
TMPL
|
478
|
-
|
479
|
-
template.render
|
480
|
-
|
481
|
-
template.adjective = "delicious"
|
482
|
-
template.render
|
483
|
-
|
484
|
-
template.adjective = nil
|
485
|
-
template.noun = "banana"
|
486
|
-
template.render
|
487
|
-
|
488
|
-
Would produce the output:
|
489
|
-
|
490
|
-
Goodbye, cruel world!
|
491
|
-
Goodbye, delicious world!
|
492
|
-
Goodbye, cruel banana!
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
== Troubleshooting/Introspection
|
497
|
-
|
498
|
-
=== pp
|
499
|
-
|
500
|
-
The +pp+ tag uses the +PP+ library to output an escaped representation of its argument.
|
501
|
-
|
502
|
-
==== Creating an object to inspect
|
503
|
-
|
504
|
-
content = Inversion::Template.load( 'content.tmpl' )
|
505
|
-
content.file = File.stat( '/tmp/example.txt' )
|
506
|
-
|
507
|
-
puts content.render
|
508
|
-
|
509
|
-
==== Inspecting an object from within a template (content.tmpl)
|
510
|
-
|
511
|
-
<div class="debugging">
|
512
|
-
The file's stat attributes:
|
513
|
-
<?pp file ?>
|
514
|
-
</div>
|
515
|
-
|
516
|
-
The output is escaped according to the current setting of the {:escape_format}[rdoc-ref:Templates@Template+Options] option.
|
517
|
-
|
518
|
-
==== The rendered result
|
519
|
-
|
520
|
-
<div class="debugging">
|
521
|
-
The file's stat attributes:
|
522
|
-
#<File::Stat
|
523
|
-
dev=0xe000004,
|
524
|
-
ino=3064556,
|
525
|
-
mode=0100644 (file rw-r--r--),
|
526
|
-
nlink=1,
|
527
|
-
uid=501 (mahlon),
|
528
|
-
gid=0 (wheel),
|
529
|
-
rdev=0x0 (0, 0),
|
530
|
-
size=0,
|
531
|
-
blksize=4096,
|
532
|
-
blocks=0,
|
533
|
-
atime=2011-08-12 08:43:15 -0700 (1313163795),
|
534
|
-
mtime=2011-08-12 08:43:15 -0700 (1313163795),
|
535
|
-
ctime=2011-08-12 08:43:15 -0700 (1313163795)></div>
|
536
|
-
</div>
|
537
|
-
|
538
|
-
|
539
|
-
== Custom Tags
|
540
|
-
|
541
|
-
We have a lot of documentation work to do for this still, but the basics are:
|
542
|
-
|
543
|
-
* Create a file with the path <code>lib/inversion/template/«yourtagnametag».rb/code>
|
544
|
-
|
545
|
-
* Name your class Inversion::Template::«yourtagname.capitalize»Tag
|
546
|
-
|
547
|
-
* Subclass one of the abstract template tag types:
|
548
|
-
|
549
|
-
* If your tag body will contain code, subclass Inversion::Template::CodeTag
|
550
|
-
|
551
|
-
* Otherwise, subclass Inversion::Template::Tag
|
552
|
-
|
553
|
-
* If your tag is a container, include Inversion::Template::ContainerTag
|
554
|
-
|
555
|
-
* Read the documentation for the tag type you're subclassing for the methods you're
|
556
|
-
required to implement.
|
557
|
-
|
558
|
-
Unfortunately, the tag superclasses aren't currently documented very well, so
|
559
|
-
the best way to accomplish what you want is to find an existing tag that does
|
560
|
-
something similar and look at how it does it.
|