]> git.decadent.org.uk Git - maypole.git/blob - templates/factory/macros
Templates should be valid XHTML now...
[maypole.git] / templates / factory / macros
1 [%#
2
3 =head1 MACROS
4
5 These are some default macros which are used by various templates in the
6 system.
7
8 =head2 link
9
10 This creates an <A HREF="... to a command in the Apache::MVC system by
11 catenating the base URL, table, command, and any arguments.
12
13 #%]
14 [%
15 MACRO link(table, command, additional, label) BLOCK;
16     '<a href="' _ base _ "/" _ table _ "/" _ command _ "/" _ additional _ '">';
17     label;
18     "</a>";
19 END;
20 %]
21
22 [%#
23
24 =head2 maybe_link_view
25
26 C<maybe_link_view> takes something returned from the database - either
27 some ordinary data, or an object in a related class expanded by a
28 has-a relationship. If it is an object, it constructs a link to the view
29 command for that object. Otherwise, it just displays the data.
30
31 #%]
32
33 [%
34 MACRO maybe_link_view(object) BLOCK;
35     IF object.table; # It's an object, i.e. a has-a
36         link(object.table, "view", object.id, object);
37     ELSE;
38         object;
39     END;
40 END;
41 %]
42
43 [%#
44
45 =head2 display_line
46
47 C<display_line> is used in the list template to display a row from the
48 database, by iterating over the columns and displaying the data for each
49 column. It misses out the C<id> column by default, and magically
50 URLifies columns called C<url>. This may be considered too much magic
51 for some.
52
53 #%]
54 [% MACRO display_line(item) BLOCK;
55     FOR col = classmetadata.columns;
56         NEXT IF col == "id";
57         "<td>";
58         IF col == "url";
59             "<A HREF="; item.url; "> "; item.url; "</A>";
60         ELSIF col == item.stringify_column;
61             maybe_link_view(item);
62         ELSE;
63             maybe_link_view(item.$col);
64         END;
65         "</td>";
66     END;
67     button(item, "edit");
68     button(item, "delete");
69 END %]
70 [%#
71
72 =head2 button
73
74 This is a generic button, which performs an action on an object.
75
76 =cut
77
78 #%]
79 [% MACRO button(obj, action) BLOCK; %]
80 <td>
81 <form method=post action="[% base %]/[% obj.table %]/[% action %]/[% obj.id %]">
82     <input type=submit name=[% action %] value=[% action %] />
83 </form>
84 </td>
85 [% END %]
86 [%#
87
88 =head2 view_related
89
90 This takes an object, and looks up the C<related_accessors>; this should
91 give a list of accessors that can be called to get a list of related
92 objects. It then displays a title for that accessor, (i.e. "Beers" for a
93 brewery) calls the accesor, and displays a list of the results. 
94
95 =cut
96
97 #%]
98 [% 
99 MACRO view_related(object) BLOCK;
100     FOR accessor = classmetadata.related_accessors.list;
101         "<h3>"; accessor | ucfirst; "</h3>\n";
102         "<ul id=\"vlist\">";
103         FOR thing = object.$accessor;
104             "<li>"; maybe_link_view(thing); "</li>\n";
105         END;
106         "</ul>";
107     END; 
108 END;
109
110 MACRO test_xxxx(myblock) BLOCK;
111     FOR col = classmetadata.columns;
112         NEXT IF col == "id";
113         myblock;
114     END;
115 END;
116 %]