Code

More tweaks to the design.
[roundup.git] / doc / templating.txt
1 ==========================
2 HTML Templating Mechanisms
3 ==========================
5 :Version: $Revision: 1.11 $
7 Current Situation and Issues
8 ============================
10 Syntax
11 ------
13 Roundup currently uses an element-based HTML-tag-alike templating syntax::
15    <display call="checklist('status')">
17 The templates were initially parsed using recursive regular expression
18 parsing, and since no template tag could encapsulate itself, the parser
19 worked just fine. Then we got the ``<require>`` tag, which could have other
20 ``<require>`` tags inside. This forced us to move towards a more complete
21 parser, using the standard python sgmllib/htmllib parser. The downside of this
22 switch is that constructs of the form::
24    <tr class="row-<display call="plain('status')">">
26 don't parse as we'd hope. We can modify the parser to work, but that doesn't
27 another couple of issues that have arisen:
29 1. the template syntax is not well-formed, and therefore is a pain to parse
30    and doesn't play well with other tools, and
31 2. user requirements generally have to be anticipated and accounted for in
32    templating functions (like ``plain()`` and ``checklist()`` above), and
33    we are therefore artificially restrictive.
35 Arguments for switching templating systems:
37 *Pros*
39   - more flexibility in templating control and content
40   - we can be well-formed
42 *Cons*
44   - installed user base (though they'd have to edit their templates with the
45     next release anyway)
46   - current templating system is pretty trivial, and a more flexible system
47     is likely to be more complex
50 Templates
51 ---------
53 We should also take this opportunity to open up the flexibility of the
54 templates through:
56 1. allowing the instance to define a "page" template, which holds the overall
57    page structure, including header and footer
61 Possible approaches
62 ===================
64 Zope's PageTemplates
65 --------------------
67 Using Zope's PageTemplates seems to be the best approach of the lot.
68 In my opinion, it's the peak of HTML templating technology at present. With
69 appropriate infrastructure, the above two examples would read:
71   <span tal:replace="item/status/checklist">status checklist</span>
73   <tr tal:attributes="class string:row-${item/status/name}">
75 ... which doesn't look that much more complicated... honest...
77 Other fun can be had when you start playing with stuff like:
79   <table>
80    <tr tal:repeat="message item/msg/list">
81     <td tal:define="from message/from">
82      <a href="" tal:attributes="href string:mailto:${from/address}"
83                 tal:content="from/name">mailto link</a>
84     </td>
85     <td tal:content="message/title">subject</td>
86     <td tal:content="message/created">received date</td>
87    </tr>
88   </table>
90 Note: even if we don't switch templating as a whole, this document may be
91 applied to the ZRoundup frontend.
93 PageTemplates in a Nutshell
94 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
96 PageTemplates consist of three technologies:
98 TAL - Template Attribute Language
99   This is the syntax which is woven into the HTML using the ``tal:`` tag
100   attributes. A TAL parser pulls out the TAL commands from the attributes
101   runs them using some expression engine.
103 TALES - TAL Expression Syntax
104   The expression engine used in this case is TALES, which runs the expressions
105   that form the tag attribute values. TALES expressions come in three
106   flavours:
108   Path Expressions - eg. ``item/status/checklist``
109    These are object attribute / item accesses. Roughly speaking, the path
110    ``item/status/checklist`` is broken into parts ``item``, ``status``
111    and ``checklist``. The ``item`` part is the root of the expression.
112    We then look for a ``status`` attribute on ``item``, or failing that, a
113    ``status`` item (as in ``item['status']``). If that
114    fails, the path expression fails. When we get to the end, the object we're
115    left with is evaluated to get a string - methods are called, objects are
116    stringified. Path expressions may have an optional ``path:`` prefix, though
117    they are the default expression type, so it's not necessary.
119   String Expressions - eg. ``string:hello ${user/name}``
120    These expressions are simple string interpolations (though they can be just
121    plain strings with no interpolation if you want. The expression in the
122    ``${ ... }`` is just a path expression as above.
124   Python Expressions - eg. ``python: 1+1``
125    These expressions give the full power of Python. All the "root level"
126    variables are available, so ``python:item.status.checklist()`` would be
127    equivalent to ``item/status/checklist``, assuming that ``checklist`` is
128    a method.
130 PageTemplates
131   The PageTemplates module glues together TAL and TALES.
134 Implementation
135 ~~~~~~~~~~~~~~
137 I'm envisaging an infrastructure layer where each template has the following
138 "root level" (that is, directly accessible in the TALES namespace) variables
139 defined:
141 *user*
142   The current user node as an HTMLItem instance
144 *class*
145   The current class of node being displayed as an HTMLClass instance
147 *item*
148   The current node from the database, if we're viewing a specific node, as an
149   HTMLItem instance. If it doesn't exist, then we're on a new item page.
151 (*classname*)
152   this is one of two things:
154   1. the *item* is also available under its classname, so a *user* node
155      would also be available under the name *user*. This is also an HTMLItem
156      instance.
157   2. if there's no *item* then the current class is available through this
158      name, thus "user/name" and "user/name/menu" will still work - the latter
159      will pull information from the form if it can.
161 *form*
162   The current CGI form information as a mapping of form argument name to value
164 *request*
165   Includes information about the current request, including:
166    - the url
167    - the current index information (``filterspec``, ``filter`` args,
168      ``properties``, etc) parsed out of the form. 
169    - methods for easy filterspec link generation
171 *instance*
172   The current instance
174 *db*
175   The current open database
177 *config*
178   The current instance config
180 *modules*
181   python modules made available (XXX: not sure what's actually in there tho)
183 Accesses through the *user*::
185     class HTMLUser:
186         def hasPermission(self, ...)
188 (note that the other permission check implemented by the security module may
189  be implemented easily in a tal:condition, so isn't needed here)
191 Accesses through a class (either through *class* or *db.<classname>*):
193     class HTMLClass:
194         def __getattr__(self, attr):
195             ''' return an HTMLItem instance '''
196         def classhelp(self, ...)
197         def list(self, ...)
198         def filter(self):
199             ''' Return a list of items from this class, filtered and sorted
200                 by the current requested filterspec/filter/sort/group args
201             '''
203 Accesses through an *item*::
205     class HTMLItem:
206         def __getattr__(self, attr):
207             ''' return an HTMLItem instance '''
208         def history(self, ...)
209         def remove(self, ...)
211 Note: the above could cause problems if someone wants to have properties
212 called "history" or "remove"...
214 String, Number, Date, Interval HTMLProperty
215  a wrapper object which may be stringified for the current plain() behaviour
216  and has methods emulating all the current display functions, so
217  ``item/name/plain`` would emulate the current ``call="plain()``". Also, 
218  ``python:item.name.plain(name=value)`` would work just fine::
220     class HTMLProperty:
221         def __init__(self, instance, db, ...)
222         def __str__(self):
223             return self.plain()
225     class StringHTMLProperty(HTLProperty):
226         def plain(self, ...)
227         def field(self, ...)
228         def stext(self, ...)
229         def multiline(self, ...)
230         def email(self, ...)
232     class NumberHTMLProperty(HTMLProperty):
233         def plain(self, ...)
234         def field(self, ...)
236     class BooleanHTMLProperty(HTMLProperty):
237         def plain(self, ...)
238         def field(self, ...)
240     class DateHTMLProperty(HTMLProperty):
241         def plain(self, ...)
242         def field(self, ...)
243         def reldate(self, ...)
245     class IntervalHTMLProperty(HTMLProperty):
246         def plain(self, ...)
247         def field(self, ...)
248         def pretty(self, ...)
250 Link HTMLProperty
251  the wrapper object would include the above as well as being able to access
252  the class information. Stringifying the object itself would result in the
253  value from the item being displayed. Accessing attributes of this object
254  would result in the appropriate entry from the class being queried for the
255  property accessed (so item/assignedto/name would look up the user entry
256  identified by the assignedto property on item, and then the name property of
257  that user)::
258     
259     class LinkHTMLProperty(HTMLProperty):
260         ''' Be a HTMLItem too '''
261         def __getattr__(self, attr):
262             ''' return a new HTMLProperty '''
263         def download(self, ...)
264         def checklist(self, ...)
266 Multilink HTMLProperty
267  the wrapper would also be iterable, returning a wrapper object like the Link
268  case for each entry in the multilink::
270     class MultilinkHTMLProperty(HTMLProperty):
271         def __len__(self):
272             ''' length of the multilink '''
273         def __getitem(self, num):
274             ''' return a new HTMLItem '''
275         def checklist(self, ...)
276         def list(self, ...)
277  
278 *request*
279  the request object will handle::
281     class Request:
282         def __init__(self, ...)
283         def filterspec(self, ...)
285 Template files
286 ~~~~~~~~~~~~~~
288 Each instance will have the opportunity to supply the following templates:
290 page
291  This is the overall page look template, and includes at some point a TAL
292  command that includes the variable "content". This variable causes the actual
293  page content to be generated.
295 [classname].[template type]
296  Templates that have this form are applied to item data. There are three forms
297  of special template types:
299  [classname].index
300   This template is used when the URL specifies only the class, and not a node
301   designator.  It displays a list of [classname] items from the database, and
302   a "filter refinement" form.
303   Would perform a TAL ``repeat`` command using the list supplied by
304   ``class/filter``. This deviates from the current situation in that currently
305   the index template specifies a single row, and the filter part is
306   automatically generated.
308  [classname].item
309   This template is used when the URL specifies an item designator. It's the
310   default template used (when no template is explicitly given). It displays
311   a single item from the database using the *classname* variable (that
312   is, the variable of the same name as the class being displayed. If 
314  These two special template types may be overridden by the :template CGI
315  variable.
317 Note that the "newitem" template doesn't exist any more because the item
318 templates may determine whether the page has an existing item to render. The
319 new item page would be accessed by "/tracker/url/issue?:template=item".
320 The old "filter" template has been subsumed by the index template.
323 Integrating Code
324 ~~~~~~~~~~~~~~~~
326 We will install PageTemplates, TAL and ZTUtils in site-packages. If there is a
327 local Zope installation, it will use its own PageTemplates code (Zope modifies
328 the module search path to give precedence to its own module library).
330 We will then install the trivial MultiMapping and ComputedAttribute modules in
331 the Roundup package, and have some import trickery that determines whether
332 they are required, and if so they will be imported as if they were at the
333 "top level" of the module namespace.
335 New CGI client structure
336 ~~~~~~~~~~~~~~~~~~~~~~~~
338 Handling of a request in the CGI client will take three phases:
340 1. Determine user, pre-set "content" to authorisation page if necessary
341 2. Render main page, with callback to "content"
342 3. Render content - if not pre-set, then determine which content to render
345 Use Cases
346 ~~~~~~~~~
348 Meta/parent bug
349   Can be done with addition to the schema and then the actual parent heirarchy
350   may be displayed with a new template page ":dependencies" or something.
352 Submission wizard
353   Can be done using new templates ":page1", ":page2", etc and some additional
354   actions on the CGI Client class in the instance.