NAME
Template::Plugin::Handel::Cart - Template Toolkit plugin for shopping cart
VERSION
$Id: Cart.pm 203 2005-02-20 19:32:40Z claco $
SYNOPSIS
[% USE Handel.Cart %]
[% IF (cart = Handel.Cart.fetch(id => 'A2CCD312-73B5-4EE4-B77E-3D027349A055')) %]
[% cart.name %]
[% FOREACH item IN cart.items %]
[% item.sku %]
[% END %]
[% END %]
DESCRIPTION
Template::Plugin::Handel::Cart
is a TT2 (Template Toolkit 2) plugin for Handel::Cart
. It's API is exactly the same as Handel::Cart
with a few minor exceptions noted below.
Since new
and load
are used by TT2 to load plugins, Handel::Carts new
and load
can be accesed using create
and fetch
.
CAVEATS
Template Toolkit
handles method params in a smart fashion that allows you to pass named parameters into methoda and it will convert them into HASH references.
For example:
[% cart.method(name1=val1, name2=val2, otherarg);
is turned into:
cart->method(otherarg, {name1=>val1, name2=>val2});
Unfortunatly, it looks like TT2 reverses the @ARGS order during translation. This causes problems with Handel::Cart::load
and items
as they expect ($hashref, $wantiterator)
instead.
Do to this, it is recommended that you always use the same explicit form as you would use when calling Handel::Cart
when calling create
and items
:
[% cart.method({name1=>val1, name2=>val2}, $wantiterator) %]
Other issue is how Handel::Cart
returns an iterator or an array based on its inspection ot wantarray
. It appears that TT2 thwarts wantarray
in some manner.
For example:
[% carts = Handel.Carts.fetch() %]
returns an array reference sinceit's not clear at this point what you really want. To counteract this behaviour, you can use RETURNAS
constants to specify the exact output desired:
[% USE hdl = Handel.Constants %]
[% carts = Handel.cart.fetch(undef, hdl.RETURNAS_ITERATOR) %]
This will force a return of a Handel::Iterator
in scalar context. Then you can simply loop through the iterator:
[% WHILE (cart = carts.next) %]
...
[% END %]
On the upshot, if you are only expecting a single result, like loading a specific cart by id
, then it will just do Do What You Want:
[% cart = Handel.Cart.fetch({id => '12345678-7654-3212-345678987654'}) %]
[% cart.id %]
[% cart.name %]
...
You can even use FOREACH
without specifying the return type as TT2 appears to just Do The Right Thing regardless of whether it receives an array or a single Handel::Cart
or Handel::Cart::Item
object:
[% FOREACH cart IN Handel.Cart.fetch({id => '12345678-7654-3212-345678987654'}}) %]
[% FOREACH item IN cart.items %]
[% item.sku %]
[% END %]
[% END %]
CONSTRUCTOR
Unlike using Handel::Cart
to create a new cart object using new
and load
, Template::Plugin::Handel::Cart
takes a slightly different approach to cart objects. Because USE
ing in TT2 calls new
, we first USE
or create a new Template::Plugin::Handel::Cart
object then create
or load
to return a new cart object, iterator, or array of carts.
create(\%filter)
-
[% USE Handel.Cart %] [% IF (cart = Handel.Cart.create({ shopper => '12345678-9876-5432-1234-567890987654', name => 'My New Cart', description =>'Favorite Items'})) %} [% cart.name %] ... [% END %]
fetch(\%filter [, $wantiterator])
-
The safest way to get a cart is to use FOREACH. This negates the need to specfy
$wanteriterator
forHandel::Cart::load
. See CAVEATS for further info on$wantiterator
, Perlswantarray
within TT2.[% USE Handel.Cart %] [% FOREACH cart IN Handel.Cart.fetch({shopper => '12345678-9876-5432-1234-567890987654'}) %] [% cart.id %] [% cart.name %] ... [% END %]
SEE ALSO
Template::Plugin::Handel::Constants, Handel::Constants, Handel::Cart
AUTHOR
Christopher H. Laco
CPAN ID: CLACO
cpan@chrislaco.com
http://today.icantfocus.com/blog/