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