Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add support for template engines.
ITL is the default and Zoom (based on Template::Zoom) is the new kid on the block.
  • Loading branch information
racke committed Oct 19, 2010
1 parent a5da81d commit d45abac
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 6 deletions.
41 changes: 35 additions & 6 deletions lib/WellWell/Compose.pm
Expand Up @@ -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');
Expand All @@ -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
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -221,19 +231,38 @@ 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;

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."});
}
}
Expand Down
41 changes: 41 additions & 0 deletions 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@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;
62 changes: 62 additions & 0 deletions 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@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;
49 changes: 49 additions & 0 deletions 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@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;

53 changes: 53 additions & 0 deletions 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@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;
1 change: 1 addition & 0 deletions lib/WellWell/Core.pm
Expand Up @@ -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');
Expand Down
60 changes: 60 additions & 0 deletions lib/WellWell/Engine.pm
@@ -0,0 +1,60 @@
# WellWell::Engine - WellWell template engine routines
#
# Copyright (C) 2010 Stefan Hornburg (Racke) <racke@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;

0 comments on commit d45abac

Please sign in to comment.