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