This commit is contained in:
briandilley 2004-11-02 01:38:42 +00:00
parent 89e1d416d0
commit 73a87c5461
4 changed files with 49 additions and 21 deletions

View File

@ -1,3 +1,7 @@
11-01-04
* added support for templates
* added xml template
10-27-04
* added configuration for site name.
* added functionality for random tag lines.

View File

@ -310,6 +310,21 @@ sub html_escape {
return $ret_data;
}
##
# Escape xml
sub xml_escape {
my ($data) = @_;
my $ret_data = "";
foreach my $line (split(/\n/,$data)) {
$line =~ s/</&lt;/g;
$line =~ s/>/&gt;/g;
$line =~ s/"/&quot;/g;
$line =~ s/^\s+/"&nbsp;"x$+[0]/e;
$ret_data .= $line;
}
return $ret_data;
}
##
# Sends the HTML header
sub send_html_header {
@ -319,8 +334,8 @@ sub send_html_header {
##
# Renders an HTML template
sub wrap_template {
my ($template_file, %vars) = @_;
open(IN,"$template_file");
my ($template, $template_file, %vars) = @_;
open(IN,"templates/$template/$template_file");
my $data = join("",<IN>);
close(IN);
foreach $key (keys %vars) {
@ -332,8 +347,8 @@ sub wrap_template {
##
# Wraps and renders a template
sub render_template {
my ($template_file, %vars) = @_;
my $data = wrap_template($template_file, %vars);
my ($template, $template_file, %vars) = @_;
my $data = wrap_template($template, $template_file, %vars);
print STDOUT $data;
}

View File

@ -13,6 +13,9 @@ $SITE_NAME = "Kwotes";
# tag lines to be randomly displayed
# in the header
$DEFAULT_TEMPLATE = "default";
# name of the default template
############################
## DATABASE CONFIGURATION ##
############################

View File

@ -30,6 +30,7 @@ my %FORM = parse_form();
my $action = $FORM{"action"};
my $main_content;
my %vars;
my $template = ($FORM{"template"}) ? $FORM{"template"} : $DEFAULT_TEMPLATE;
# populate %vars with ENV
foreach my $key (keys %ENV) {
@ -41,6 +42,7 @@ $vars{KWOTE_COUNT} = get_kwote_count();
$vars{KWOTE_BACKUP_COUNT} = get_kwote_backup_count();
$vars{SITE_NAME} = $SITE_NAME;
$vars{TAG_LINE} = get_tagline();
$vars{TEMPLATE_DIR} = "templates/$template";
# send header
send_html_header();
@ -49,7 +51,7 @@ send_html_header();
# action: add (show add form)
if ($action eq "add") {
$vars{TITLE} = "Add Kwote";
$main_content = wrap_template("html/content-addform.html", %vars);
$main_content = wrap_template($template, "content-addform.html", %vars);
############
# action: doadd (add the kwote to the db)
@ -58,7 +60,7 @@ if ($action eq "add") {
if ($FORM{"content"} eq "") {
$vars{TITLE} = "An Error Occured";
$vars{ERROR_MESSAGE} = "No text entered";
$main_content = wrap_template("html/content-error.html",%vars);
$main_content = wrap_template($template, "content-error.html",%vars);
} else {
@ -70,13 +72,13 @@ if ($action eq "add") {
if (!defined($kid)) {
$vars{TITLE} = "An Error Occured";
$vars{ERROR_MESSAGE} = "Couldn't add kwote";
$main_content = wrap_template("html/content-error.html",%vars);
$main_content = wrap_template($template, "content-error.html",%vars);
# all was good
} else {
$vars{TITLE} = "Kwote Added";
$vars{KWOTE_ID} = $kid;
$main_content = wrap_template("html/content-addform-thanks.html", %vars);
$main_content = wrap_template($template, "content-addform-thanks.html", %vars);
}
}
@ -93,7 +95,7 @@ if ($action eq "add") {
if (!defined($kwote)) {
$vars{TITLE} = "Kwote Does Not Exist";
$vars{ERROR_MESSAGE} = "That kwote does not exist";
$main_content = wrap_template("html/content-error.html",%vars);
$main_content = wrap_template($template, "content-error.html",%vars);
# all was good
} else {
@ -101,7 +103,7 @@ if ($action eq "add") {
$vars{KWOTE_ID} = $kwote->{'id'};
$vars{KWOTE_TEXT} = html_escape($kwote->{'content'});
$vars{KWOTE_RATING} = $kwote->{'rating'};
$main_content = wrap_template("html/content-show-kwote.html", %vars);
$main_content = wrap_template($template, "content-show-kwote.html", %vars);
}
@ -159,6 +161,8 @@ if ($action eq "add") {
$vars{LAST_INDEX} = $start_index-$max_returned;
$vars{MAX_RECORDS} = $max_records;
$vars{SEARCH_STRING} = $search_string;
$vars{MAX_RATING} = $max_rating;
$vars{MIN_RATING} = $min_rating;
# get the navigation template
my $navigation_template = undef;
@ -166,41 +170,43 @@ if ($action eq "add") {
# forward, no back
if ($FORM{"o"} ne "random" && $start_index<=0 && @rows>=$max_returned
&& ($start_index+$max_returned)<$max_records) {
$navigation_template = "html/content-list-navigate-no-back.html";
$navigation_template = "content-list-navigate-no-back.html";
# forward and back
} elsif ($FORM{"o"} ne "random" && $start_index>0 && @rows>=$max_returned
&& ($start_index+$max_returned)<$max_records ) {
$navigation_template = "html/content-list-navigate.html";
$navigation_template = "content-list-navigate.html";
# back only
} elsif ($FORM{"o"} ne "random" && $start_index>0 && @rows<$max_returned) {
$navigation_template = "html/content-list-navigate-no-forward.html";
$navigation_template = "content-list-navigate-no-forward.html";
}
# wrap the navigation template
$main_content .= wrap_template($navigation_template, %vars);
$main_content .= wrap_template($template, $navigation_template, %vars);
# loop through the results
if (defined(@rows)) {
for (my $i=0; $i<@rows && ($i+$start_index)<$max_records; $i++) {
my $row = $rows[$i];
$vars{KWOTE_ID} = $row->{'id'};
$vars{KWOTE_TEXT} = html_escape($row->{'content'});
$vars{KWOTE_TEXT_HTML} = html_escape($row->{'content'});
$vars{KWOTE_TEXT_XML} = xml_escape($row->{'content'});
$vars{KWOTE_TEXT_PLAIN} = $row->{'content'};
$vars{KWOTE_RATING} = $row->{'rating'};
$main_content .= wrap_template("html/content-show-kwote.html", %vars);
$main_content .= wrap_template($template, "content-show-kwote.html", %vars);
}
}
# wrap the navigation template
$main_content .= wrap_template($navigation_template, %vars);
$main_content .= wrap_template($template, $navigation_template, %vars);
##########
# action: search (show the search page)
} elsif ($action eq "search") {
$vars{TITLE} = "Search";
$main_content = wrap_template("html/content-search.html", %vars);
$main_content = wrap_template($template, "content-search.html", %vars);
##########
# action: love
@ -220,11 +226,11 @@ if ($action eq "add") {
# show the homepage
} else {
$vars{TITLE} = "The Better kwote Database";
$main_content = wrap_template("html/content-default.html", %vars);
$main_content = wrap_template($template, "content-default.html", %vars);
}
# finish the HTML
render_template("html/header.html", %vars);
render_template($template, "header.html", %vars);
print STDOUT $main_content;
render_template("html/footer.html", %vars);
render_template($template, "footer.html", %vars);