[Catalyst] Relationship layout

Andreas Marienborg omega at palle.net
Wed Oct 5 20:09:42 CEST 2005


If you have set the $c->stash->{artist} correctly, you should be able  
to do the following in your template:

[% artist.<FIELD> %]

if you use Template Toolkit

for instance:

[% artist.name %]

if artist has a field "name".

There should be noe need to do the EVAL_PERL, PERL stuff at all, as  
everything in the stash is available to the template-engine, atleast  
if your view is set up correctly.

My tip is still to look at the examples, intro-articles and such, as  
they give you a pretty good idea of how to get started.

andreas

On 5. okt. 2005, at 20.00, Will Smith wrote:

> Thanks for the tip. Everything works well now for the has_a.
> In the muic::M::CDBI::Artist I define the has_many as (artist has  
> many Cd):
> -------
> music:M:::CDBI::Artist->has_many(cds =>  
> 'music::M::CDBI::Cd'=>'artistid');
> -------
> I've tried all possibilities but just can't get the data out of the  
> stash.
> In muic::C::CDBI::Artist i have a sub named beatle
> -----
> sub beatle : Local {
>     my ( $self, $c ) = @_;
>
>     $c->stash->{artist} = music::M::CDBI::Artist->search_like 
> (name=>'%beatle%');
>     $c->stash->{template} = 'Artist/test.tt';
> }
> --------
> If I run the script: ---------
> print $artist->name,"\n";
>
> foreach ($artist->cds) {
>         print "\t",$_->title,"\n";
> }---------------
> it worksin the STDOUT.
> 1-how can I get it on the Template.
> 2-on a template (within catalyst), can we set the EVAL_PERL => 1 in  
> order to use rawperl by:
> [% SET EVAL_PERL  = 1 %]
> [% PERL %]
> #!/perl/usr/perl
> ....
> [% END %]
>
>
> Thanks a lot.
>
> Andreas Marienborg <omega at palle.net> wrote:
> You seem to mix models and controllers.. Models are where teh
> relationships go (as you obviously have gotten to work, thats good).
> Controllers use theese models to fetch the right stuff from the DB/
> backend, and make it available to the View(s). The way to do that is
> via the "stash":
>
> package music::C::Cd; # note the lack of CDBI here, the CDBI is
> naming convention for Models, but rarely used in controller  
> namespaces.
>
> sub blue : Local {
> my ($self, $c) = @_;
> $c->stash->{cds} = music::M::CDBI::Cd->search(title => "Blue");
> $c->stash->{template} = 'cds/list.tt'; # For instance
> }
>
>
> then you can access cds from your template
>
> [% WHILE (cd = cds.next) %]
> [% cd.title %]
>
> [% END %]
>
> or somesuch.
>
> I would recomend the article by our beloved Jesse
> http://www.perl.com/pub/a/2005/06/02/catalyst.html
>
> and also the documentation of the Catalystpackage itself:
> http://cpansearch.perl.org/~mramberg/Catalyst-5.33/
>
> hope that helps you a little bit along
>
> andreas
>
>
> Yahoo! for Good
> Click here to donate to the Hurricane Katrina relief effort.
> _______________________________________________
> Catalyst mailing list
> Catalyst at lists.rawmode.org
> http://lists.rawmode.org/mailman/listinfo/catalyst
>




More information about the Catalyst mailing list