Mojolicious-Plugin-Breadcrumbs

 view release on metacpan or  search on metacpan

lib/Mojolicious/Plugin/Breadcrumbs.pm  view on Meta::CPAN

package Mojolicious::Plugin::Breadcrumbs;

use Mojo::Base 'Mojolicious::Plugin';

our $VERSION = '1.001002'; # VERSION

use utf8;

my $Crumbs_Map = {
    '/' => 'Home',
};

sub register {
    my ($self, $app) = @_;

    $app->helper(
        breadcrumbs => sub {
            my $c = shift;
            @_ and $Crumbs_Map = shift;

            length $c->url_for->to_string
                or return; # if this is the case, we're just setting the
                           # $Crumbs_Map and not needing crumbs;

            my $path = '';
            my @crumbs;
            for my $crumb (
                '', grep length, split m{/}, $c->url_for->to_string
            ) {
                $path .= ($path eq '/' ? '' : '/') . $crumb;
                push @crumbs, {
                    path => $path,
                    text => $Crumbs_Map->{ $path }
                                // do{
                                    ( my $x = $crumb ) =~ tr/-_/ /;
                                    ucfirst $x;
                                },
                };
            }

            my $current_page = pop @crumbs;
            my $bread = '<section class="breadcrumbs">' .
                join(q{<span class="breadcrumb_sep">â–¸</span>},
                    (map qq{<a href="$_->{path}">$_->{text}</a>}, @crumbs),
                    '',
                );

            $bread .= '<span class="last_breadcrumb">'
                . ($current_page->{text}) . '</span></section>';

            return $bread;
        },
    );
}

'
Q. Why was the statement scared while the comment was not?
A. Statements are executed.
';

__END__

=encoding utf8

=for stopwords  breadcrumbs  autogenerating

=head1 NAME

Mojolicious::Plugin::Breadcrumbs - Mojolicious plugin for autogenerating breadcrumbs links

=head1 SYNOPSIS

=for html  <div style="display: table; height: 91px; background: url(http://zoffix.com/CPAN/Dist-Zilla-Plugin-Pod-Spiffy/icons/section-code.png) no-repeat left; padding-left: 120px;" ><div style="display: table-cell; vertical-align: middle;">

    #!perl

    use Mojolicious::Lite;

    plugin 'Breadcrumbs';

    get '/user/account-settings' => 'account-settings';

    app->start;

    __DATA__

    @@ account-settings.html.ep



( run in 0.569 second using v1.01-cache-2.11-cpan-5511b514fd6 )