]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/templates/factory/macros
cbf58bb21902fd0b1f80a907190363aa184ecf1b
[maypole.git] / lib / Maypole / 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     SET lnk = base _ "/" _ table _ "/" _ command _ "/" _ additional;
17     lnk = lnk | uri | html;
18     '<a href="' _ lnk _ '">';
19     label | html;
20     "</a>";
21 END;
22 %]
23
24 [%#
25
26 =head2 maybe_link_view
27
28 C<maybe_link_view> takes something returned from the database - either
29 some ordinary data, or an object in a related class expanded by a
30 has-a relationship. If it is an object, it constructs a link to the view
31 command for that object. Otherwise, it just displays the data.
32
33 #%]
34
35 [%
36 MACRO maybe_link_view(object) BLOCK;
37     IF object.isa('Maypole::Model::Base');
38         link(object.table, "view", object.id.join('/'), object);
39     ELSE;
40         object | html ;
41     END;
42 END;
43 %]
44
45 [%#
46
47 =head2 display_line
48
49 C<display_line> is used in the list template to display a row from the
50 database, by iterating over the columns and displaying the data for each
51 column. It misses out the C<id> column by default, and magically
52 URLifies columns called C<url>. This may be considered too much magic
53 for some.
54
55 #%]
56 [% MACRO display_line(item) BLOCK;
57     FOR col = classmetadata.list_columns;
58         NEXT IF col == "id" OR col == classmetadata.table _ "_id";
59         "<td>";
60         IF col == "url" AND item.url;
61             '<a href="'; item.url | html ; '"> '; item.url; '</a>';
62         ELSIF col == classmetadata.stringify_column;
63             maybe_link_view(item);
64         ELSE;
65             accessor = item.accessor_name(col);
66             maybe_link_view(item.$accessor);
67         END;
68         "</td>";
69     END;
70     '<td class="actions">';
71     button(item, "edit");
72     button(item, "delete");
73     "</td>";
74 END %]
75 [%#
76
77 =head2 button
78
79 This is a generic button, which performs an action on an object.
80
81 =cut
82
83 #%]
84 [% MACRO button(obj, action) BLOCK; %]
85 [% IF obj.is_public(action) %]
86 <form class="actionform" action="[% base %]/[% obj.table %]/[% action %]/[% obj.id.join('/') %]" method="post">
87 <div class="field"><input class="actionbutton" type="submit" value="[% action %]" /></div></form>
88 [% END %]
89 [% END %]
90 [%#
91
92 =head2 view_related
93
94 This takes an object, and looks up the C<related_accessors>; this should
95 give a list of accessors that can be called to get a list of related
96 objects. It then displays a title for that accessor, (i.e. "Beers" for a
97 brewery) calls the accesor, and displays a list of the results. 
98
99 =cut
100
101 #%]
102 [% 
103 MACRO view_related(object) BLOCK;
104     FOR accessor = classmetadata.related_accessors.list;
105         "<div id=\"subtitle\">"; accessor | ucfirst; "</div>\n";
106         "<ul id=\"vlist\">";
107         FOR thing = object.$accessor;
108             "<li>"; maybe_link_view(thing); "</li>\n";
109         END;
110         "</ul>";
111     END; 
112 END;
113
114 MACRO test_xxxx(myblock) BLOCK;
115     FOR col = classmetadata.columns;
116         NEXT IF col == "id";
117         myblock;
118     END;
119 END;
120 %]
121 [%#
122
123 =head2 view_item
124
125 This takes an object and and displays its properties in a table. 
126
127 =cut
128
129 #%]
130 [% MACRO view_item(item) BLOCK; %]
131     [% SET string = classmetadata.stringify_column %]
132     <div id="title"> [% item.$string | html %]</div>
133     [% INCLUDE navbar %]
134     <table class="view">
135         <tr>
136             <td class="field">[% classmetadata.colnames.$string %]</td>
137             <td>[% item.$string | html %]</td>
138         </tr>
139         [% FOR col = classmetadata.columns.list;
140             NEXT IF col == "id" OR col == string OR col == classmetadata.table _ "_id";;
141             NEXT UNLESS item.$col;
142         %]
143 [%# 
144
145 =for doc
146
147 It gets the displayable form of a column's name from the hash returned
148 from the C<column_names> method:
149
150 #%]
151             <tr>
152                 <td class="field">[% classmetadata.colnames.$col; %]</td>
153                 <td>
154                     [% IF col == "url" && item.url;  # Possibly too much magic.
155                         '<a href="'; item.url | html ; '"> '; item.url; '</a>';
156                     ELSE;
157                         maybe_link_view(item.$col); 
158                     END; %]
159 [%#
160
161 This tests whether or not the returned value is an object, and if so,
162 creates a link to a page viewing that object; if not, it just displays
163 the text as normal. The object is linked using its stringified name;
164 by default this calls the C<name> method, or returns the object's ID
165 if there is no C<name> method or other stringification method defined.
166
167 =cut
168
169 #%] 
170                 </td>
171             </tr>
172         [% END; %]
173     </table>
174 [% END %]