[Catalyst] Re: Action for index not 'index'?

Marius Olsthoorn olsthrn at gmail.com
Thu Mar 24 09:17:27 GMT 2011


We use the same technique. We have some breadcrumbs that extend the
path of the previous breadcrumb. For example Home > User > Rating
corresponds with paths '/', '/user/<id>/, '/user/<id>/rating'.

Our version has support for this if you pass it an 'append_url' option
instead of just 'url' in the relevant parts of your chain.

Our version looks like:

sub add_breadcrumb {
    my ( $self, $c, $breadcrumb ) = @_;
    $c->stash->{breadcrumbs} ||= [];

    if ( defined( $breadcrumb->{append_url} ) ) {
        assert( not( defined( $breadcrumb->{url} ) ),
            'Breadcrumb argument has either append_url or url but not both' );

        my @breadcrumbs = @{ $c->stash->{breadcrumbs} };
        my $prev_uri    = URI->new( $breadcrumbs[-1]->{url} );
        my @segments    = $prev_uri->path_segments;

        while ( $segments[-1] eq '' ) {
            pop(@segments);
        }

        my @new_segments = split( qr|/|, $breadcrumb->{append_url},
KEEP_TRAILING_SPACE() );

        my $uri = URI->new();
        $uri->path_segments( @segments, @new_segments );

        $breadcrumb->{url} = $uri->as_string;

        delete( $breadcrumb->{append_url} );
    }

    push( @{ $c->stash->{breadcrumbs} }, $breadcrumb );
}

--
Marius Olsthoorn

On Wed, Mar 23, 2011 at 9:10 PM, Aristotle Pagaltzis <pagaltzis at gmx.de> wrote:
>
> * will trillich <will.trillich at serensoft.com> [2011-03-23 15:30]:
> > What we do instead is, we call a function to add another link
> > in our breadcrumb chain, so it's deliberate and we're
> > completely in control:
>
> That’s what I did too.
>
> > sub add_breadcrumb {
> >     my ( $c, $path, $label ) = @_;
> >     my $bc = $c->stash->{breadcrumbs} ||= [];
> >     push @$bc, +{
> >         path => $path,
> >         label=> $label,
> >     };
> > }
>
> Mine looks like this:
>
>    sub breadcrumb {
>        my $c = shift;
>        my $label = shift;
>        my $uri;
>
>        $uri = $c->uri_for_action( @_ )
>            if @_;
>
>        push @$_, {
>            label => $label,
>            href  => $uri.
>        } for $c->stash->{ breadcrumbs };
>
>        return $c;
>    }
>
> Note that it takes the label as first argument.
>
> This yields a few nice properties. You can set breadcrumbs as
> pure labels without a link simply by passing just the label.
> I use that option mainly on the final action of a chain. (The
> code in the template also omits the `<a>` on the last breadcrumb
> even if there is a link.)
>
> And aside from the first argument it’ll work just like Catalyst’s
> own `uri_for_action`.
>
> So in a typical mid-chain action I get something like this:
>
>    sub base : PathPart('workspace') CaptureArgs(0) {
>        # ...
>        $c->breadcrumb( 'Workspace', '/workspace/list' );
>    }
>
>    sub item : PathPart('') CaptureArgs(1) {
>        # ...
>        $c->breadcrumb( $ws->name, '/workspace/view', [$ws->id] );
>    }
>
> So the chain structure automatically yields the right choice and
> sequence of breadcrumbs.
>
> --
> *AUTOLOAD=*_;sub _{s/::([^:]*)$/print$1,(",$\/"," ")[defined wantarray]/e;chop;$_}
> &Just->another->Perl->hack;
> #Aristotle Pagaltzis // <http://plasmasturm.org/>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/



More information about the Catalyst mailing list