]> git.decadent.org.uk Git - maypole.git/blob - templates/factory/macros
Split the meat of the view template off to a view_item macro.
[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     SET lnk = base _ "/" _ table _ "/" _ command _ "/" _ additional;
17     lnk = lnk | uri | html;
18     '<a href="' _ lnk _ '">';
19     label;
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;
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";
59         "<td>";
60         IF col == "url" AND item.url;
61             '<a href="'; item.url; '"> '; item.url; '</a>';
62         ELSIF col == item.stringify_column;
63             maybe_link_view(item);
64         ELSE;
65             maybe_link_view(item.$col);
66         END;
67         "</td>";
68     END;
69     '<td class="actions">';
70     button(item, "edit");
71     button(item, "delete");
72     "</td>";
73 END %]
74 [%#
75
76 =head2 button
77
78 This is a generic button, which performs an action on an object.
79
80 =cut
81
82 #%]
83 [% MACRO button(obj, action) BLOCK; %]
84 [% IF obj.is_public(action) %]
85 <form class="actionform" action="[% base %]/[% obj.table %]/[% action %]/[% obj.id.join('/') %]" method="post">
86 <div class="field"><input class="actionbutton" type="submit" value="[% action %]" /></div></form>
87 [% END %]
88 [% END %]
89 [%#
90
91 =head2 view_related
92
93 This takes an object, and looks up the C<related_accessors>; this should
94 give a list of accessors that can be called to get a list of related
95 objects. It then displays a title for that accessor, (i.e. "Beers" for a
96 brewery) calls the accesor, and displays a list of the results. 
97
98 =cut
99
100 #%]
101 [% 
102 MACRO view_related(object) BLOCK;
103     FOR accessor = classmetadata.related_accessors.list;
104         "<div id=\"subtitle\">"; accessor | ucfirst; "</div>\n";
105         "<ul id=\"vlist\">";
106         FOR thing = object.$accessor;
107             "<li>"; maybe_link_view(thing); "</li>\n";
108         END;
109         "</ul>";
110     END; 
111 END;
112
113 MACRO test_xxxx(myblock) BLOCK;
114     FOR col = classmetadata.columns;
115         NEXT IF col == "id";
116         myblock;
117     END;
118 END;
119 %]
120 [%#
121
122 =head2 view_item
123
124 This takes an object and and displays its properties in a table. 
125
126 =cut
127
128 #%]
129 [% MACRO view_item(item) BLOCK; %]
130     [% SET string = item.stringify_column %]
131     <div id="title"> [% item.$string %]</div>
132     [% INCLUDE navbar %]
133     <table class="view">
134         <tr>
135             <td class="field">[% classmetadata.colnames.$string %]</td>
136             <td>[% item.$string %]</td>
137         </tr>
138         [% FOR col = classmetadata.columns.list;
139             NEXT IF col == "id" OR col == string;
140             NEXT UNLESS item.$col;
141         %]
142 [%# 
143
144 =for doc
145
146 It gets the displayable form of a column's name from the hash returned
147 from the C<column_names> method:
148
149 #%]
150             <tr>
151                 <td class="field">[% classmetadata.colnames.$col; %]</td>
152                 <td>
153                     [% IF col == "url" && item.url;  # Possibly too much magic.
154                         '<a href="'; item.url; '"> '; item.url; '</a>';
155                     ELSE;
156                         maybe_link_view(item.$col); 
157                     END; %]
158 [%#
159
160 This tests whether or not the returned value is an object, and if so,
161 creates a link to a page viewing that object; if not, it just displays
162 the text as normal. The object is linked using its stringified name;
163 by default this calls the C<name> method, or returns the object's ID
164 if there is no C<name> method or other stringification method defined.
165
166 =cut
167
168 #%] 
169                 </td>
170             </tr>
171         [% END; %]
172     </table>
173 [% END %]