[Maypole] additional_data and template_args

Dave Howorth Dave.Howorth@acm.org
Mon, 03 Jan 2005 11:51:50 +0000


Peter Speltz wrote:
> In general the additional_data sub only allows you to generate class 
> specific data for your templates.  This is because of where it is 
> called. It is called before the objects are generated.  So if you go 
> to "customer/view/21" additional_data is called before customer 21 is
>  even retrieved from the database and object is created.  This means 
> you cant generate template args based on customer 21 object in 
> additional_data.
...
> So in Customer.pm and Repair.pm classes I define a "menu_data" sub
> and thus specify my menu.
...
> That works great when I want menus solely based on classes ie the
> general customer and repair menus : "addnew, search, list".  But  ***
> I want menus based on objects as well. ie: "edit, delete, add a
> repiar to John Smith, Look at John Smiths 15 late payments, Email
> John Smith" . . . You see -- menu based on my objects.  The
> difference to me is simply calling menu_data as a class or object
> method.
...
 > my additional_data would look like this:
 > sub additional_data {
 > ...
 > # get object menu if we have only one object:
 > if (@{$r->objects} eq 1) {
 >     $r=>objects->[0]->menu_data;
 > }
 > # else class menu
 > else {
 >     $r->model_class->menu_data;
 > }

If I've understood you properly, you have, or would have, a method 
called menu_data in your model subclasses. When it's called it generates 
some object-specific data structure or text string and attaches it to 
the template_args hash so it is made available as a template variable.

If that's so, I think there's a simpler solution. Delete the last line 
of menu_data! Don't attach the data structure to a template variable, 
just return the data structure directly. Then change where it's called. 
Instead of calling it in additional_data, call it directly as an object 
method in the template:  [% menu = customer.menu_data %]

Cheers, Dave


-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.298 / Virus Database: 265.6.7 - Release Date: 30/12/04