[vhffs-dev] [1675] Beginning of the Template::Toolkit journey.

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 1675
Author:   beuss
Date:     2011-04-30 23:26:02 +0200 (Sat, 30 Apr 2011)
Log Message:
-----------
Beginning of the Template::Toolkit journey.

Beuss said "By the end of the year (2012), all panel will
use Template::Toolkit" and by the end of the year (2012) all panel was
using Template::Toolkit?\226?\128?\166

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Panel/Main.pm

Modified: trunk/vhffs-api/src/Vhffs/Panel/Main.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Panel/Main.pm	2011-04-30 21:21:43 UTC (rev 1674)
+++ trunk/vhffs-api/src/Vhffs/Panel/Main.pm	2011-04-30 21:26:02 UTC (rev 1675)
@@ -15,6 +15,7 @@
 use CGI::Session;
 use File::Spec;
 use Encode;
+use Template;
 
 use Vhffs::Main;
 use Vhffs::User;
@@ -462,12 +463,14 @@
 sub add_error
 {
     my ($panel, $error) = @_;
+    # TODO Do not use anonymous hash when Template::Toolkit transition is over.
     push(@{$panel->{errors}}, {msg => $error});
 }
 
 sub add_info
 {
     my ($panel, $info) = @_;
+    # TODO Do not use anonymous hash when Template::Toolkit transition is over.
     push(@{$panel->{infos}}, {msg => $info});
 }
 
@@ -704,6 +707,14 @@
 }
 
 
+=head2 $panel->display
+
+B<DEPRECATED> Render HTML::Template based template.
+
+Please use $panel->render() which uses new Template::Toolkit rendering.
+
+=cut
+
 sub display
 {
 	my $panel = shift;
@@ -737,7 +748,79 @@
 	return;
 }
 
+=head2 $panel->render($file, $vars, $layout)
 
+Renders given template with substitution variables C<$vars>.
+
+If no C<$layout> is provided, C<layouts/panel.tt> will be used
+otherwise C<$layout> should be the name of the layout relative
+to the C<layouts> folder.
+
+If request was made through Ajax, no layout will be processed.
+
+=cut
+
+
+sub render {
+	my ($self, $file, $vars, $layout) = @_;
+	my $conf = $self->{vhffs}->get_config;
+	
+	# TODO Should be in parent class when Template::Toolkit switch is over
+	my $create_vars = {
+		INCLUDE_PATH => $self->{templatedir}.'/panel/',
+		FILTERS => {
+			i18n => \&gettext,
+			mail => sub {
+				return Vhffs::Functions::obfuscate_email($self->{vhffs}, $_[0]);
+			},
+			# Format filter accept only one argument
+			# pretty_print can 'sprintf' anything, use it as
+			# [% '%s is $%d' | pretty_print(article, price)]
+			pretty_print => [sub {
+				my $context = shift;
+				my @args = @_;
+				return sub {
+					my $format = shift;
+					return sprintf($format, @args);
+				}
+			}, 1]
+		}
+	};
+	
+	
+	$vars = {} unless(defined $vars);
+	
+	$vars->{theme} = 'vhffs'; # TODO $conf->get_panel->{default_theme} unless(defined $vars->{theme});
+	$vars->{panel_url} = $conf->get_panel->{url};
+	$vars->{help_url} = $conf->get_panel->{url_help};
+    $vars->{title} = sprintf( gettext( '%s\'s Panel' ), $conf->get_host_name );
+    my @langs = $self->{vhffs}->get_config->get_available_languages;
+    $vars->{languages} = \@langs;
+    $vars->{errors} = $self->{errors};
+    $vars->{infos} = $self->{infos};
+
+	# Handling ajax stuff
+	if($self->{is_ajax_request}) {
+		delete $create_vars->{PROCESS};
+	} else {
+        if(defined $layout) {
+            $create_vars->{PROCESS} = "layouts/$layout";
+        } else {
+    		$create_vars->{PROCESS} = 'layouts/panel.tt';
+        }
+	}
+	
+	my $template = new Template($create_vars);
+
+	binmode STDOUT , ':utf8';	
+	
+	print CGI->header( -cookie=>[ @{$self->{cookies}} ], -type=>'text/html', -charset=>'utf-8' );
+
+	$template->process($file, $vars) 
+		|| die('Error while processing template: '.$template->error());
+	
+}
+
 =pod
 
 =head2 redirect


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/