[Petal] [TRICK] Custom modifiers in your applications with Petal

Jean-Michel Hiver jhiver at mkdoc.com
Thu May 29 13:08:10 BST 2003


Hi List,

I would like to submit this trick to the list. Your applications are
split in a horde of modules, and you would like to access some of the
those modules functionality from Petal.

For example, let's imagine that you have a class 'FruitFactory' with a
static instanciate ($fruitclass, @args) method that instanciates fruits.
You want to be able to instanciate those fruits from Petal.


In your FruitFactory class, write:

    use Petal; # sell your soul!
    $Petal::Hash::MODIFIERS->{'new_fruit:'} = sub {
        my $hash = shift;
        my @args = @_;
        return FruitFactory->instanciate (@_);
    };


Then from your template you can do:

    <p
      petal:define="apple new_fruit: Fruit::Apple big red"
      petal:content="apple/as_string">Banana
    </p>
      
      
At the moment I use this technique to instanciate various objects
(config, web logic, data objects) which I need to use and it works well!

Cheers,
-- 
Building a better web - http://www.mkdoc.com/
---------------------------------------------
Jean-Michel Hiver
jhiver at mkdoc.com  - +44 (0)114 255 8097
Homepage: http://www.webmatrix.net/


More information about the Petal mailing list