| 1 | |||||||||||||||||||||
| 2 | =head1 NAME | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | Templer::Plugin::Breadcrumbs - A plugin to create breadcrumbs | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =cut | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 SYNOPSIS | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | The following is a good example use of this plugin | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | title: About my site | ||||||||||||||||||||
| 13 | crumbs: Home,Software | ||||||||||||||||||||
| 14 | ---- | ||||||||||||||||||||
| 15 | <p>This is my page content...</p> | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | Here the variable 'crumbs' will be converted into a loop variable called | ||||||||||||||||||||
| 19 | 'breadcrumbs'. THe links and titls will be set automatically, but if you | ||||||||||||||||||||
| 20 | need to override them you can do so via: | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | title: About my site | ||||||||||||||||||||
| 23 | crumbs: Home,Software[Soft],Testing[Test] | ||||||||||||||||||||
| 24 | ---- | ||||||||||||||||||||
| 25 | <p>This is my page content...</p> | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | That will result in links to /, /Soft, and /Soft/Test, respectively. With | ||||||||||||||||||||
| 29 | display names of "Home", "Software" and "Testing". | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | =head1 DESCRIPTION | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | This plugin expands the values of "crumbs", as written in a template, to | ||||||||||||||||||||
| 36 | a loop-variable with the name "breadcrumbs". | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | This can be used in a template like so: | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =for example begin | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | <!-- tmpl_if name='breadcrumbs' --> | ||||||||||||||||||||
| 43 | <ul> | ||||||||||||||||||||
| 44 | <!-- tmpl_loop name='breadcrumbs' --> | ||||||||||||||||||||
| 45 | <li><a href="<!-- tmpl_var name='link' -->"><!-- tmpl_var name='title' --></a></li> | ||||||||||||||||||||
| 46 | <!-- /tmpl_loop --> | ||||||||||||||||||||
| 47 | </ul> | ||||||||||||||||||||
| 48 | <!-- /tmpl_if --> | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =for example end | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | This template is an example. | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =cut | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =head1 LICENSE | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | This module is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 59 | under the terms of either: | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | a) the GNU General Public License as published by the Free Software | ||||||||||||||||||||
| 62 | Foundation; either version 2, or (at your option) any later version, | ||||||||||||||||||||
| 63 | or | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | b) the Perl "Artistic License". | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =head1 AUTHOR | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | Steve Kemp <steve@steve.org.uk> | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | Copyright (C) 2015 Steve Kemp <steve@steve.org.uk>. | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | This library is free software. You can modify and or distribute it under | ||||||||||||||||||||
| 80 | the same terms as Perl itself. | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =head1 METHODS | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 11 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 90 | 11 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | package Templer::Plugin::Breadcrumbs; | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =head2 new | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | Constructor. No arguments are required/supported. | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | sub new | ||||||||||||||||||||
| 103 | 11 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 104 | 11 | my ( $proto, %supplied ) = (@_); | |||||||||||||||||||
| 105 | 11 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 11 | my $self = {}; | |||||||||||||||||||
| 108 | 11 | bless( $self, $class ); | |||||||||||||||||||
| 109 | 11 | return $self; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =head2 expand_variables | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | This is the method which is called by the L<Templer::Plugin::Factory> | ||||||||||||||||||||
| 117 | to expand the variables contained in a L<Templer::Site::Page> object. | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | This method will expand any variable that is called 'crumbs' into a HTML::Template | ||||||||||||||||||||
| 120 | loop, suitable for the display of breadcrumbs. | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | sub expand_variables | ||||||||||||||||||||
| 125 | 9 | ▶ | { | ||||||||||||||||||
|
Subroutine
expand_variables: called CC=8 SCAR=37.3
Pod
documented
| |||||||||||||||||||||
| 126 | 9 | my ( $self, $site, $page, $data ) = (@_); | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | # | ||||||||||||||||||||
| 129 | # Get the page-variables in the template. | ||||||||||||||||||||
| 130 | # | ||||||||||||||||||||
| 131 | 9 | my %hash = %$data; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | # | ||||||||||||||||||||
| 134 | # Look for a value of "read_file" in each key. | ||||||||||||||||||||
| 135 | # | ||||||||||||||||||||
| 136 | 9 | foreach my $key ( keys %hash ) | |||||||||||||||||||
| 137 | { | ||||||||||||||||||||
| 138 | 66 | ▶ | if ( $key =~ /^crumbs$/i ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | { | ||||||||||||||||||||
| 140 | 0 | my $loop; | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 0 | my $val = $hash{ $key }; | |||||||||||||||||||
| 143 | 0 | my $link = "/"; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 0 | foreach my $path ( split( /,/, $val ) ) | |||||||||||||||||||
| 146 | { | ||||||||||||||||||||
| 147 | 0 | $path =~ s/^\s+|\s+$//g; | |||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 0 | ▶ | if ( $path =~ /Home/i ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | { | ||||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | else | ||||||||||||||||||||
| 153 | { | ||||||||||||||||||||
| 154 | 0 | ▶ | if ( $path =~ /(.*)\[(.*)\]/ ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 155 | { | ||||||||||||||||||||
| 156 | 0 | $link .= "/" . $1; | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | else | ||||||||||||||||||||
| 159 | { | ||||||||||||||||||||
| 160 | 0 | $link .= "/" . $path; | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 0 | $link =~ s/\/\//\//g; | |||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 0 | ▶ | if ( $path =~ /\[(.*)\]/ ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 167 | { | ||||||||||||||||||||
| 168 | 0 | $path = $1; | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | else | ||||||||||||||||||||
| 171 | { | ||||||||||||||||||||
| 172 | 0 | $path = ucfirst($path); | |||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | 0 | push( @$loop, | |||||||||||||||||||
| 175 | { title => $path, | ||||||||||||||||||||
| 176 | link => $link | ||||||||||||||||||||
| 177 | } ); | ||||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 0 | ▶ | $hash{ 'breadcrumbs' } = $loop if ($loop); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | 0 | delete $hash{ $key }; | |||||||||||||||||||
| 183 | } | ||||||||||||||||||||
| 184 | } | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 9 | return ( \%hash ); | |||||||||||||||||||
| 187 | } | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | # | ||||||||||||||||||||
| 191 | # Register the plugin. | ||||||||||||||||||||
| 192 | # | ||||||||||||||||||||
| 193 | 11 | Templer::Plugin::Factory->new() | |||||||||||||||||||
| 194 | ->register_plugin("Templer::Plugin::Breadcrumbs"); | ||||||||||||||||||||