| 1 | package Template::Caribou::Tags; | ||||||||||||||||||||||||||||
| 2 | 17 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||||||||||
| 3 | #ABSTRACT: generates tags functions for Caribou templates | ||||||||||||||||||||||||||||
| 4 | 17 | $Template::Caribou::Tags::VERSION = '1.1.0'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 17 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 17 | ▶ | use Template::Caribou::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 17 | ▶ | use List::AllUtils qw/ pairmap pairgrep /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 17 | ▶ | use parent 'Exporter::Tiny'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 17 | ▶ | use experimental 'signatures', 'postderef'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 17 | our @EXPORT_OK = qw/ render_tag mytag attr /; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | ▶ | sub attr(@){ | |||||||||||||||||||||||||||
|
Subroutine
attr: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 18 | ▶ | return $_{$_[0]} if @_ == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 17 | ▶ | croak "number of attributes must be even" if @_ % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 17 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 17 | ▶ | while( my ( $k, $v ) = splice @_, 0, 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 25 | ▶ | if ( $k =~ s/^\+// ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 30 | 3 | $_{$k} = { map { $_ => 1 } split ' ', $_{$k} } | |||||||||||||||||||||||||||
| 31 | 3 | ▶ | unless ref $_{$k}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | $_{$k}{$v} = 1; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | elsif ( $k =~ s/^-// ) { | ||||||||||||||||||||||||||||
| 36 | 0 | $_{$k} = { map { $_ => 1 } split ' ', $_{$k} } | |||||||||||||||||||||||||||
| 37 | 1 | ▶ | unless ref $_{$k}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | delete $_{$k}{$v}; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | else { | ||||||||||||||||||||||||||||
| 42 | 21 | $_{$k} = $v; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 17 | return; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub _generate_mytag { | ||||||||||||||||||||||||||||
| 51 | 177 | ▶ | my ( undef, undef, $arg ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_generate_mytag: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | $arg->{'-as'} ||= $arg->{tag} | ||||||||||||||||||||||||||||
| 54 | 177 | ▶ | or die "mytag needs to be given '-as' or 'name'\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$arg->{"-as"} ||= $arg->{"tag"}
MC/DC0%0/2
$arg->{"-as"} ||= $arg->{"tag"}
$arg->{"-as"}
$arg->{"tag"}
Truth table66%2/3
A ||= B
A= $arg->{"-as"}B= $arg->{"tag"}
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 177 | ▶ | my $tagname = $arg->{tag} || 'div'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg->{'tag'} || 'div'
MC/DC100%1/1
$arg->{'tag'} || 'div'
$arg->{'tag'}
Truth table100%2/2
A
A= $arg->{'tag'}
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | my $groom = sub { | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 17 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 83 | ▶ | if( my $defaults = $arg->{classes} || $arg->{class} ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=12 SCAR=29.2
Branch: 1 branch
Condition66%2/3
$arg->{'classes'} || $arg->{'class'}
MC/DC50%1/2
$arg->{'classes'} || $arg->{'class'}
$arg->{'classes'}
$arg->{'class'}
Truth table66%2/3
A || B
A= $arg->{'classes'}B= $arg->{'class'}
| |||||||||||||||||||||||||||||
| 63 | 0 | $_{class} = { map { $_ => 1 } split ' ', $_{class} } | |||||||||||||||||||||||||||
| 64 | 1 | ▶ | unless ref $_{class}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | if( ref $defaults ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | $_{class}{$_} //= 1 for @$defaults; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_{"class"}{$_} //= 1
MC/DC0%0/1
$_{"class"}{$_} //= 1
$_{"class"}{$_}
Truth table0%0/2
A
A= $_{"class"}{$_}
| |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||
| 69 | 1 | ▶ | $_{class}{$_} //= 1 for split ' ', $defaults; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_{"class"}{$_} //= 1
MC/DC0%0/1
$_{"class"}{$_} //= 1
$_{"class"}{$_}
Truth table50%1/2
A
A= $_{"class"}{$_}
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 83 | ▶ | $_{$_} ||= $arg->{attr}{$_} for eval { keys %{ $arg->{attr} } }; | ||||||||||||||||||||||||||
|
Condition33%1/3
$_{$_} ||= $arg->{"attr"}{$_}
MC/DC0%0/2
$_{$_} ||= $arg->{"attr"}{$_}
$_{$_}
$arg->{"attr"}{$_}
Truth table33%1/3
A ||= B
A= $_{$_}B= $arg->{"attr"}{$_}
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 83 | ▶ | $arg->{groom}->() if $arg->{groom}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 177 | }; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | return sub :prototype(&) { | ||||||||||||||||||||||||||||
| 79 | 83 | ▶ | my $inner = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 80 | 83 | ▶ | render_tag( $tagname, $inner, $groom, $arg->{indent}//1 ); | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg->{"indent"} // 1
MC/DC100%1/1
$arg->{"indent"} // 1
$arg->{"indent"}
Truth table100%2/2
A
A= $arg->{"indent"}
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 177 | } | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | sub render_tag { | ||||||||||||||||||||||||||||
| 86 | 107 | ▶ | my ( $tag, $inner_sub, $groom, $indent ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
render_tag: called CC=11 SCAR=24.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 107 | ▶ | $indent //= 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$indent //= 1
MC/DC100%1/1
$indent //= 1
$indent
Truth table100%2/2
A
A= $indent
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 107 | ▶ | local $Template::Caribou::TAG_INDENT_LEVEL = $indent ? $Template::Caribou::TAG_INDENT_LEVEL : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 107 | ▶ | my $sub = ref $inner_sub eq 'CODE' ? $inner_sub : sub { $inner_sub }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # need to use the object for calls to 'show' | ||||||||||||||||||||||||||||
| 95 | 107 | ▶ | my $bou = $Template::Caribou::TEMPLATE || Moose::Meta::Class->create_anon_class( | ||||||||||||||||||||||||||
|
Condition66%2/3
$Template::Caribou::TEMPLATE || 'Moose::Meta::Class'->create_anon_class('roles', ['Template::Caribou::Role'])->new_object
MC/DC0%0/2
$Template::Caribou::TEMPLATE || 'Moose::Meta::Class'->create_anon_class('roles', ['Template::Caribou::Role'])->new_object
$Template::Caribou::TEMPLATE
'Moose::Meta::Class'->create_anon_class('roles', ['Template::Caribou::Role'])->new_object
Truth table66%2/3
A || B
A= $Template::Caribou::TEMPLATEB= 'Moose::Meta::Class'->create_anon_class('roles', ['Template::Caribou::Role'])->new_object
| |||||||||||||||||||||||||||||
| 96 | roles => [ 'Template::Caribou::Role' ] | ||||||||||||||||||||||||||||
| 97 | )->new_object; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 107 | local %_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 107 | my $inner = do { | |||||||||||||||||||||||||||
| 102 | 107 | local $Template::Caribou::TAG_INDENT_LEVEL = $Template::Caribou::TAG_INDENT_LEVEL; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 107 | ▶ | $Template::Caribou::TAG_INDENT_LEVEL++ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$Template::Caribou::TAG_INDENT_LEVEL // $bou->indent
MC/DC0%0/2
$Template::Caribou::TAG_INDENT_LEVEL // $bou->indent
$Template::Caribou::TAG_INDENT_LEVEL
$bou->indent
Truth table66%2/3
A // B
A= $Template::Caribou::TAG_INDENT_LEVELB= $bou->indent
| |||||||||||||||||||||||||||||
| 105 | if $Template::Caribou::TAG_INDENT_LEVEL // $bou->indent; | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 107 | $bou->get_render($sub); | |||||||||||||||||||||||||||
| 108 | }; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 107 | ▶ | if ( $groom ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 90 | local $_ = "$inner"; # stringification required in case it's an object | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 90 | $groom->(); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 90 | $inner = $_; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | my $attrs = join ' ', '', | ||||||||||||||||||||||||||||
| 119 | 33 | ▶ | pairmap { ( qq{$a="$b"} ) x (length $b > 0) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | map { | ||||||||||||||||||||||||||||
| 121 | $_ => ref $_{$_} | ||||||||||||||||||||||||||||
| 122 | 6 | ▶ | ? join ' ', sort { $a cmp $b } pairmap { $a } pairgrep { $b } $_{$_}->%* | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | 33 | ▶ | : $_{$_} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 107 | } grep { defined $_{$_} } sort keys %_; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 17 | ▶ | no warnings qw/ uninitialized /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 107 | ▶ | my $prefix = !!$Template::Caribou::TAG_INDENT_LEVEL | ||||||||||||||||||||||||||
|
Condition100%2/2
!!$Template::Caribou::TAG_INDENT_LEVEL && "\n" . ' ' x $Template::Caribou::TAG_INDENT_LEVEL
MC/DC100%1/1
!!$Template::Caribou::TAG_INDENT_LEVEL && "\n" . ' ' x $Template::Caribou::TAG_INDENT_LEVEL
!!$Template::Caribou::TAG_INDENT_LEVEL
Truth table100%2/2
A
A= !!$Template::Caribou::TAG_INDENT_LEVEL
| |||||||||||||||||||||||||||||
| 129 | && "\n" . ( ' ' x $Template::Caribou::TAG_INDENT_LEVEL ); | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 107 | ▶ | my $output = length($inner) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | ? Template::Caribou::String->new( "$prefix<${tag}$attrs>$inner$prefix</$tag>" ) | ||||||||||||||||||||||||||||
| 133 | : Template::Caribou::String->new( "$prefix<${tag}$attrs />" ) | ||||||||||||||||||||||||||||
| 134 | ; | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 107 | return print_raw( $output ); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | ▶ | sub print_raw($text) { | |||||||||||||||||||||||||||
|
Subroutine
print_raw: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | 108 | print ::RAW $text; | |||||||||||||||||||||||||||
| 141 | 108 | return $text; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 17 | 1; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =pod | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head1 NAME | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Template::Caribou::Tags - generates tags functions for Caribou templates | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head1 VERSION | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | version 1.1.0 | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | package MyTemplate; | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | use Template::Caribou; | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | use Template::Caribou::Tags | ||||||||||||||||||||||||||||
| 167 | mytag => { | ||||||||||||||||||||||||||||
| 168 | -as => 'foo', | ||||||||||||||||||||||||||||
| 169 | tag => 'p', | ||||||||||||||||||||||||||||
| 170 | class => 'baz' | ||||||||||||||||||||||||||||
| 171 | }; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | template bar => sub { | ||||||||||||||||||||||||||||
| 174 | foo { 'hello' }; | ||||||||||||||||||||||||||||
| 175 | }; | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | # <p class="baz">hello</p> | ||||||||||||||||||||||||||||
| 178 | print __PACKAGE__->new->bar; | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | This module provides the tools to create tag libraries, or ad-hoc tags. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | For pre-defined sets of tags, you may want to look at L<Template::Caribou::Tags::HTML>, | ||||||||||||||||||||||||||||
| 185 | L<Template::Caribou::Tags::HTML::Extended>, and friends. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head2 Core functionality | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Tag functions are created using the C<render_tag> function. For example: | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | package MyTemplate; | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | use Template::Caribou; | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | use Template::Caribou::Tags qw/ render_tag /; | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | sub foo(&) { render_tag( 'foo', shift ) } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # renders as '<foo>hi!</foo>' | ||||||||||||||||||||||||||||
| 200 | template main => sub { | ||||||||||||||||||||||||||||
| 201 | foo { "hi!" }; | ||||||||||||||||||||||||||||
| 202 | }; | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head2 Creating ad-hoc tags | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Defining a function and using C<render_tag> is a little bulky and, typically, will only be used when creating | ||||||||||||||||||||||||||||
| 207 | tag libraries. In most cases, | ||||||||||||||||||||||||||||
| 208 | the C<my_tag> export keyword can be used to create custom tags. For example, the | ||||||||||||||||||||||||||||
| 209 | previous C<foo> definition could have been done this way: | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | package MyTemplate; | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | use Template::Caribou; | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | use Template::Caribou::Tags | ||||||||||||||||||||||||||||
| 216 | mytag => { tag => 'foo' }; | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # renders as '<foo>hi!</foo>' | ||||||||||||||||||||||||||||
| 219 | template main => sub { | ||||||||||||||||||||||||||||
| 220 | foo { | ||||||||||||||||||||||||||||
| 221 | "hi!"; | ||||||||||||||||||||||||||||
| 222 | }; | ||||||||||||||||||||||||||||
| 223 | }; | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | By default, nothing is exported. | ||||||||||||||||||||||||||||
| 228 | The functions C<render_tag> and C<attr> can be exported by this module. | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Custom tag functions can also be defined via the export keyword C<mytag>. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | C<mytag> accepts the following arguments: | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =over | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =item tag => $name | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Tagname that will be used. If not specified, defaults to C<div>. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =item -as => $name | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | Name under which the tag function will be exported. If not specified, defaults to the | ||||||||||||||||||||||||||||
| 243 | value of the C<tag> argument. At least one of C<-as> or C<tag> must be given explicitly. | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =item groom => sub { } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | Grooming function for the tag block. See C<render_tag> for more details. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =item classes => \@classes | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Default value for the 'class' attribute of the tag. | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | use Template::Caribou::Tags | ||||||||||||||||||||||||||||
| 254 | # <div class="main">...</div> | ||||||||||||||||||||||||||||
| 255 | mytag => { -as => 'main_div', classes => [ 'main' ] }; | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | If you want to remove a default class from the tag, | ||||||||||||||||||||||||||||
| 258 | set its value to C<0> in C<%_>. E.g., | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | main_div { $_{class}{main} = 0; ... }; | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =item attr => \%attributes | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | Default set of attributes for the tag. | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | use Template::Caribou::Tags | ||||||||||||||||||||||||||||
| 267 | # <input disabled="disabled">...</input> | ||||||||||||||||||||||||||||
| 268 | mytag => { -as => 'disabled_input', tag => 'input', attr => { disabled => 'disabled' } }; | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =back | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | =function attr( $name => $value ) | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | I recommend you use C<%_> directly instead. | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | Accesses the attributes of a tag within its block. | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | If provided an even number of parameters, sets the attributes to those values. | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | div { | ||||||||||||||||||||||||||||
| 281 | attr class => 'foo', | ||||||||||||||||||||||||||||
| 282 | style => 'text-align: center'; | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | "hi there"; | ||||||||||||||||||||||||||||
| 285 | }; | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | # <div class="foo" style="text-align: center">hi there</div> | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | Many calls to C<attr> can be done within the same block. | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | div { | ||||||||||||||||||||||||||||
| 292 | attr class => 'foo'; | ||||||||||||||||||||||||||||
| 293 | attr style => 'text-align: center'; | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | "hi there"; | ||||||||||||||||||||||||||||
| 296 | }; | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | # <div class="foo" style="text-align: center">hi there</div> | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | To add/remove to an attribute instead of replacing its value, prefix the attribute name | ||||||||||||||||||||||||||||
| 301 | with a plus or minus sign. Doing either will automatically | ||||||||||||||||||||||||||||
| 302 | turn the value in C<%_> to a hashref. | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | div { | ||||||||||||||||||||||||||||
| 305 | attr class => 'foo baz'; | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | attr '+class' => 'bar'; | ||||||||||||||||||||||||||||
| 308 | attr '-class' => 'baz'; | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | "hi there"; | ||||||||||||||||||||||||||||
| 311 | }; | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | # <div class="foo bar">hi there</div> | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | The value of an attribute can also be queried by passing a single argument to C<attr>. | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | div { | ||||||||||||||||||||||||||||
| 318 | ...; # some complex stuff here | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | my $class = attr 'class'; | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | attr '+style' => 'text-align: center' if $class =~ /_centered/; | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | ...; | ||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =function render_tag( $tag_name, $inner_block, \&groom, $indent ) | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | Prints out a tag in a template. The C<$inner_block> is a string or coderef | ||||||||||||||||||||||||||||
| 330 | holding the content of the tag. | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | If the C<$inner_block> is empty, the tag will be of the form | ||||||||||||||||||||||||||||
| 333 | C<< <foo /> >>. | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | render_tag( 'div', 'hello' ); # <div>hello</div> | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | render_tag( 'div', sub { 'hello' } ) # <div>hello</div> | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | render_tag( 'div', '' ); # <div /> | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | An optional grooming function can be passed. If it is, an hash holding the | ||||||||||||||||||||||||||||
| 342 | attributes of the tag, and its inner content will be passed to it as C<%_> and C<$_>, respectively. | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | # '<div>the current time is Wed Nov 25 13:18:33 2015</div>' | ||||||||||||||||||||||||||||
| 345 | render_tag( 'div', 'the current time is DATETIME', sub { | ||||||||||||||||||||||||||||
| 346 | s/DATETIME/scalar localtime/eg; | ||||||||||||||||||||||||||||
| 347 | }); | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | # '<div class="mine">foo</div>' | ||||||||||||||||||||||||||||
| 350 | render_tag( 'div', 'foo', sub { $_{class} = 'mine' } ) | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | An optional C<$indent> argument can also be given. If explicitly set to | ||||||||||||||||||||||||||||
| 353 | C<false>, the tag won't be indented even when the template | ||||||||||||||||||||||||||||
| 354 | is in pretty-print mode. Used for tags where whitespaces | ||||||||||||||||||||||||||||
| 355 | are significant or would alter | ||||||||||||||||||||||||||||
| 356 | the presentation (e.g., C<pre> or C<emphasis>). Defaults to C<true>. | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | Yanick Champoux <yanick@cpan.org> | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | This software is copyright (c) 2017 by Yanick Champoux. | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 367 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =cut | ||||||||||||||||||||||||||||