inversion 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data.tar.gz.sig +0 -0
  2. data/Manifest.txt +45 -0
  3. data/lib/inversion.rb +2 -2
  4. data/lib/inversion/template/begintag.rb +122 -0
  5. data/lib/inversion/template/defaulttag.rb +135 -0
  6. data/lib/inversion/template/rescuetag.rb +92 -0
  7. data/lib/inversion/template/timedeltatag.rb +93 -0
  8. data/manual/layouts/default.erb +87 -0
  9. data/manual/lib/api-filter.rb +96 -0
  10. data/manual/lib/editorial-filter.rb +59 -0
  11. data/manual/lib/examples-filter.rb +238 -0
  12. data/manual/lib/links-filter.rb +111 -0
  13. data/manual/resources/css/manual.css +764 -0
  14. data/manual/resources/fonts/GraublauWeb.otf +0 -0
  15. data/manual/resources/fonts/GraublauWebBold.otf +0 -0
  16. data/manual/resources/fonts/Inconsolata.otf +0 -0
  17. data/manual/resources/images/arrow_225_small.png +0 -0
  18. data/manual/resources/images/arrow_315_small.png +0 -0
  19. data/manual/resources/images/arrow_skip.png +0 -0
  20. data/manual/resources/images/cc-by.png +0 -0
  21. data/manual/resources/images/dialog-error.png +0 -0
  22. data/manual/resources/images/dialog-information.png +0 -0
  23. data/manual/resources/images/dialog-warning.png +0 -0
  24. data/manual/resources/images/emblem-important.png +0 -0
  25. data/manual/resources/images/help.png +0 -0
  26. data/manual/resources/images/information.png +0 -0
  27. data/manual/resources/images/magnifier.png +0 -0
  28. data/manual/resources/images/magnifier_left.png +0 -0
  29. data/manual/resources/images/page_white_code.png +0 -0
  30. data/manual/resources/images/page_white_copy.png +0 -0
  31. data/manual/resources/images/printer.png +0 -0
  32. data/manual/resources/images/question.png +0 -0
  33. data/manual/resources/images/scripts_code.png +0 -0
  34. data/manual/resources/images/wrap.png +0 -0
  35. data/manual/resources/images/wrapping.png +0 -0
  36. data/manual/resources/js/jquery-1.4.4.min.js +167 -0
  37. data/manual/resources/js/manual.js +30 -0
  38. data/manual/resources/js/sh.js +580 -0
  39. data/manual/resources/swf/clipboard.swf +0 -0
  40. data/manual/src/examples.page +154 -0
  41. data/manual/src/gettingstarted.page +64 -0
  42. data/manual/src/index.page +97 -0
  43. data/manual/src/tags.page +501 -0
  44. data/manual/src/templates.page +74 -0
  45. data/spec/inversion/template/begintag_spec.rb +217 -0
  46. data/spec/inversion/template/defaulttag_spec.rb +60 -0
  47. data/spec/inversion/template/rescuetag_spec.rb +109 -0
  48. data/spec/inversion/template/timedeltatag_spec.rb +215 -0
  49. metadata +72 -27
  50. metadata.gz.sig +0 -0
