FAQ
Hello All,

I'm working on adding breadcrumbs to my catalyst app and I've run into
something unexpected.

I'm using Catalyst::Plugin::Breadcrumbs. Let's assume I have a
controller Foo. If I just go to /foo ( the index ), the plugin assumes
that the action will be 'index', but the action is actually just
'foo'.

Because of this, my breadcrumbs show as Foo/Foo instead of Foo/Index (
or just Foo if hide_index is true ).

Is this expected behaviour? Has the behaviour changed since
Plugin::Breadcrumbs was written? I've been able to modify the code for
the plugin to compensate, but was hoping someone could shed some light
on this subject.

Thanks,
Kyle

http://www.kylehall.info
Mill Run Technology Solutions ( http://millruntech.com )
Crawford County Federated Library System ( http://www.ccfls.org )
Meadville Public Library ( http://www.meadvillelibrary.org )

Search Discussions

  • Will Trillich at Mar 23, 2011 at 2:27 pm
    We tried some of the breadcrumb libraries too, but couldn't wrestle them
    into our paradigm, either. There are underlying assumptions about your app
    or url structure where it tries to divine patterns on its own that often
    just don't jibe with what you've got set up.

    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:

    $c->add_breadcrumb( "/some/$url_component/here", 'Text for link here' );

    throughout our code, and then in the template:

    <ul id="breadcrumbs">
    <li>You are here:</li>
    [% FOREACH crumb IN breadcrumbs -%]
    <li>[%
    IF loop.last %]<span title="You are here">[% crumb.label %]</span>[%
    ELSE %]<a href="[% c.uri_for(crumb.path).path %]">[% crumb.label
    %]</a>[%
    END
    %]</li>
    [% END -%]
    </ul>

    And here's MyApp::add_breadcrumb en toto--

    sub add_breadcrumb {
    my ( $c, $path, $label ) = @_;
    my $bc = $c->stash->{breadcrumbs} ||= [];
    push @$bc, +{
    path => $path,
    label=> $label,
    };
    }
    On Wed, Mar 23, 2011 at 8:40 AM, Kyle Hall wrote:

    Hello All,

    I'm working on adding breadcrumbs to my catalyst app and I've run into
    something unexpected.

    I'm using Catalyst::Plugin::Breadcrumbs. Let's assume I have a
    controller Foo. If I just go to /foo ( the index ), the plugin assumes
    that the action will be 'index', but the action is actually just
    'foo'.

    Because of this, my breadcrumbs show as Foo/Foo instead of Foo/Index (
    or just Foo if hide_index is true ).

    Is this expected behaviour? Has the behaviour changed since
    Plugin::Breadcrumbs was written? I've been able to modify the code for
    the plugin to compensate, but was hoping someone could shed some light
    on this subject.

    Thanks,
    Kyle

    http://www.kylehall.info
    Mill Run Technology Solutions ( http://millruntech.com )
    Crawford County Federated Library System ( http://www.ccfls.org )
    Meadville Public Library ( http://www.meadvillelibrary.org )

    _______________________________________________
    List: Catalyst@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/


    --
    11 cheers for binary!
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20110323/fbd3fd0c/attachment.htm
  • Kyle Hall at Mar 23, 2011 at 5:16 pm
    That sounds like a good technique. I've modified Plugin::Breadcrumb to
    work properly and put it in my source tree, but I think I like your
    technique better.

    Kyle

    http://www.kylehall.info
    Mill Run Technology Solutions ( http://millruntech.com )
    Crawford County Federated Library System ( http://www.ccfls.org )
    Meadville Public Library ( http://www.meadvillelibrary.org )




    On Wed, Mar 23, 2011 at 10:27 AM, will trillich
    wrote:
    We tried some of the breadcrumb libraries too, but couldn't wrestle them
    into our paradigm, either. There are underlying assumptions about your app
    or url structure where it tries to divine patterns on its own that often
    just don't jibe with what you've got set up.
    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:
    $c->add_breadcrumb( "/some/$url_component/here", 'Text for link here' );
    throughout our code, and then in the template:
    <ul id="breadcrumbs">
    <li>You are here:</li>
    [% FOREACH crumb IN breadcrumbs -%]
    ?? ?<li>[%
    ?? ? ? ?IF loop.last %]<span title="You are here">[% crumb.label %]</span>[%
    ?? ? ? ?ELSE %]<a href="[% c.uri_for(crumb.path).path %]">[% crumb.label
    %]</a>[%
    ?? ? ? ?END
    ?? ?%]</li>
    [% END -%]
    </ul>
    And here's MyApp::add_breadcrumb en toto--
    sub add_breadcrumb {
    ?? ?my ( $c, $path, $label ) = @_;
    ?? ?my $bc = $c->stash->{breadcrumbs} ||= [];
    ?? ?push @$bc, +{
    ?? ? ? ?path => $path,
    ?? ? ? ?label=> $label,
    ?? ?};
    }
    On Wed, Mar 23, 2011 at 8:40 AM, Kyle Hall wrote:

    Hello All,

    I'm working on adding breadcrumbs to my catalyst app and I've run into
    something unexpected.

    I'm using Catalyst::Plugin::Breadcrumbs. Let's assume I have a
    controller Foo. If I just go to /foo ( the index ), the plugin assumes
    that the action will be 'index', but the action is actually just
    'foo'.

    Because of this, my breadcrumbs show as Foo/Foo instead of Foo/Index (
    or just Foo if hide_index is true ).

    Is this expected behaviour? Has the behaviour changed since
    Plugin::Breadcrumbs was written? I've been able to modify the code for
    the plugin to compensate, but was hoping someone could shed some light
    on this subject.

    Thanks,
    Kyle

    http://www.kylehall.info
    Mill Run Technology Solutions ( http://millruntech.com )
    Crawford County Federated Library System ( http://www.ccfls.org )
    Meadville Public Library ( http://www.meadvillelibrary.org )

    _______________________________________________
    List: Catalyst@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/


    --
    11 cheers for binary!
  • Tomas Doran at Mar 27, 2011 at 7:16 am

    On 23 Mar 2011, at 17:16, Kyle Hall wrote:

    That sounds like a good technique. I've modified Plugin::Breadcrumb to
    work properly and put it in my source tree, but I think I like your
    technique better.
    Is there a way to make your change generic, and send the author a patch?

    As then you don't have to maintain that code yourself any more, and
    everyone else gets to benefit ;)

    Cheers
    t0m
  • Kyle Hall at Mar 27, 2011 at 1:39 pm
    I suppose I could try, the code hasn't been updated since 2006, so I
    don't know if the author is still actively maintaining it. I guess I
    could always upload a fork if I had to.

    Kyle

    http://www.kylehall.info
    Mill Run Technology Solutions ( http://millruntech.com )
    Crawford County Federated Library System ( http://www.ccfls.org )
    Meadville Public Library ( http://www.meadvillelibrary.org )



    On Sun, Mar 27, 2011 at 3:20 AM, Tomas Doran wrote:
    On 23 Mar 2011, at 17:16, Kyle Hall wrote:

    That sounds like a good technique. I've modified Plugin::Breadcrumb to
    work properly and put it in my source tree, but I think I like your
    technique better.
    Is there a way to make your change generic, and send the author a patch?

    As then you don't have to maintain that code yourself any more, and everyone
    else gets to benefit ;)

    Cheers
    t0m

  • Aristotle Pagaltzis at Mar 23, 2011 at 8:10 pm

    * will trillich [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/>
  • Marius Olsthoorn at Mar 24, 2011 at 9:17 am
    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 wrote:

    * will trillich [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@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/
  • Aristotle Pagaltzis at Mar 24, 2011 at 9:00 pm

    * Marius Olsthoorn [2011-03-24 10:25]:
    We use the same technique. We have some breadcrumbs that extend
    the path of the previous breadcrumb. Eg. 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.
    This encodes your URI structure into your `add_breadcrumb` call
    *sequence* as well as the chain structure, though. If you leave
    the job all to `uri_for_action` you can shuffle things around and
    the breadcrumb links will continue to be correct with no extra
    work. I doesn?t happen a lot, but I was glad for the magic each
    time it did. It doesn?t cause much redundancy either, you just
    have to pass a few ID values a few times extra.

    Regards,
    --
    Aristotle Pagaltzis // <http://plasmasturm.org/>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedMar 23, '11 at 1:40p
activeMar 27, '11 at 1:39p
posts8
users5
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase