[Petal] Re: Mason + Petal

Mark Holland mark at thinkfoo.com
Fri Jan 17 12:44:04 GMT 2003


Hi *,

Quoting Jean-Michel Hiver <jhiver at mkdoc.com>:

> Mark, could you please bounce this mail sent to William to the list? I
> think it'd be quite interesting to have it in the archives for
> reference.

It seems people might be interested in knowing how to combine Mason and Petal,
so I'm rewriting a mail I wrote to William McKee and posting it to the list. I
would suggest familiarising yourself with Mason (www.masonhq.com) before
attempting to implement this.

One thing before I start -I am just a hacker and my code aint that pretty!

Firstly you need to modify the existing mason class
HTML::Mason::Component::FileBased.  See
(http://perl.thinkfoo.com/petal/FileBased.pm) for example. This adds the
template() routine to any component within Mason.

Secondly you need something like the HTML::Mason::PetalWrapper class (example
here: http://perl.thinkfoo.com/petal/PetalWrapper.pm).
This sits between the template and petal and inserts mason's global $m (the
request object) into the arguments passed to petal's process() routine so that 
it is available as 'mason' within a template. Allowing you to do something like:
<FORM petal:attributes="action mason/current_comp/path">

Now you need a custom Request class to inherit from HTML::Mason::Request. This
class intercepts the Request class' fetch_comp() and fetch_next() methods and
ensures they pass on the global $m object. I'm sure there's a neater way of
doing this I just havent been arsed to figure out how. An example class
HTML::Mason::Request::Petal is available here
http://perl.thinkfoo.com/petal/Petal.pm.

Lastly in your Apache handler.pl or in your httpd.conf you need to tell Mason it
to use a custom request_class and configure your @Petal::BASE_DIR to include
your component root.

Where you store the templates is up to you, but I have done it like this:
Templates are called 'default' and stored in directories which have the same
name as the component but preceded with a '.'
e.g. 
component:    /test_comp        # (relative to component root)
template:     /.test_comp/default
You can hack around with the FileBased.pm module to change this to whatever you
fancy.

Now within your mason component inside a perl block you should be able to call:
$m->current_comp->template->process(this => 'that', foo => 'bar');
or to call template other tha default:
$m->current_comp->template('cust_file')->process(this => 'that', foo => 'bar');

Lastly I would say Mason _does_ add some overhead. I'm sure hacking about with
the Mason stuff to reduce the amount of parsing it has to do could speed things
up, but if you do some code profiling with Devel::Profiler::Apache you will see
that petal is still taking up a fat chunk. 

That's pretty much it. This isn't a brilliant implementation, but it works for
me. If any gurus fancy doing a better job, or have some better ideas on how to
do this, I would be interested to see them.

Cheers,
Mark

-- 
Mark Holland
mark at thinkfoo.com

-------------------------------------------------
This mail sent through IMP: http://horde.org/imp/



More information about the Petal mailing list