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