1package AmphetaDesk::AmphetaDesk::Templates;
2###############################################################################
3# AmphetaDesk::AmphetaDesk                                           (c) 2000-2002 Disobey #
4# morbus@disobey.com                      http://www.disobey.com/amphetadesk/ #
5###############################################################################
6# ABOUT THIS PACKAGE:                                                         #
7#   This package encapsulates all the various template handling code. It      #
8#   uses AmphetaDesk::Text::Template to parse the templates, and then sends the results    #
9#   back to the calling function for further action. Simple stuff.            #
10#                                                                             #
11# LIST OF ROUTINES BELOW:                                                     #
12#   include - used within template files to include other template files.     #
13#   parse_template - take the passed file path, parse it, and return results. #
14#   send_to_browser - take the output destined for the browser, and send it.  #
15#   to_browser - add the passed message to the output for the browser.        #
16###############################################################################
17#           "Put something witty here before committing to CVS."              #
18###############################################################################
19
20use strict; $|++;
21use AmphetaDesk::Text::Template;
22require Exporter;
23use vars qw( @ISA @EXPORT );
24@ISA = qw( Exporter );
25@EXPORT = qw( parse_template );
26
27# we always prepend all this stuff to the various AmphetaDesk::Text::Template
28# files so that we can use templates-within-templates without the
29# namespace being lost (ie, a template-within-template would get
30# AmphetaDesk::Text::Template::get_setting unless you used PACKAGE statements
31# in the actual parent template). this isn't ideal since you're
32# restricted to only one PACKAGE at a time. so, we predeclare
33# all the packages and pragmas we may want to use in our templates.
34AmphetaDesk::Text::Template->always_prepend("
35   use strict;                      use File::Spec::Functions;
36   use CGI qw/:standard :cgi-lib/;  use AmphetaDesk::AmphetaDesk::Channels;
37   use AmphetaDesk::AmphetaDesk::Settings;       use AmphetaDesk::AmphetaDesk::ChannelsList;
38   use AmphetaDesk::AmphetaDesk::MyChannels;     use AmphetaDesk::AmphetaDesk::Utilities;
39   use AmphetaDesk::AmphetaDesk::Versioning;     use AmphetaDesk::AmphetaDesk::WWW;
40");
41
42# where we store our template output. we do this
43# in an array because adding a zillion items to an
44# array and then joining on whitespace is far less
45# memory hungry than concating string after string.
46my @BROWSER_OUTPUT;
47
48###############################################################################
49# include - used within template files to include other template files.       #
50###############################################################################
51# USAGE:                                                                      #
52#   [$ include ( $file_path ) $]         # within the user templates.         #
53#                                                                             #
54# NOTES:                                                                      #
55#   Only used within the user templates, this routine parses another          #
56#   template and puts the results in the current template. It's meant         #
57#   reproduce (to a small degree) server-side includes. This code comes       #
58#   almost verbatim from the AmphetaDesk::Text::Template documentation.                    #
59#                                                                             #
60# RETURNS:                                                                    #
61#   undef; if the include failed for some reason.                             #
62#   results; the parsed results of the included template.                     #
63###############################################################################
64
65sub include { my $file = shift; &AmphetaDesk::Text::Template::fill_in_file($file, DELIMITERS => ['[$', '$]']); };
66
67###############################################################################
68# parse_template - take the passed file path, parse it, and return results.   #
69###############################################################################
70# USAGE:                                                                      #
71#   my $results = parse_template( $file_path );                               #
72#                                                                             #
73# NOTES:                                                                      #
74#   This routine accepts a full file path (which is assumed to exist),        #
75#   attempts to parse the template with AmphetaDesk::Text::Template, and then returns      #
76#   the results. If this fails, something really wrong happened.              #
77#                                                                             #
78# RETURNS:                                                                    #
79#   $results; the results of a successful parsing.                            #
80#   die; if the template could not be parsed for some odd reason.             #
81###############################################################################
82
83sub parse_template {
84
85   my ($file_path) = @_;
86
87   # construct the template object.
88   my $template = AmphetaDesk::Text::Template->new( SOURCE => $file_path, DELIMITERS => [ '[$ ', ' $]' ] )
89               or error("AmphetaDesk couldn't create the template: $AmphetaDesk::Text::Template::ERROR");
90
91   # and return the results.
92   return $template->fill_in();
93
94}
95
96###############################################################################
97# send_to_browser - take the output destined for the browser, and send it.    #
98# to_browser - add the passed message to the output for the browser.          #
99###############################################################################
100# USAGE:                                                                      #
101#   $OUT = send_to_browser;                                                   #
102#   to_browser("random crap for the ", $browser",  "whoooO");                 #
103#   to_browser("the above was an array. this is a string.");                  #
104#                                                                             #
105# NOTES:                                                                      #
106#   In previous versions of the AmphetaDesk::AmphetaDesk template code, we concat'd all    #
107#   of our strings together in the templates. This started causing memory     #
108#   leaks and slowdowns, due to the sheer amount of concat's we do for heavy  #
109#   users. Now we throw everything into an array, and only concat things      #
110#   together when we're done everything else. to_browser adds the message     #
111#   to our array, and send_to_browser dumps it all out in one big string.     #
112#                                                                             #
113#   You can, of course, choose not to use send_?to_browser when it's not      #
114#   needed, such as small template places like my_settings.html, etc. If      #
115#   that's the case, you can send straight to AmphetaDesk::Text::Template's $OUT.          #
116#                                                                             #
117# RETURNS:                                                                    #
118#   $out; the final string from send_to_browser for AmphetaDesk::Text::Template's $OUT.    #
119#   1; to_browser always returns 1 - it'd be really weird if it didn't.       #
120###############################################################################
121
122sub send_to_browser { my $out = join("", @BROWSER_OUTPUT); undef @BROWSER_OUTPUT; return $out; }
123sub to_browser { my (@msgs) = @_; push(@BROWSER_OUTPUT, join("", @msgs)); return 1; }
124
1251;
126