root/branches/release-31/lib/MT/DefaultTemplates.pm @ 1463

Revision 1463, 9.6 kB (checked in by fumiakiy, 21 months ago)

Merged the latest release-30 changes to release-31. svn merge -r1446:1462 http://code.sixapart.com/svn/movabletype/branches/release-30 .

  • Property svn:keywords set to Id Revision
Line 
1# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
2# This program is distributed under the terms of the
3# GNU General Public License, version 2.
4#
5# $Id$
6
7package MT::DefaultTemplates;
8
9use strict;
10
11=start
12Registry storage format for default templates:
13
14default_templates:
15    index:
16        # The identifier used here never changes for this
17        # template. It is also unique.
18        main_index:
19            filename: (optional; defaults to <identifier>.mtml)
20            label: Main Index (auto-translated)
21            outfile: (applicable for index templates only)
22            rebuild_me: (applicable for index templates only)
23
24=cut
25
26my $loaded = 0;
27my $templates;
28BEGIN {
29    $templates = {
30        'index' => {
31            'main_index' => {
32                label => 'Main Index',
33                outfile => 'index.html',
34                rebuild_me => 1,
35            },
36            'archive_index' => {
37                label => 'Archive Index',
38                outfile => 'archives.html',
39                rebuild_me => 1,
40            },
41            'styles' => {
42                label => 'Stylesheet',
43                outfile => 'styles.css',
44                rebuild_me => 1,
45            },
46            'javascript' => {
47                label => 'JavaScript',
48                outfile => 'mt.js',
49            },
50            'rsd' => {
51                label => 'RSD',
52                outfile => 'rsd.xml',
53                rebuild_me => 1,
54            },
55            'feed_recent' => {
56                label => 'Feed - Recent Entries',
57                outfile => 'atom.xml',
58                rebuild_me => 1,
59            },
60        },
61        'individual' => {
62            'entry' => {
63                label => 'Entry',
64                mappings => {
65                    entry_archive => {
66                        archive_type => 'Individual',
67                    },
68                },
69            },
70        },
71        'page' => {
72            'page' => {
73                label => 'Page',
74                mappings => {
75                    page_archive => {
76                        archive_type => 'Page',
77                    },
78                },
79            },
80        },
81        'archive' => {
82            'entry_listing' => {
83                label => 'Entry Listing',
84                mappings => {
85                    monthly => {
86                        archive_type => 'Monthly',
87                    },
88                    category_monthly => {
89                        archive_type => 'Category-Monthly',
90                    },
91                    category => {
92                        archive_type => 'Category',
93                    },
94                },
95            },
96        },
97        'system' => {
98            'comment_response' => {
99                label => 'Comment Response',
100                description_label => 'Displays error, pending or confirmation message for comments.'
101            },
102            'comment_preview' => {
103                label => 'Comment Preview',
104                description_label => 'Displays preview of comment.',
105            },
106            'dynamic_error' => {
107                label => 'Dynamic Error',
108                description_label => 'Displays errors for dynamically published templates.',
109            },
110            'popup_image' => {
111                label => 'Popup Image',
112                description_label => 'Displays image when user clicks a popup-linked image.',
113            },
114            'search_results' => {
115                label => 'Search Results',
116                description_label => 'Displays results of a search.',
117            },
118        },
119        'module' => {
120            'categories' => {
121                label => 'Categories',
122            },
123            'comment_detail' => {
124                label => 'Comment Detail',
125            },
126            'comment_form' => {
127                label => 'Comment Form',
128            },
129            'comments' => {
130                label => 'Comments',
131            },
132            'entry_detail' => {
133                label => 'Entry Detail',
134            },
135            'entry_summary' => {
136                label => 'Entry Summary',
137            },
138            'entry_metadata' => {
139                label => 'Entry Metadata',
140            },
141            'tags' => {
142                label => 'Tags',
143            },
144            'footer' => {
145                label => 'Footer',
146            },
147            'header' => {
148                label => 'Header',
149            },
150            'sidebar_2col' => {
151                label => 'Sidebar - 2 Column Layout',
152            },
153            'sidebar_3col' => {
154                label => 'Sidebar - 3 Column Layout',
155            },
156            'sidebar' => {
157                label => 'Sidebar',
158            },
159            'trackbacks' => {
160                label => 'TrackBacks',
161            },
162            'page_detail' => {
163                label => 'Page Detail',
164            },
165        },
166        'global:module' => {
167            'footer-email' => {
168                label => 'Mail Footer',
169            },
170        },
171        'global:email' => {
172            'comment_throttle' => {
173                label => 'Comment throttle',
174            },
175            'commenter_confirm' => {
176                label => 'Commenter Confirm',
177            },
178            'commenter_notify' => {
179                label => 'Commenter Notify',
180            },
181            'new-comment' => {
182                label => 'New Comment',
183            },
184            'new-ping' => {
185                label => 'New Ping',
186            },
187            'notify-entry' => {
188                label => 'Entry Notify',
189            },
190            'recover-password' => {
191                label => 'Password Recovery',
192            },
193            'verify-subscribe' => {
194                label => 'Subscribe Verify',
195            },
196        },
197    };
198}
199
200sub core_default_templates {
201    return $templates;
202}
203
204sub load {
205    my $class = shift;
206    my ($terms) = @_;
207    my $tmpls = $class->templates || [];
208    if ($terms) {
209        foreach my $key (keys %$terms) {
210            @$tmpls = grep { $_->{$key} eq $terms->{$key} } @$tmpls;
211        }
212    }
213    return wantarray ? @$tmpls : (@$tmpls ? $tmpls->[0] : undef);
214}
215
216sub templates {
217    my $pkg = shift;
218    my ($set) = @_;
219    require File::Spec;
220
221    # A set of default templates as returned by MT::Component->registry
222    # yields an array of hashes.
223
224    my @tmpl_path = $set ? ("template_sets", $set) : ("default_templates");
225    my $all_tmpls = MT::Component->registry(@tmpl_path) || [];
226    my $weblog_templates_path = MT->config('WeblogTemplatesPath');
227
228    my (%tmpls, %global_tmpls);
229    foreach my $def_tmpl (@$all_tmpls) {
230        # copy structure, then run filter
231
232        my $tmpl_hash;
233        if ($def_tmpl->{templates} && ($def_tmpl->{templates} eq '*')) {
234            $tmpl_hash = MT->registry("default_templates");
235        }
236        else {
237            $tmpl_hash = $set ? $def_tmpl->{templates} : $def_tmpl;
238        }
239
240        foreach my $tmpl_set (keys %$tmpl_hash) {
241            next unless ref($tmpl_hash->{$tmpl_set}) eq 'HASH';
242            foreach my $tmpl_id (keys %{ $tmpl_hash->{$tmpl_set} }) {
243                next if $tmpl_id eq 'plugin';
244
245                my $p = $tmpl_hash->{plugin} || $tmpl_hash->{$tmpl_set}{plugin};
246                my $base_path = $def_tmpl->{base_path} || $tmpl_hash->{$tmpl_set}{base_path};
247                if ($p && $base_path) {
248                    $base_path = File::Spec->catdir($p->path, $base_path);
249                }
250                else {
251                    $base_path = $weblog_templates_path;
252                }
253
254                my $tmpl = { %{ $tmpl_hash->{$tmpl_set}{$tmpl_id} } };
255                my $type = $tmpl_set;
256                if ($tmpl_set =~ m/^global:/) {
257                    $type =~ s/^global://;
258                    $tmpl->{global} = 1;
259                }
260                $tmpl->{set} = $type; # system, index, archive, etc.
261
262                $type = 'custom' if $type eq 'module';
263                $type = $tmpl_id if $type eq 'system';
264                my $name = $tmpl->{label};
265                $name = $name->() if ref($name) eq 'CODE';
266                $tmpl->{name} = $name;
267                $tmpl->{type} = $type;
268                $tmpl->{key} = $tmpl_id;
269                $tmpl->{identifier} = $tmpl_id;
270
271                # load template if it hasn't been loaded already
272                if (!exists $tmpl->{text}) {
273                    local (*FIN, $/);
274                    my $filename = $tmpl->{filename} || ($tmpl_id . '.mtml');
275                    my $file = File::Spec->catfile($base_path, $filename);
276                    if ((-e $file) && (-r $file)) {
277                        open FIN, "<$file"; my $data = <FIN>; close FIN;
278                        $tmpl->{text} = $data;
279                    } else {
280                        $tmpl->{text} = '';
281                    }
282                }
283
284                my $local_global_tmpls = $tmpl->{global} ? \%global_tmpls : \%tmpls;
285                if (exists $local_global_tmpls->{$tmpl_id}) {
286                    # allow components/plugins to override core
287                    # templates
288                    $local_global_tmpls->{$tmpl_id} = $tmpl if $p && ($p->id ne 'core');
289                }
290                else {
291                    $local_global_tmpls->{$tmpl_id} = $tmpl;
292                }
293            }
294        }
295    }
296    my @tmpls = (values(%tmpls), values(%global_tmpls));
297    MT->run_callbacks('DefaultTemplateFilter' . ($set ? '.' . $set : ''), \@tmpls);
298    return \@tmpls;
299}
300
3011;
302__END__
303
304=head1 NAME
305
306MT::DefaultTemplates
307
308=head1 METHODS
309
310=head2 templates()
311
312Return the list of the templates in the WeblogTemplatesPath.
313
314=head1 AUTHOR & COPYRIGHT
315
316Please see L<MT/AUTHOR & COPYRIGHT>.
317
318=cut
Note: See TracBrowser for help on using the browser.