[wellwell-devel] [wellwell/zoom] Add support for template engines.
Stefan Hornburg
wellwell-devel at rt.icdevgroup.org
Tue Oct 19 14:36:49 UTC 2010
commit d45abac29b5ee6dd80872d523ae8c805c3f197d3
Author: Stefan Hornburg (Racke) <racke at linuxia.de>
Date: Tue Oct 19 16:35:02 2010 +0200
Add support for template engines.
ITL is the default and Zoom (based on Template::Zoom) is the new kid on the block.
lib/WellWell/Compose.pm | 41 ++++++++++++++++++---
lib/WellWell/Compose/Component/ITL.pm | 41 +++++++++++++++++++++
lib/WellWell/Compose/Component/Zoom.pm | 62 ++++++++++++++++++++++++++++++++
lib/WellWell/Compose/Engine/ITL.pm | 49 +++++++++++++++++++++++++
lib/WellWell/Compose/Engine/Zoom.pm | 53 +++++++++++++++++++++++++++
lib/WellWell/Core.pm | 1 +
lib/WellWell/Engine.pm | 60 ++++++++++++++++++++++++++++++
7 files changed, 301 insertions(+), 6 deletions(-)
---
diff --git a/lib/WellWell/Compose.pm b/lib/WellWell/Compose.pm
index 06c91e0..fed49aa 100644
--- a/lib/WellWell/Compose.pm
+++ b/lib/WellWell/Compose.pm
@@ -22,8 +22,14 @@ use strict;
use warnings;
use Vend::Config;
+use Vend::Data;
use Vend::Tags;
+use WellWell::Engine qw/load_engine/;
+
+use WellWell::Compose::Engine::ITL;
+use WellWell::Compose::Engine::Zoom;
+
Vend::Config::parse_tag('UserTag', 'compose Order template');
Vend::Config::parse_tag('UserTag', 'compose HasEndTag');
Vend::Config::parse_tag('UserTag', 'compose AddAttr');
@@ -43,7 +49,7 @@ sub dots2hash {
sub compose {
my ($template, $opt, $body) = @_;
- my (%acl, %forms, $template_file, $container);
+ my (%acl, %forms, %engines, $template_file, $container);
if ($opt->{acl}) {
# check permissions first
@@ -85,6 +91,10 @@ sub compose {
}
}
+ if ($opt->{engine}) {
+ %engines = %{$opt->{engine}};
+ }
+
$opt->{body} ||= $body;
# preserve local body even after components.body=, as user might want it
$opt->{local_body} = $opt->{body};
@@ -221,8 +231,21 @@ sub compose {
$type = $attributes{$alias||$name}{css_type} || 'class';
}
- # locate component
- $components_file = "$::Variable->{MV_COMPONENT_DIR}/$name";
+ # locate component depending on the engine
+ my ($engine_name, $engine, $compobj);
+
+ if (exists $engines{$name}) {
+ $engine_name = $engines{$name};
+ }
+ else {
+ $engine_name = 'itl';
+ }
+ delete $Vend::Session->{engine}->{$engine_name};
+ unless ($Vend::Session->{engine}->{$engine_name} ||= load_engine($engine_name, database_exists_ref('products')->dbh())) {
+ die "Unknown template engine $engine_name\n";
+ }
+
+ $engine = $Vend::Session->{engine}->{$engine_name};
# add component
my $component_content;
@@ -230,10 +253,16 @@ sub compose {
if ($_ eq 'body' and $name eq 'local_body' ) {
$component_content = $opt->{local_body};
} else {
- $component_content = Vend::Tags->include($components_file);
+ if ($compobj = $engine->locate_component($name)) {
+ $component_content = $compobj->process($component_attributes);
- unless (defined $component_content && $name ne 'local_body') {
- Log("Component $name from $components_file not found");
+ unless (defined $component_content && $name ne 'zlocal_body') {
+ ::logError("Error processing component $name.");
+ Vend::Tags->error({name => 'component', set => "Error processing component $name."});
+ }
+ }
+ elsif ($name ne 'local_body') {
+ ::logError("Component $name not found");
Vend::Tags->error({name => 'component', set => "Component $name not found."});
}
}
diff --git a/lib/WellWell/Compose/Component/ITL.pm b/lib/WellWell/Compose/Component/ITL.pm
new file mode 100644
index 0000000..c0e59e8
--- /dev/null
+++ b/lib/WellWell/Compose/Component/ITL.pm
@@ -0,0 +1,41 @@
+# WellWell::Compose::Component::ITL - ITL component class for WellWell
+#
+# Copyright (C) 2010 Stefan Hornburg (Racke) <racke at linuxia.de>.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this program; if not, write to the Free
+# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+package WellWell::Compose::Component::ITL;
+
+sub new {
+ my ($class, @parms) = @_;
+
+ my $self = {@parms};
+
+ bless $self;
+
+ return $self;
+}
+
+sub process {
+ my ($self) = @_;
+ my ($content);
+
+ $content = Vend::Tags->include($self->{file});
+
+ return $content;
+}
+
+1;
diff --git a/lib/WellWell/Compose/Component/Zoom.pm b/lib/WellWell/Compose/Component/Zoom.pm
new file mode 100644
index 0000000..179329b
--- /dev/null
+++ b/lib/WellWell/Compose/Component/Zoom.pm
@@ -0,0 +1,62 @@
+# WellWell::Compose::Component::Zoom - Zoom component class for WellWell
+#
+# Copyright (C) 2010 Stefan Hornburg (Racke) <racke at linuxia.de>.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this program; if not, write to the Free
+# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+package WellWell::Compose::Component::Zoom;
+
+use Template::Zoom;
+use Template::Zoom::Specification::XML;
+use Template::Zoom::HTML;
+
+sub new {
+ my ($class, @parms) = @_;
+
+ my $self = {@parms};
+
+ bless $self;
+
+ return $self;
+}
+
+sub process {
+ my ($self, $attributes) = @_;
+ my ($content, $xml_spec, $spec, $html_object, $zoom);
+
+ # parse specification
+ $xml_spec = new Template::Zoom::Specification::XML;
+
+ unless ($spec = $xml_spec->parse_file($self->{specification})) {
+ die "$0: error parsing $xml_file: " . $xml_spec->error() . "\n";
+ }
+
+ $html_object = new Template::Zoom::HTML;
+
+ $html_object->parse_template($self->{template}, $spec);
+
+ for my $list_object ($html_object->lists()) {
+ # seed and check input
+ $list_object->input(\%input);
+ }
+
+ $zoom = new Template::Zoom ($html_object, $self->{dbh});
+
+ return $zoom->process($attributes);
+
+}
+
+1;
diff --git a/lib/WellWell/Compose/Engine/ITL.pm b/lib/WellWell/Compose/Engine/ITL.pm
new file mode 100644
index 0000000..cc08e87
--- /dev/null
+++ b/lib/WellWell/Compose/Engine/ITL.pm
@@ -0,0 +1,49 @@
+# WellWell::Compose::Engine::ITL - ITL composing engine for WellWell
+#
+# Copyright (C) 2010 Stefan Hornburg (Racke) <racke at linuxia.de>.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this program; if not, write to the Free
+# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+package WellWell::Compose::Engine::ITL;
+
+use strict;
+use warnings;
+
+use Vend::Tags;
+
+use WellWell::Compose::Component::ITL;
+
+sub new {
+ my ($class) = @_;
+ my $self = {};
+
+ bless $self;
+
+ return $self;
+}
+
+sub locate_component {
+ my ($self, $name) = @_;
+ my ($component);
+
+ if (-f "$::Variable->{MV_COMPONENT_DIR}/$name") {
+ $component = new WellWell::Compose::Component::ITL(file => "$::Variable->{MV_COMPONENT_DIR}/$name", name => $name);
+ return $component;
+ }
+}
+
+1;
+
diff --git a/lib/WellWell/Compose/Engine/Zoom.pm b/lib/WellWell/Compose/Engine/Zoom.pm
new file mode 100644
index 0000000..eb29ba7
--- /dev/null
+++ b/lib/WellWell/Compose/Engine/Zoom.pm
@@ -0,0 +1,53 @@
+# WellWell::Compose::Engine::Zoom - Zoom composing engine for WellWell
+#
+# Copyright (C) 2010 Stefan Hornburg (Racke) <racke at linuxia.de>.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this program; if not, write to the Free
+# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+package WellWell::Compose::Engine::Zoom;
+
+use strict;
+use warnings;
+
+use WellWell::Compose::Component::Zoom;
+
+sub new {
+ my $class = shift;
+ my $self = {@_};
+
+ bless $self;
+ return $self;
+}
+
+sub locate_component {
+ my ($self, $name) = @_;
+ my (%component_hash, $component);
+
+ if (-f "$::Variable->{MV_COMPONENT_DIR}/$name.xml"
+ && -f "$::Variable->{MV_COMPONENT_DIR}/$name.html") {
+ %component_hash = (dbh => $self->{dbh},
+ name => $name,
+ specification => "$::Variable->{MV_COMPONENT_DIR}/$name.xml",
+ template => "$::Variable->{MV_COMPONENT_DIR}/$name.html");
+
+ $component = new WellWell::Compose::Component::Zoom(%component_hash);
+ return $component;
+ }
+
+ return '';
+}
+
+1;
diff --git a/lib/WellWell/Core.pm b/lib/WellWell/Core.pm
index 20d1c16..3390563 100644
--- a/lib/WellWell/Core.pm
+++ b/lib/WellWell/Core.pm
@@ -27,6 +27,7 @@ use Vend::Config;
use WellWell::Cart;
use WellWell::Data;
use WellWell::Plugin qw/plugin_scan plugin_enable/;
+use WellWell::Engine;
# setup configuration directives
Vend::Config::parse_directive('Hook', 'Hook hook');
diff --git a/lib/WellWell/Engine.pm b/lib/WellWell/Engine.pm
new file mode 100644
index 0000000..4567a73
--- /dev/null
+++ b/lib/WellWell/Engine.pm
@@ -0,0 +1,60 @@
+# WellWell::Engine - WellWell template engine routines
+#
+# Copyright (C) 2010 Stefan Hornburg (Racke) <racke at linuxia.de>.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this program; if not, write to the Free
+# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+package WellWell::Engine;
+
+use strict;
+use warnings;
+
+require Exporter;
+our @ISA = qw(Exporter);
+our @EXPORT_OK = qw/load_engine/;
+
+our %engines = ('itl' => 'WellWell::Compose::Engine::ITL',
+ 'zoom' => 'WellWell::Compose::Engine::Zoom');
+
+use Vend::Config;
+
+sub load_engine {
+ my ($name, $dbh) = @_;
+ my ($class, $object);
+
+ # lookup class for engine
+ if (exists $engines{$name}) {
+ $class = $engines{$name};
+
+ eval "require $class";
+ if ($@) {
+ die "Failed to load $class: $@\n";
+ }
+
+ eval {
+ $object = $class->new(dbh => $dbh);
+ };
+ if ($@) {
+ die "Failed to load engine $name: $@\n";
+ }
+
+ return $object;
+ }
+
+ return;
+}
+
+1;
More information about the wellwell-devel
mailing list