Binary file
@@ -0,0 +1,154 @@
1
+ ---
2
+ # vim: set et nosta sw=4 ts=4 ft=textile :
3
+ title: Annotated Examples
4
+ layout: default
5
+ index: 3
6
+ filters:
7
+ - erb
8
+ - links
9
+ - examples
10
+ - editorial
11
+ - api
12
+ - textile
13
+ ---
14
+
15
+ h2. <%= page.config['title'] %>
16
+
17
+ <div id="auto-toc"></div>
18
+
19
+ This is a list of template examples with annotations explaining what each group of lines is doing.
20
+
21
+ h3(#simple-examples). Simple Examples
22
+
23
+ ...
24
+
25
+
26
+ h3(#advanced-examples). Advanced Examples
27
+
28
+ Here's a somewhat more complex example. At our company Spime-Thorpe from above, say we're creating a system that will handle mass layoffs without the need for management to handle all those messy personal interactions. We'll need a mass-mailer for the employees that will be afforded the chance to explore their career opportunities, right? So we create a template called @overboard-mail.tmpl@:
29
+
30
+ <?example { language: xml, caption: "overboard-mail.tmpl" } ?>
31
+ <?config debugging_comments: true ?>
32
+ <?default grace_period to "7 days" ?>
33
+
34
+ <?begin?>
35
+ <p>Dear <?call employee.fullname ?>,</p>
36
+ <?rescue DatabaseError ?>
37
+ <p>Dear Valued Ex-Employee,</p>
38
+ <?end begin?>
39
+
40
+ <p>Congratulations! You have been selected by <?call failed_company.name ?>'s
41
+ elite management team as one of the many lucky individuals that will
42
+ enjoy the exciting challenge of pursuing other rewarding employment
43
+ opportunities!</p>
44
+
45
+ <p><em>Kudos!</em></p>
46
+
47
+ <p>You will find your accounts have been disabled, your desk has been helpfully
48
+ cleared out for you, and all of your personal effects packaged up and
49
+ delivered to your address of record approximately
50
+ <?timedelta tracking_info.delivery_date ?>:</p>
51
+
52
+ <?for line in employee.address ?>
53
+ <?attr line ?><br />
54
+ <?end for ?>
55
+
56
+ <p>Please visit your <a href="[?call config.overboard_url ?]/[?uriencode
57
+ failed_company.id ?]/[?uriencode employee.id ?]">customized Man OverBoard
58
+ transition site</a> immediately:</p>
59
+
60
+ <p>This will acknowledge that you have received this message, and automatically
61
+ disable your email account. Be sure and save this message!</p>
62
+
63
+ <?if employee.severance_amount.nonzero? ?>
64
+ <p>Failure to acknowledge this message within <?attr grace_period ?> could result
65
+ in delay of your final severance pay, in the amount of
66
+ <?call "$%0.2f" % employee.severance_amount ?>.</p>
67
+ <?else?>
68
+ <p>Failure to acknowledge this message within <?attr grace_period ?> will
69
+ result in automatic forfeiture of your numerous Man Overboard package benefits.</p>
70
+ <?end if?>
71
+
72
+ <?comment Disabled at client request ?>
73
+ If you have any questions or concerns, please don't hesitate to contact your
74
+ friendly Spime-Thorpe <a href="mailto:salesteam2@spime-thorpe.com">representative</a>.
75
+ <?end comment ?>
76
+
77
+ <p>Good Luck,<br />
78
+ Your friends at Spime-Thorpe, Inc!<br />
79
+ <a href="http://www.spime-thorpe.com/">http://www.spime-thorpe.com/</a></p>
80
+
81
+ <?end ?>
82
+
83
+ When wrapped with a layout template, here's what this renders as:
84
+
85
+ <?example { language: xml, caption: "The rendered output for one lucky individual" } ?>
86
+ <!DOCTYPE html>
87
+ <!--
88
+
89
+ Spime-Thorpe!
90
+ $Id: examples.page,v 9613e2c60530 2011/08/15 17:45:23 ged $
91
+
92
+ -->
93
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
94
+ <head>
95
+ <title>Spime-Thorpe: Untitled</title>
96
+
97
+ <link rel="stylesheet" src="/css/base.css" type="text/css" />
98
+
99
+ <script type="text/javascript" src="/js/jquery-latest.min.js"></script>
100
+
101
+ </head>
102
+ <body>
103
+
104
+ <header>
105
+ <hgroup>
106
+ <h1>Spime-Thorpe</h1>
107
+ <h2></h2>
108
+ </hgroup>
109
+ </header>
110
+
111
+ <section id="content">
112
+ <p>Dear James Random,</p>
113
+
114
+ <p>Congratulations! You have been selected by Widgets R Us's
115
+ elite management team as one of the many lucky individuals that will
116
+ enjoy the exciting challenge of pursuing other rewarding employment
117
+ opportunities!</p>
118
+
119
+ <p><em>Kudos!</em></p>
120
+
121
+ <p>You will find your accounts have been disabled, your desk has been helpfully
122
+ cleared out for you, and all of your personal effects packaged up and
123
+ delivered to your address of record approximately
124
+ 3 days ago:</p>
125
+
126
+ 1213 NE. Winding Road<br />
127
+ Syracuse, NY 100213<br />
128
+
129
+ <p>Please visit your <a href="http://failedcompany.spime-thorpe.com/overboard/a18661/1881">
130
+ customized Man OverBoard transition site</a> immediately:</p>
131
+
132
+ <p>This will acknowledge that you have received this message, and automatically
133
+ disable your email account. Be sure and save this message!</p>
134
+
135
+ <p>Failure to acknowledge this message within 11 days could result
136
+ in delay of your final severance pay, in the amount of
137
+ $12.81.</p>
138
+
139
+
140
+ <p>Good Luck,<br />
141
+ Your friends at Spime-Thorpe, Inc!<br />
142
+ <a href="http://www.spime-thorpe.com/">http://www.spime-thorpe.com/</a></p>
143
+ </section>
144
+
145
+ <footer>
146
+ <section id="copyright">Copyright 2011, Spime-Thorpe</section>
147
+ </footer>
148
+
149
+ </body>
150
+ </html>
151
+ <?end ?>
152
+
153
+ This example can be found in the Inversion repository, in the @experiments@ directory.
154
+
@@ -0,0 +1,64 @@
1
+ ---
2
+ # vim: set et nosta sw=4 ts=4 ft=textile :
3
+ title: Getting Started
4
+ layout: default
5
+ index: 1
6
+ filters:
7
+ - erb
8
+ - links
9
+ - examples
10
+ - textile
11
+ ---
12
+
13
+ h2. <%= page.config['title'] %>
14
+
15
+ <div id="auto-toc"></div>
16
+
17
+ h3(#requirements). Requirements
18
+
19
+ - Ruby 1.9.2 or later
20
+
21
+ h3(#installation). Installation
22
+
23
+ $ gem install inversion
24
+
25
+ h3(#usage). Basic Usage
26
+
27
+ Inversion, like most other templating systems, works by giving you a way of defining the static
28
+ parts of your output, and then letting you combine that at a later point with the dynamic parts:
29
+
30
+ Create the template and use it to render an exciting message:
31
+
32
+ <?example { syntax: ruby, caption: "Creating a template" } ?>
33
+ tmpl = Inversion::Template.new( "Hello, <?attr name ?>!" )
34
+ tmpl.name = "World"
35
+ puts tmpl.render
36
+ <?end?>
37
+
38
+ The <tt><?attr name ?></tt> tag defines the _name_ accessor on the <?link "template":Templates ?>
39
+ object, the value of which is substituted for any occurrences of @name@ in the template:
40
+
41
+ <?example { syntax: text } ?>
42
+ Hello, World!
43
+ <?end ?>
44
+
45
+ This by itself isn't fantastically useful, but it does illustrate one of the ways in which Inversion
46
+ is different: the program and the template share data through an API, instead of through a complex
47
+ data structure, which establishes a clear delineation between what responsibility is the program's
48
+ and which is the template's. The program doesn't have to know how the view uses the data it's given,
49
+ and tests of the controller can substitute a Mock Object for the template to test the interaction
50
+ between the two instead of having to match patterns in the eventual output like an integration test.
51
+
52
+ You can also interact with the values set in the template:
53
+
54
+ <?example { syntax: text } ?>
55
+ Name: <?attr employee.full_name ?>
56
+ <?end ?>
57
+
58
+ This will call the @#full_name@ method on whatever is set as the @employee@ attribute when
59
+ rendered, and the result will take the place of the tag.
60
+
61
+ Inversion also comes with a collection of <?link "other tags":Tags ?> that
62
+ provide flow control, exception-handling, etc.
63
+
64
+
@@ -0,0 +1,97 @@
1
+ ---
2
+ # vim: set et nosta sw=4 ts=4 ft=textile :
3
+ title: Inversion User's Guide
4
+ layout: default
5
+ index: 1
6
+ filters:
7
+ - erb
8
+ - links
9
+ - examples
10
+ - textile
11
+ ---
12
+
13
+ h2. <%= page.config['title'] %>
14
+
15
+ <div id="auto-toc"></div>
16
+
17
+ Inversion is a templating system for Ruby. It uses the Inversion of Control principle to decouple
18
+ the contents and structure of the template from the code that uses it, making it easier to use,
19
+ test-friendly, and clean.
20
+
21
+ # <?link Getting Started ?>
22
+ ## <?link "Requirements":Getting Started #requirements ?>
23
+ ## <?link "Installation":Getting Started #installation ?>
24
+ ## <?link "Basic Usage":Getting Started #usage ?>
25
+ # <?link Templates ?>
26
+ ## <?link "Options":Templates#options ?>
27
+ # <?link Tags ?>
28
+ ## <?link "Tag Syntax":Tags#tagsyntax ?>
29
+ ## <?link "Comment":Tags#comment ?>
30
+ ## <?link "Config":Tags#config ?>
31
+ ## <?link "Placeholders":Tags#placeholders ?>
32
+ ### <?link "Attr":Tags#attr ?>
33
+ ### <?link "Call":Tags#call ?>
34
+ ### <?link "Escape":Tags#escape ?>
35
+ ### <?link "Urlencode":Tags#urlencode ?>
36
+ ## <?link "Special Placeholders":Tags#special-placeholders ?>
37
+ ### <?link "TimeDelta":Tags#timedelta ?>
38
+ ## <?link "Import/Export":Tags#import-export ?>
39
+ ### <?link "Import":Tags#import ?>
40
+ ### <?link "Export":Tags#export ?>
41
+ ### <?link "Include":Tags#include ?>
42
+ ### <?link "Render":Tags#render ?>
43
+ ## <?link "Flow Control":Tags#flowcontrol ?>
44
+ ### <?link "For":Tags#for ?>
45
+ ### <?link "If/Elsif/Else":Tags#if ?>
46
+ ### <?link "Unless":Tags#unless ?>
47
+ ### <?link "Yield":Tags#yield ?>
48
+ ## <?link "Troubleshooting/Introspection":Tags#troubleshooting ?>
49
+ ### <?link "PP":Tags#pp ?>
50
+ ### <?link "PrettyPrint":Tags#prettyprint ?>
51
+
52
+ # <?link Annotated Examples ?>
53
+ ## <?link "Simple Examples":Annotated Examples#simple-examples ?>
54
+ ## <?link "Advanced Examples":Annotated Examples#advanced-examples ?>
55
+
56
+
57
+ h3. Authors
58
+
59
+ * Michael Granger
60
+ * Mahlon E. Smith
61
+
62
+
63
+ h3. License
64
+
65
+ Copyright © 2011, Michael Granger, Mahlon E. Smith
66
+ All rights reserved.
67
+
68
+ Redistribution and use in source and binary forms, with or without modification, are
69
+ permitted provided that the following conditions are met:
70
+
71
+ * Redistributions of source code must retain the above copyright notice, this list of
72
+ conditions and the following disclaimer.
73
+ * Redistributions in binary form must reproduce the above copyright notice, this list of
74
+ conditions and the following disclaimer in the documentation and/or other materials
75
+ provided with the distribution.
76
+ * Neither the name of the authors nor contributors may be used to endorse or promote products
77
+ derived from this software without specific prior written permission.
78
+
79
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
80
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
81
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
82
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
83
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
84
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
86
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
87
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88
+
89
+ <div id="cc-license">
90
+ <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">
91
+ !http://i.creativecommons.org/l/by/3.0/88x31.png(Creative Commons License)!
92
+ </a><br/>
93
+ The content of this manual, including images, video, and any example source code is
94
+ licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">Creative
95
+ Commons Attribution 3.0 License</a>.
96
+ </div>
97
+
@@ -0,0 +1,501 @@
1
+ ---
2
+ # vim: set et nosta sw=4 ts=4 ft=textile :
3
+ title: Tags
4
+ layout: default
5
+ index: 1
6
+ filters:
7
+ - erb
8
+ - links
9
+ - examples
10
+ - textile
11
+ ---
12
+
13
+ h2. <%= page.config['title'] %>
14
+
15
+ <div id="auto-toc"></div>
16
+
17
+ h3(#tagsyntax). Tag Syntax
18
+
19
+ Tags can be in either of two formats:
20
+
21
+ * XML Pre-processing instruction style: @<?tagname tagdata ?>@
22
+ * or the same thing, but with square brackets instead: @[?tagname tagdata ?]@
23
+
24
+ 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:
25
+
26
+ <?example { syntax: html, caption: "Using square-bracket tags inside an HTML tag" } ?>
27
+ <a href="[?call article.permalink ?]">Permalink</a>
28
+ <?end ?>
29
+
30
+ You can mix tag forms in a single document.
31
+
32
+ h3(#comment). Comment
33
+
34
+ The @comment@ tag can be used to temporarily stop a section of a document from being rendered:
35
+
36
+ <?example { syntax: html, caption: "Using square-bracket tags inside an HTML tag" } ?>
37
+ <?comment?>
38
+ This stuff won't be rendered.
39
+ <?end comment ?>
40
+ But this stuff will.
41
+ <?end?>
42
+
43
+ Note that the tags inside the comment will still be parsed, so the template's interface won't be affected by the comment:
44
+
45
+ <?example { syntax: html, caption: "Commenting out an attr tag" } ?>
46
+ <?comment?>
47
+ <?attr content ?>
48
+ <?end comment ?>
49
+ <?end?>
50
+
51
+ If you have @debugging_comments@ enabled in the template <?link "options":Templates#options ?>, sections which are commented out will be rendered as a comment describing what was omitted, surrounded by the comment characters set in @comment_start@ and @comment_end@ <?link "options":Templates#options ?>, respectively.
52
+
53
+ <?example { syntax: html, caption: "Rendered comment section" } ?>
54
+ <!-- Commented out 1 nodes on line 1 -->
55
+ <?end?>
56
+
57
+ You can also embed a description of why the section is commented in the tag body, which will be used as a label when rendering the comment:
58
+
59
+ <?example { syntax: html, caption: "Commenting with a label" } ?>
60
+ <?comment Disabled until there's content ?>
61
+ <?attr content ?>
62
+ <?end comment ?>
63
+ <?end?>
64
+
65
+ <?example { syntax: html, caption: "Rendered comment with a label" } ?>
66
+ <!-- Commented out 1 nodes on line 1: Disabled until there's content -->
67
+ <?end?>
68
+
69
+ h3(#config). Config
70
+
71
+ The @config@ tag can be used to override template <?link "options":Templates#options ?>
72
+ on a per-template basis. It allows for convenient, inline settings from
73
+ within a template rather than from the code in which the template is
74
+ loaded.
75
+
76
+ For example, if you want to enable debugging comments on a single template:
77
+
78
+ <?example { syntax: html, caption: "Enable debugging comments via the config tag" } ?>
79
+ <?config debugging_comments: true ?>
80
+ <?end?>
81
+
82
+ Multiple template options can be set simultaneously by using a YAML hash:
83
+
84
+ <?example { syntax: html, caption: "Changing multiple template options" } ?>
85
+ <?config
86
+ on_render_error: propagate
87
+ debugging_comments: true
88
+ comment_start: /*
89
+ comment_end: */
90
+ ?>
91
+ <?end?>
92
+
93
+ Note that this also allows you to set multiple options on a single line, when wrapped in braces:
94
+
95
+ <?example { syntax: html, caption: "Changing multiple template options on one line" } ?>
96
+ <?config { comment_start: "/*", comment_end: "*/" } ?>
97
+ <?end?>
98
+
99
+
100
+ h3(#placeholders). Placeholders
101
+
102
+ h4(#attr). Attr
103
+
104
+ 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:
105
+
106
+ <?example { syntax: html, caption: "A basic template attribute" } ?>
107
+ Title: <?attr title ?>
108
+ <?end?>
109
+
110
+ Calling the template object's @#title=@ method will inject the stringified value into that part of the output when it's rendered:
111
+
112
+ <?example { syntax: ruby, caption: "Setting the title attribute" } ?>
113
+ template.title = "How to Breed Kangaroos for Milk and Meat"
114
+ template.render
115
+ # => "Title: How to Breed Kangaroos for Milk and Meat"
116
+ <?end?>
117
+
118
+ The rendered values of an @attr@ tag can also be the result of calling methods on the attr value:
119
+
120
+ <?example { syntax: html, caption: "An attr tag that calls a method" } ?>
121
+ ISBN: <?attr book.isbn ?>
122
+ <?end?>
123
+
124
+ Attributes can be sprintf formatted:
125
+
126
+ <?example { syntax: html, caption: "An attr tag with formatting" } ?>
127
+ Book price: <?attr "%0.2f" % book.price ?>
128
+ <?end?>
129
+
130
+ Attributes can also contain other template objects, which allows templates to be nested within each other easily.
131
+
132
+ <?example { syntax: ruby, caption: "Nesting templates" } ?>
133
+ layout = Inversion::Template.load( 'layout.tmpl' )
134
+ content = Inversion::Template.load( 'content.tmpl' )
135
+ content.caption = "Your kids will love their new Kangaroo family!"
136
+ layout.body = content
137
+ layout.render
138
+ <?end?>
139
+
140
+
141
+ h4(#call). Call
142
+
143
+ @call@ is just an alias for @attr@. Use whichever strikes your fancy.
144
+
145
+ h4(#escape). Escape
146
+
147
+ @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 <?link ":escape_format":Templates#options ?> option on the template, or in a @config@ tag; it defaults to using HTML escaping.
148
+
149
+ <?example { syntax: html, caption: "Using the escape tag" } ?>
150
+ <p>Company name: <?escape company.name ?></p>
151
+ <?end?>
152
+
153
+ If the company was AT&T, the output would look like:
154
+
155
+ <?example { syntax: html, caption: "Rendered escape tag" } ?>
156
+ <p>Company name: AT&amp;T</p>
157
+ <?end?>
158
+
159
+
160
+ h4(#urlencode). Uriencode
161
+
162
+ The @urlencode@ tag is another @attr@-like tag, but this one does URI encoding:
163
+
164
+ <?example { syntax: html, caption: "Using the uriencode tag" } ?>
165
+ <nav>Edit <a href="/profile?name=[?uriencode person.name ?]">your profile</a></nav>
166
+ <?end?>
167
+
168
+
169
+ h3(#special-placeholders). Special Placeholders
170
+
171
+ h4(#timedelta). TimeDelta
172
+
173
+ If you need to automatically generate a human-readable description of the interval between two times, you can use the @timedelta@ tag:
174
+
175
+ <?example { syntax: html, caption: "Using the timedelta tag" } ?>
176
+ <article class="blogentry">
177
+ <header>
178
+ <p>Posted: <?timedelta entry.date_posted ?>.</p>
179
+ </header>
180
+ ...
181
+ </article>
182
+ <?end?>
183
+
184
+ The tag supports any object which support the @#to_time@ method, so @Time@, @Date@, and @DateTime@ and anything else that quacks similarly.
185
+
186
+ Dates are compared against the current time, and render to approximate descriptions of the interval:
187
+
188
+ * 4 days ago
189
+ * about an hour from now
190
+ * 6 weeks ago
191
+ * less than a minute from now
192
+
193
+
194
+ h3(#import-pubsub). Import and Publish/Subscribe
195
+
196
+ Both of these tags operate on nested templates: one copies objects from an outer template into an inner one (@import@), and the other publishes sections of content from one template to any other subscribed template.
197
+
198
+ h4(#import). Import
199
+
200
+ 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:
201
+
202
+ <?example { syntax: html, caption: "Container template (layout.tmpl)" } ?>
203
+ Logged in as: <?attr request.authenticated_user ?>
204
+ <?attr body ?>
205
+ <?end?>
206
+
207
+ <?example { syntax: html, caption: "Content template (body.tmpl)" } ?>
208
+ <?import request ?>
209
+ <p>You can check your balance using <a href="[?call request.path_info ?]/accounts">the accounts tool</a>.</p>
210
+ <?end?>
211
+
212
+ 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:
213
+
214
+ <?example { syntax: ruby, caption: "" } ?>
215
+ layout = Inversion::Template.load( 'layout.tmpl' )
216
+ body = Inversion::Template.load( 'body.tmpl' )
217
+
218
+ layout.body = body
219
+ layout.request = request
220
+
221
+ puts layout.render
222
+ <?end?>
223
+
224
+ Without the use of @import@, you'd need to similarly set the request attribute on the body template.
225
+
226
+ The imported attribute's value is determined at render time, so you can also use it to import values from an iteration.
227
+
228
+ <?example { syntax: html, caption: "Container template (table.tmpl)" } ?>
229
+ <table>
230
+ <thead>...</thead>
231
+ <tbody>
232
+ <?for user in assigned_users ?>
233
+ <?attr row ?>
234
+ <?end for ?>
235
+ </tbody>
236
+ </table>
237
+ <?end?>
238
+
239
+ <?example { syntax: html, caption: "Content template (row.tmpl)" } ?>
240
+ <?import user ?>
241
+ <tr>
242
+ <th>Username:</th><td><?escape user.username ?></td>
243
+ <th>UID:</th><td><?escape user.uid ?></td>
244
+ <th>GID:</th><td><?escape user.gid ?></td>
245
+ </tr>
246
+ <?end?>
247
+
248
+ <?example { syntax: ruby, caption: "Template setup" } ?>
249
+ usertable = Inversion::Template.load( 'table.tmpl' )
250
+ userrow = Inversion::Template.load( 'row.tmpl' )
251
+
252
+ usertable.row = userrow
253
+ usertable.assigned_users = User.assigned.all
254
+
255
+ puts usertable.render
256
+ <?end?>
257
+
258
+ 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@.
259
+
260
+ You can import values into deeply-nested templates, provided each container imports it as well.
261
+
262
+
263
+ h4(#publish-subscribe). Publish/Subscribe
264
+
265
+ 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.
266
+
267
+ <?example { syntax: html, caption: "Look and feel template (layout.tmpl)" } ?>
268
+ <!DOCTYPE HTML>
269
+ <html lang="en">
270
+ <head>
271
+ <title><?subscribe title || Untitled ?></title>
272
+ <link rel="stylesheet" href="/css/base.css" type="text/css" media="screen"
273
+ title="Base Stylesheet" charset="utf-8" />
274
+ <?subscribe stylesheets ?>
275
+
276
+ <script defer="defer" src="/js/jquery-1.4.2.min.js" type="text/javascript" charset="utf-8"></script>
277
+ <?subscribe scripts ?>
278
+ </head>
279
+
280
+ <body><?attr body ?></body>
281
+ </html>
282
+ <?end?>
283
+
284
+
285
+ <?example { syntax: html, caption: "A content template (content.tmpl)" } ?>
286
+ <?publish title ?>I make stuff up<?end publish?>
287
+
288
+ <?publish stylesheets ?>
289
+ <link rel="stylesheet" href="/css/content.css" type="text/css" media="screen"
290
+ title="Content Style Overrides" charset="utf-8" />
291
+ <?end publish?>
292
+
293
+ <?publish scripts ?>
294
+ <script defer="defer" src="/js/content.js" type="text/javascript" charset="utf-8"></script>
295
+ <?end publish?>
296
+
297
+ <div>Hi, there.</div>
298
+ <?end?>
299
+
300
+ <?example { syntax: ruby, caption: "Template setup" } ?>
301
+ layout = Inversion::Template.load( 'layout.tmpl' )
302
+ content = Inversion::Template.load( 'content.tmpl' )
303
+
304
+ layout.body = content
305
+
306
+ puts layout.render
307
+ <?end?>
308
+
309
+ @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.)
310
+ In this fashion, you can dynamically switch out different content pages, with each having the ability to optionally override various HTML elements.
311
+
312
+ h4(#include). Include
313
+
314
+ 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.
315
+
316
+ <?example { syntax: ruby, caption: "Include setup" } ?>
317
+ email = Inversion::Template.load( 'email.tmpl' )
318
+
319
+ email.greeting = "Kudos"
320
+ email.company = Company[ :spime_thorpe ]
321
+ email.user = User[ :jrandom ]
322
+
323
+ puts main.render
324
+ <?end?>
325
+
326
+ <?example { syntax: html, caption: "Including template (email.tmpl)" } ?>
327
+ Subject: Great news, everybody!
328
+ From: <?attr company.email ?>
329
+ To: <?attr user.email ?>
330
+
331
+ <?attr greeting ?>, <?attr user.first_name ?>!
332
+
333
+ We are excited to inform you that you have been selected to participate
334
+ in a challenging and exciting career displacement opportunity!
335
+
336
+ Please attend the mandatory Man Overboard (tm) session we have scheduled
337
+ for you at 8:45AM on Thursday in the Sunshine Room. Light refreshments
338
+ and computer-aided aptitude testing will be provided.
339
+
340
+ <?import "signature.tmpl" ?>
341
+ <?end?>
342
+
343
+ <?example { syntax: html, caption: "Included template (signature.tmpl)" } ?>
344
+
345
+ Sincerely,
346
+ Your Friends at <?attr company.name ?>!
347
+ <?end?>
348
+
349
+ <?example { syntax: text, caption: "The rendered output" } ?>
350
+ Subject: Great news, everybody!
351
+ From: "Spime-Thorpe, Inc." <salesteam2@spime-thorpe.com>
352
+ To: "James Random" <jrandom@compusa.com>
353
+
354
+ Kudos, James!
355
+
356
+ We are excited to inform you that you have been selected to participate
357
+ in a challenging and exciting career displacement opportunity!
358
+
359
+ Please attend the mandatory Man Overboard (tm) session we have scheduled
360
+ for you at 8:45AM on Thursday in the Sunshine Room. Light refreshments
361
+ and computer-aided aptitude testing will be provided.
362
+
363
+ Sincerely,
364
+ Your Friends at Spime Thorpe!
365
+ <?end?>
366
+
367
+ h4(#render). Render
368
+
369
+ #TODO
370
+
371
+ h3(#flowcontrol). Flow Control
372
+
373
+ The following tags are used to alter the flow of rendering from within templates.
374
+
375
+ h4(#for). For
376
+
377
+ The @for@ tag iterates over the objects in a collection, rendering its
378
+ template section once for each iteration. Its attribute can be set to anything
379
+ that responds to @#each@. The iteration variable(s) are scoped to the block,
380
+ and temporarily override any template attributes of the same name.
381
+
382
+ <?example { syntax: ruby, caption: "'For' tag setup" } ?>
383
+ overhead_list = Inversion::Template.load( 'employee_list.tmpl' )
384
+ overhead_list.users = User.
385
+ filter( :start_date < 6.months.ago ).
386
+ filter( :department => 'Information Technology' ).all
387
+
388
+ puts overhead_list.render
389
+ <?end?>
390
+
391
+ The @for@ tag's iteration works just like Ruby's @for@; if the enumerated
392
+ value has more than one value, you can give a list of iteration variables to
393
+ be assigned to.
394
+
395
+ <?example { syntax: html, caption: "Employee list using 'For'" } ?>
396
+ <table>
397
+ <thead>...</thead>
398
+ <tbody>
399
+ <?for user, i in users.each_with_index ?>
400
+ <tr class="[?if i.even? ?]even[?else?]odd[?end if?]-row">
401
+ <td><?attr user.first_name ?></td>
402
+ <td><?attr user.last_name ?></td>
403
+ <td><?attr user.title ?></td>
404
+ <td><?attr user.start_date ?></td>
405
+ <td><?attr user.salary ?></td>
406
+ </tr>
407
+ <?end for ?>
408
+ </tbody>
409
+ </table>
410
+ <?end?>
411
+
412
+ 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'@.
413
+
414
+
415
+ h4(#if). If/Elsif/Else
416
+
417
+ 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.
418
+
419
+ <?example { syntax: html, caption: "Conditional block" } ?>
420
+ <?if user.has_stock_options? ?>
421
+ You will have 21 days to exercise your stock options.
422
+ <?else ?>
423
+ You have a week to optionally take home a handful of supplies from the
424
+ office cabinet.
425
+ <?end if ?>
426
+ <?end?>
427
+
428
+
429
+ h4(#unless). Unless
430
+
431
+ 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.
432
+
433
+
434
+ h4(#yield). Yield
435
+
436
+ The @yield@ tag is used to defer rendering of some part of the template to the code that is calling <?api Inversion::Template#render:"#render" ?> on it. If a block is passed to @#render@, then the @yield@ tag will call it with the <?api Inversion::RenderState ?> object that is currently in effect, and will render the return value in its place.
437
+
438
+ <?example { syntax: html, caption: "Using 'yield' to defer an expensive database lookup (report.tmpl)" } ?>
439
+ <?if extra_details_enabled ?>
440
+ <?yield ?>
441
+ <?end if ?>
442
+
443
+ <?example { syntax: ruby, caption: "" } ?>
444
+ report = Inversion::Template.load( 'report.tmpl' )
445
+ report.extra_details_enabled = true if $DEBUG
446
+ puts report.render do
447
+ report_table = Inversion::Template.load( 'table.tmpl' )
448
+ report_table.rows = an_expensive_database_query()
449
+ report_table
450
+ end
451
+ <?end?>
452
+
453
+ This will insert the @report_table@ template in place of the yield, but only if $DEBUG is true.
454
+
455
+
456
+ h3(#troubleshooting). Troubleshooting/Introspection
457
+
458
+ h4(#pp). PP
459
+
460
+ The @pp@ tag uses the @PP@ library to output an escaped representation of its argument.
461
+
462
+ <?example { syntax: ruby, caption: "Creating an object to inspect" } ?>
463
+ content = Inversion::Template.load( 'content.tmpl' )
464
+ content.file = File.stat( '/tmp/example.txt' )
465
+
466
+ puts content.render
467
+ <?end?>
468
+
469
+ <?example { syntax: html, caption: "Inspecting an object from within a template" } ?>
470
+ <div class="debugging">
471
+ The file's stat attributes:
472
+ <?pp file ?>
473
+ </div>
474
+ <?end?>
475
+
476
+ The output is escaped according to the current setting of the <?link ":escape_format":Templates#options ?> option.
477
+
478
+ <?example { syntax: html, caption: "The rendered result" } ?>
479
+ <div class="debugging">
480
+ The file's stat attributes:
481
+ #&lt;File::Stat
482
+ dev=0xe000004,
483
+ ino=3064556,
484
+ mode=0100644 (file rw-r--r--),
485
+ nlink=1,
486
+ uid=501 (mahlon),
487
+ gid=0 (wheel),
488
+ rdev=0x0 (0, 0),
489
+ size=0,
490
+ blksize=4096,
491
+ blocks=0,
492
+ atime=2011-08-12 08:43:15 -0700 (1313163795),
493
+ mtime=2011-08-12 08:43:15 -0700 (1313163795),
494
+ ctime=2011-08-12 08:43:15 -0700 (1313163795)&gt;</div>
495
+ </div>
496
+ <?end?>
497
+
498
+
499
+ h4(#prettyprint). PrettyPrint
500
+
501
+ #TODO