[Petal] Implementation question
William McKee
william@knowmad.com
Wed, 11 Sep 2002 08:25:20 -0400
--Message-Boundary-7282
Content-type: text/plain; charset=US-ASCII
Content-transfer-encoding: 7BIT
Content-description: Mail message body
Hi Jean-Michel,
Thanks for the extensive example of using methods with Petal. I look
forward to trying to incorporate the technique into future projects.
In the meantime, I've added it to the growing list of examples for using
Petal that I've started keeping for my own edification. I've attached the
file for comments/additions. I need to capture some more of the basic
techniques in order for it to be a real cookbook, but perhaps it could be
useful to include with the Petal release files with a bit more work.
Regards,
William
On 21 Aug 2002 at 17:55, Jean-Michel Hiver wrote:
> Hi William,
>
> I'm going to make the following assumptions:
>
> * all your records are hashrefs which come from some database
> * you have a list of them to display
>
> Let's say that the database table looks like this:
>
> Raters (id, first_name, last_name, relation, phone, email)
>
> You could bless each record into a package as is:
>
> use MyApplication::Record::Rater;
> my @records = complicated_query_somewhere_else();
> bless $_, "MyApplication::Record::Rater" for (@records);
>
>
> Your module could look like that:
>
> package MyApplication::Record::Rater;
> use strict;
> use warnings;
> use CGI;
> use Carp;
>
> sub is_current_id
> {
> my $self = shift;
> my $cgi = CGI->new;
> my $id = $cgi->param ('rater.id');
> return unless (defined $id and $id and $id =~ /^\d+$/);
> return $id == $self->{id};
> }
>
> 1;
>
> Then on top of your existing data, you have a method which you can call
> from Petal, i.e.
>
> <span petal:condition="true:record/is_current_id">
> blah blah blah...
> </span>
>
> This trick can also be used when you have foreign keys in database
> fields.
>
> <fictious_scenario>
>
> For example, let's imagine that you have a column called
> 'friend_id'. It references another 'rater' which is supposed to be a
> friend of that person.
>
> You could defined the following subroutine:
>
> # give me the friend record for that person
> sub friend
> {
> my $self = shift;
> my $friend_id = $self->{friend_id};
> my $sql = 'select * from rater where id = ?';
> my $sth = $::DBH_CONNECTION->prepare_cached ($sql);
> $sth->execute ($friend_id);
> my $hash = $sth->fetchrow_hashref;
> return unless (defined $hash);
>
> bless $hash, "MyApplication::Record::Rater";
> return $hash;
> }
>
> Then in your template, you could do:
>
> <?petal:if name="true:rater/friend"?>
> Your friend is:
> $encode:rater/friend/first_name
> $encode:rater/friend/last_name
> <?petal:end?>
>
> </fictious_scenario>
>
>
> This system is quite poweful, I use it heavily all the time, and that's why
> Petal supports invoking method on objects.
>
> In other words, OO-Perl rulez :-)
>
> Cheers,
> --
> IT'S TIME FOR A DIFFERENT KIND OF WEB
> ================================================================
> Jean-Michel Hiver - Software Director
> jhiver@mkdoc.com
> +44 (0)114 255 8097
> ================================================================
> VISIT HTTP://WWW.MKDOC.COM
>
--
Lead Developer
Knowmad Services Inc. || Internet Applications & Database Integration
http://www.knowmad.com
--Message-Boundary-7282
Content-type: text/plain; charset=US-ASCII
Content-disposition: inline
Content-description: Attachment information.
The following section of this message contains a file attachment
prepared for transmission using the Internet MIME message format.
If you are using Pegasus Mail, or any another MIME-compliant system,
you should be able to save it or view it from within your mailer.
If you cannot, please ask your system administrator for assistance.
---- File information -----------
File: Cookbook.pod
Date: 11 Sep 2002, 8:15
Size: 4498 bytes.
Type: Unknown
--Message-Boundary-7282
Content-type: Application/Octet-stream; name="Cookbook.pod"; type=Unknown
Content-disposition: attachment; filename="Cookbook.pod"
Content-transfer-encoding: BASE64
PWhlYWQxIE5BTUUNCg0KUGV0YWw6OkNvb2tib29rIC0gUmVjaXBlcyBmb3IgYnVpbGRpbmcg
dGVtcGxhdGVzIHdpdGggUGV0YWwNCg0KPWhlYWQxIFNZTk9QU0lTDQoNCk5vdCBBcHBsaWNh
YmxlDQoNCj1oZWFkMSBERVNDUklQVElPTg0KDQpUaGlzIGRvY3VtZW50IGNvbnRhaW5zIHNv
bWUgZXhhbXBsZXMgb2YgTDxQZXRhbHxQZXRhbD4gdGVtcGxhdGUgdXNhZ2UuIA0KDQoNCj1o
ZWFkMSBOT1RFUw0KSSB0ZW5kIHRvIHVzZSBQZXRhbCB0byBnZW5lcmF0ZSBIVE1MIGZpbGVz
IGZyb20gSFRNTCB0ZW1wbGF0ZXMuIEkgbmFtZSB0aGVzZSB0ZW1wb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