| 1 | package Template::Flute; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Module::Runtime qw/use_module/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Scalar::Util qw/blessed/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Template::Flute::Utils; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Template::Flute::Specification::XML; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use Template::Flute::HTML; | ||
| 12 | use Template::Flute::Iterator; | ||
| 13 | use Template::Flute::Iterator::Cache; | ||
| 14 | use Template::Flute::Increment; | ||
| 15 | use Template::Flute::Pager; | ||
| 16 | use Template::Flute::Paginator; | ||
| 17 | use Template::Flute::Types -types; | ||
| 18 | |||
| 19 | use Moo; | ||
| 20 | use namespace::clean; | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | Template::Flute - Modern designer-friendly HTML templating Engine | ||
| 25 | |||
| 26 | =head1 VERSION | ||
| 27 | |||
| 28 | Version 0.026 | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | our $VERSION = '0.026'; | ||
| 33 | |||
| 34 | =head1 SYNOPSIS | ||
| 35 | |||
| 36 | use Template::Flute; | ||
| 37 | |||
| 38 | my ($cart, $flute, %values); | ||
| 39 | |||
| 40 | $cart = [{...},{...}]; | ||
| 41 | $values{cost} = ... | ||
| 42 | |||
| 43 | $flute = new Template::Flute(specification_file => 'cart.xml', | ||
| 44 | template_file => 'cart.html', | ||
| 45 | iterators => {cart => $cart}, | ||
| 46 | values => \%values, | ||
| 47 | autodetect => { | ||
| 48 | disable => [qw/Foo::Bar/], | ||
| 49 | } | ||
| 50 | ); | ||
| 51 | |||
| 52 | print $flute->process(); | ||
| 53 | |||
| 54 | =head1 DESCRIPTION | ||
| 55 | |||
| 56 | Template::Flute enables you to completely separate web design and programming | ||
| 57 | tasks for dynamic web applications. | ||
| 58 | |||
| 59 | Templates are designed to be designer-friendly; there's no inline code or mini | ||
| 60 | templating language for your designers to learn - instead, standard HTML and CSS | ||
| 61 | classes are used, leading to HTML that can easily be understood and edited by | ||
| 62 | WYSIWYG editors and hand-coding designers alike. | ||
| 63 | |||
| 64 | An example is easier than a wordy description: | ||
| 65 | |||
| 66 | Given the following template snippet: | ||
| 67 | |||
| 68 | <div class="customer_name">Mr A Test</div> | ||
| 69 | <div class="customer_email">someone@example.com</div> | ||
| 70 | |||
| 71 | and the following specification: | ||
| 72 | |||
| 73 | <specification name="example" description="Example"> | ||
| 74 | <value name="customer_name" /> | ||
| 75 | <value name="email" class="customer_email" /> | ||
| 76 | </specification> | ||
| 77 | |||
| 78 | Processing the above as follows: | ||
| 79 | |||
| 80 | $flute = Template::Flute->new( | ||
| 81 | template_file => 'template.html', | ||
| 82 | specification_file => 'spec.xml', | ||
| 83 | ); | ||
| 84 | $flute->set_values({ | ||
| 85 | customer_name => 'Bob McTest', | ||
| 86 | email => 'bob@example.com', | ||
| 87 | });; | ||
| 88 | print $flute->process; | ||
| 89 | |||
| 90 | The resulting output would be: | ||
| 91 | |||
| 92 | <div class="customer_name">Bob McTest</div> | ||
| 93 | <div class="email">bob@example.com</div> | ||
| 94 | |||
| 95 | |||
| 96 | In other words, rather than including a templating language within your | ||
| 97 | templates which your designers must master and which could interfere with | ||
| 98 | previews in WYSIWYG tools, CSS selectors in the template are tied to your | ||
| 99 | data structures or objects by a specification provided by the programmer. | ||
| 100 | |||
| 101 | |||
| 102 | =head2 Workflow | ||
| 103 | |||
| 104 | The easiest way to use Template::Flute is to pass all necessary parameters to | ||
| 105 | the constructor and call the process method to generate the HTML. | ||
| 106 | |||
| 107 | You can also break it down in separate steps: | ||
| 108 | |||
| 109 | =over 4 | ||
| 110 | |||
| 111 | =item 1. Parse specification | ||
| 112 | |||
| 113 | Parse specification based on your specification format (e.g with | ||
| 114 | L<Template::Flute::Specification::XML> or L<Template::Flute::Specification::Scoped>.). | ||
| 115 | |||
| 116 | $xml_spec = new Template::Flute::Specification::XML; | ||
| 117 | $spec = $xml_spec->parse(q{<specification name="cart" description="Cart"> | ||
| 118 | <list name="cart" class="cartitem" iterator="cart"> | ||
| 119 | <param name="name" field="title"/> | ||
| 120 | <param name="quantity"/> | ||
| 121 | <param name="price"/> | ||
| 122 | </list> | ||
| 123 | <value name="cost"/> | ||
| 124 | </specification>}); | ||
| 125 | |||
| 126 | =item 2. Parse template | ||
| 127 | |||
| 128 | Parse template with L<Template::Flute::HTML> object. | ||
| 129 | |||
| 130 | $template = new Template::Flute::HTML; | ||
| 131 | $template->parse(q{<html> | ||
| 132 | <head> | ||
| 133 | <title>Cart Example</title> | ||
| 134 | </head> | ||
| 135 | <body> | ||
| 136 | <table class="cart"> | ||
| 137 | <tr class="cartheader"> | ||
| 138 | <th>Name</th> | ||
| 139 | <th>Quantity</th> | ||
| 140 | <th>Price</th> | ||
| 141 | </tr> | ||
| 142 | <tr class="cartitem"> | ||
| 143 | <td class="name">Sample Book</td> | ||
| 144 | <td><input class="quantity" name="quantity" size="3" value="10"></td> | ||
| 145 | <td class="price">$1</td> | ||
| 146 | </tr> | ||
| 147 | <tr class="cartheader"><th colspan="2"></th><th>Total</th> | ||
| 148 | </tr> | ||
| 149 | <tr> | ||
| 150 | <td colspan="2"></td><td class="cost">$10</td> | ||
| 151 | </tr> | ||
| 152 | </table> | ||
| 153 | </body></html>}, | ||
| 154 | $spec); | ||
| 155 | |||
| 156 | =item 3. Produce HTML output | ||
| 157 | |||
| 158 | $flute = new Template::Flute(template => $template, | ||
| 159 | iterators => {cart => $cart}, | ||
| 160 | values => {cost => '84.94'}); | ||
| 161 | $flute->process(); | ||
| 162 | |||
| 163 | =back | ||
| 164 | |||
| 165 | =head1 CONSTRUCTOR | ||
| 166 | |||
| 167 | =head2 new | ||
| 168 | |||
| 169 | Create a Template::Flute object with the following parameters: | ||
| 170 | |||
| 171 | =over 4 | ||
| 172 | |||
| 173 | =item specification_file | ||
| 174 | |||
| 175 | Specification file name. | ||
| 176 | |||
| 177 | =item specification_parser | ||
| 178 | |||
| 179 | Select specification parser. This can be either the full class name | ||
| 180 | like C<MyApp::Specification::Parser> or the last part for classes residing | ||
| 181 | in the Template::Flute::Specification namespace. | ||
| 182 | |||
| 183 | =item specification | ||
| 184 | |||
| 185 | Specification object or specification as string. | ||
| 186 | |||
| 187 | =item template_file | ||
| 188 | |||
| 189 | HTML template file. | ||
| 190 | |||
| 191 | =item template | ||
| 192 | |||
| 193 | L<Template::Flute::HTML> object or template as string. | ||
| 194 | |||
| 195 | =item filters | ||
| 196 | |||
| 197 | Hash reference of filter functions. | ||
| 198 | |||
| 199 | =item i18n | ||
| 200 | |||
| 201 | L<Template::Flute::I18N> object. | ||
| 202 | |||
| 203 | =item translate_attributes | ||
| 204 | |||
| 205 | An arrayref of attribute names to translate. If the name has a dot, it | ||
| 206 | is interpreted as tagname + attribute, so C<placeholder>" will | ||
| 207 | unconditionally translate all the placeholders, while | ||
| 208 | C<input.placeholder> only the placeholder found on the input tag. | ||
| 209 | |||
| 210 | Additional dotted values compose conditions for attributes. E.g. | ||
| 211 | C<input.value.type.submit> means all the value attributes with | ||
| 212 | attribute C<type> set to C<submit>. | ||
| 213 | |||
| 214 | Defaults to C<['input.value.type.submit', 'placeholder']> | ||
| 215 | |||
| 216 | =item iterators | ||
| 217 | |||
| 218 | Hash references of iterators. | ||
| 219 | |||
| 220 | =item values | ||
| 221 | |||
| 222 | Hash reference of values to be used by the process method. | ||
| 223 | |||
| 224 | =item auto_iterators | ||
| 225 | |||
| 226 | Builds iterators automatically from values. | ||
| 227 | |||
| 228 | =item autodetect | ||
| 229 | |||
| 230 | A configuration option. It should be an hashref with a key C<disable> | ||
| 231 | and a value with an arrayref with a list of B<classes> for objects | ||
| 232 | which should be considered plain hashrefs instead. Example: | ||
| 233 | |||
| 234 | my $flute = Template::Flute->new(.... | ||
| 235 | autodetect => { disable => [qw/My::Object/] }, | ||
| 236 | .... | ||
| 237 | ); | ||
| 238 | |||
| 239 | Doing so, if you pass a value holding a C<My::Object> object, and you have a specification with something like this: | ||
| 240 | |||
| 241 | <specification> | ||
| 242 | <value name="name" field="object.method"/> | ||
| 243 | </specification> | ||
| 244 | |||
| 245 | The value will be C<$object->{method}>, not C<$object->$method>. | ||
| 246 | |||
| 247 | The object is checked with C<isa>. | ||
| 248 | |||
| 249 | Classical example: C<Dancer::Session::Abstract>. | ||
| 250 | |||
| 251 | =item uri | ||
| 252 | |||
| 253 | Base URI for your template. This adjusts the links in the HTML tags | ||
| 254 | C<a>, C<base>, C<img>, C<link> and C<script>. | ||
| 255 | |||
| 256 | =item email_cids | ||
| 257 | |||
| 258 | This is meant to be used on HTML emails. When this is set to an hash | ||
| 259 | reference (which should be empty), the hash will be populated with the | ||
| 260 | following values: | ||
| 261 | |||
| 262 | cid1 => { filename => 'foo.png' }, | ||
| 263 | cid2 => { filename => 'foo2.gif' }, | ||
| 264 | |||
| 265 | and in the body the images C<src> attribute will be replaced with | ||
| 266 | C<cid:cid1>. | ||
| 267 | |||
| 268 | The cid names are arbitrary and assigned by the template. The code | ||
| 269 | should look at the reference values which were modified. | ||
| 270 | |||
| 271 | =item cids | ||
| 272 | |||
| 273 | Optional hashref with options for the CID replacement behaviour. | ||
| 274 | |||
| 275 | By default, if the source looks like an HTTP/HTTPS URI, the image | ||
| 276 | source is not altered and no CID is assigned. | ||
| 277 | |||
| 278 | If you pass a C<base_url> value in this hashref, the URI matching it | ||
| 279 | will be converted to cids and the rest of the path will be added to | ||
| 280 | the C<email_cids> hashref. | ||
| 281 | |||
| 282 | Example: | ||
| 283 | |||
| 284 | my $cids = {}; | ||
| 285 | $flute = Template::Flute->new(template => $template, | ||
| 286 | specification => $spec, | ||
| 287 | email_cids => $cids, | ||
| 288 | cids => { | ||
| 289 | base_url => 'http://example.com/' | ||
| 290 | }); | ||
| 291 | |||
| 292 | Say the template contains images with source | ||
| 293 | C<http://example.com/image.png>, the C<email_cids> hashref will | ||
| 294 | contain a cid with C<filename> "image.png". | ||
| 295 | |||
| 296 | =back | ||
| 297 | |||
| 298 | =cut | ||
| 299 | |||
| 300 | # Constructor | ||
| 301 | |||
| 302 | has autodetect => ( | ||
| 303 | is => 'ro', | ||
| 304 | isa => HashRef, | ||
| 305 | ); | ||
| 306 | |||
| 307 | has auto_iterators => ( | ||
| 308 | is => 'ro', | ||
| 309 | isa => Bool, | ||
| 310 | default => 0, | ||
| 311 | ); | ||
| 312 | |||
| 313 | has cids => ( | ||
| 314 | is => 'ro', | ||
| 315 | isa => HashRef, | ||
| 316 | ); | ||
| 317 | |||
| 318 | has email_cids => ( | ||
| 319 | is => 'ro', | ||
| 320 | isa => HashRef, | ||
| 321 | ); | ||
| 322 | |||
| 323 | has filters => ( | ||
| 324 | is => 'ro', | ||
| 325 | isa => HashRef, | ||
| 326 | default => sub { +{} }, | ||
| 327 | ); | ||
| 328 | |||
| 329 | has _filter_class => ( | ||
| 330 | is => 'ro', | ||
| 331 | isa => HashRef, | ||
| 332 | default => sub { +{} }, | ||
| 333 | ); | ||
| 334 | |||
| 335 | has _filter_objects => ( | ||
| 336 | is => 'ro', | ||
| 337 | isa => HashRef, | ||
| 338 | default => sub { +{} }, | ||
| 339 | ); | ||
| 340 | |||
| 341 | has _filter_opts => ( | ||
| 342 | is => 'ro', | ||
| 343 | isa => HashRef, | ||
| 344 | default => sub { +{} }, | ||
| 345 | ); | ||
| 346 | |||
| 347 | has _filter_subs => ( | ||
| 348 | is => 'ro', | ||
| 349 | isa => HashRef, | ||
| 350 | default => sub { +{} }, | ||
| 351 | ); | ||
| 352 | |||
| 353 | # FIXME: (SysPete 28/4/16) find & fix code that passes in i18n as undef | ||
| 354 | has i18n => ( | ||
| 355 | is => 'ro', | ||
| 356 | isa => Maybe[InstanceOf ['Template::Flute::I18N']], | ||
| 357 | ); | ||
| 358 | |||
| 359 | has iterators => ( | ||
| 360 | is => 'ro', | ||
| 361 | isa => HashRef, | ||
| 362 | lazy => 1, | ||
| 363 | default => sub { +{} }, | ||
| 364 | ); | ||
| 365 | |||
| 366 | has patterns => ( | ||
| 367 | is => 'ro', | ||
| 368 | isa => HashRef, | ||
| 369 | default => sub { +{} }, | ||
| 370 | init_arg => undef, | ||
| 371 | ); | ||
| 372 | |||
| 373 | has scopes => ( | ||
| 374 | is => 'ro', | ||
| 375 | isa => Bool, | ||
| 376 | default => 0, | ||
| 377 | ); | ||
| 378 | |||
| 379 | has _specification => ( | ||
| 380 | is => 'ro', | ||
| 381 | isa => Specification | Str, | ||
| 382 | init_arg => 'specification', | ||
| 383 | ); | ||
| 384 | |||
| 385 | # FIXME: (SysPete 28/4/16) Due to GH#54 (see below) we need a writer which | ||
| 386 | # we'll make private | ||
| 387 | has specification => ( | ||
| 388 | is => 'rwp', | ||
| 389 | isa => Specification, | ||
| 390 | lazy => 1, | ||
| 391 | init_arg => undef, | ||
| 392 | default => sub { | ||
| 393 | my $self = shift; | ||
| 394 | my $ret; | ||
| 395 | |||
| 396 | if ( ref( $self->_specification ) ) { | ||
| 397 | $ret = $self->_specification; | ||
| 398 | } | ||
| 399 | else { | ||
| 400 | my $parser_spec = use_module( $self->specification_parser )->new; | ||
| 401 | |||
| 402 | if ( $self->_specification ) { | ||
| 403 | $ret = $parser_spec->parse( $self->_specification ); | ||
| 404 | } | ||
| 405 | else { | ||
| 406 | $ret = $parser_spec->parse_file( $self->specification_file ); | ||
| 407 | } | ||
| 408 | |||
| 409 | croak "Error parsing specification: ", $parser_spec->error | ||
| 410 | unless $ret; | ||
| 411 | } | ||
| 412 | |||
| 413 | # copy iterators into specification | ||
| 414 | while ( my ( $name, $iter ) = each %{ $self->iterators } ) { | ||
| 415 | $ret->set_iterator( $name, $iter ); | ||
| 416 | } | ||
| 417 | |||
| 418 | # copy patterns from specification | ||
| 419 | if ( my %patterns = $ret->patterns ) { | ||
| 420 | foreach my $k ( keys %patterns ) { | ||
| 421 | $self->_set_pattern( $k, $patterns{$k} ); | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | return $ret; | ||
| 426 | }, | ||
| 427 | ); | ||
| 428 | |||
| 429 | has specification_file => ( | ||
| 430 | is => 'ro', | ||
| 431 | isa => ReadableFilePath, | ||
| 432 | lazy => 1, | ||
| 433 | default => sub { | ||
| 434 | my $self = shift; | ||
| 435 | |||
| 436 | croak "No template_file supplied so cannot determine specification_file" | ||
| 437 | unless $self->template_file; | ||
| 438 | |||
| 439 | Template::Flute::Utils::derive_filename( $self->template_file, '.xml' ); | ||
| 440 | }, | ||
| 441 | ); | ||
| 442 | |||
| 443 | has specification_parser => ( | ||
| 444 | is => 'ro', | ||
| 445 | isa => Str, | ||
| 446 | default => 'Template::Flute::Specification::XML', | ||
| 447 | coerce => sub { | ||
| 448 | $_[0] =~ /::/ ? $_[0] : "Template::Flute::Specification::$_[0]"; | ||
| 449 | }, | ||
| 450 | ); | ||
| 451 | |||
| 452 | has _template => ( | ||
| 453 | is => 'ro', | ||
| 454 | isa => HtmlParser | Str, | ||
| 455 | init_arg => 'template', | ||
| 456 | ); | ||
| 457 | |||
| 458 | has template => ( | ||
| 459 | is => 'ro', | ||
| 460 | isa => HtmlParser, | ||
| 461 | lazy => 1, | ||
| 462 | init_arg => undef, | ||
| 463 | default => sub { | ||
| 464 | my $self = shift; | ||
| 465 | |||
| 466 | return $self->_template if ref( $self->_template ); | ||
| 467 | |||
| 468 | my $template_object = Template::Flute::HTML->new(uri => $self->uri); | ||
| 469 | |||
| 470 | if ( $self->_template ) { | ||
| 471 | $template_object->parse( | ||
| 472 | $self->_template, $self->specification | ||
| 473 | ); | ||
| 474 | } | ||
| 475 | else { | ||
| 476 | $template_object->parse_file( | ||
| 477 | $self->template_file, $self->specification | ||
| 478 | ); | ||
| 479 | } | ||
| 480 | |||
| 481 | return $template_object; | ||
| 482 | }, | ||
| 483 | ); | ||
| 484 | |||
| 485 | has template_file => ( | ||
| 486 | is => 'ro', | ||
| 487 | isa => Str, | ||
| 488 | ); | ||
| 489 | |||
| 490 | has translate_attributes => ( | ||
| 491 | is => 'ro', | ||
| 492 | isa => ArrayRef, | ||
| 493 | default => sub { [ 'placeholder', 'input.value.type.submit' ] }, | ||
| 494 | ); | ||
| 495 | |||
| 496 | # FIXME: (SysPete 28/4/16) find & fix code that passes in uri as undef | ||
| 497 | # FIXME: (SysPete 28/4/16) perhaps stringify in coerce so we don't have | ||
| 498 | # to accept URI objects? | ||
| 499 | has uri => ( | ||
| 500 | is => 'ro', | ||
| 501 | isa => Maybe [ InstanceOf ['URI'] | Str ], | ||
| 502 | ); | ||
| 503 | |||
| 504 | has values => ( | ||
| 505 | is => 'ro', | ||
| 506 | isa => HashRef, | ||
| 507 | writer => 'set_values', | ||
| 508 | default => sub { +{} }, | ||
| 509 | ); | ||
| 510 | |||
| 511 | sub BUILDARGS { | ||
| 512 | my $class = shift; | ||
| 513 | my %args = @_ == 1 && ref($_[0]) eq 'HASH' ? %{ $_[0] } : @_; | ||
| 514 | |||
| 515 | croak "Either 'template' or 'template_file' must be supplied" | ||
| 516 | unless ( exists $args{template} || exists $args{template_file} ); | ||
| 517 | |||
| 518 | # build the various _filter_* attributes | ||
| 519 | while ( my ( $name, $value ) = each %{ $args{filters} } ) { | ||
| 520 | if ( ref($value) eq 'CODE' ) { | ||
| 521 | # passing subroutine | ||
| 522 | $args{_filter_subs}->{$name} = $value; | ||
| 523 | next; | ||
| 524 | } | ||
| 525 | if ( exists( $value->{class} ) ) { | ||
| 526 | # record filter class | ||
| 527 | $args{_filter_class}->{$name} = $value->{class}; | ||
| 528 | } | ||
| 529 | if ( exists( $value->{options} ) ) { | ||
| 530 | # record filter options | ||
| 531 | $args{_filter_opts}->{$name} = $value->{options}; | ||
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 535 | return \%args; | ||
| 536 | } | ||
| 537 | |||
| 538 | sub BUILD { | ||
| 539 | my $self = shift; | ||
| 540 | # catch exception in lazy builders early by forcing template to build now | ||
| 541 | $self->template; | ||
| 542 | } | ||
| 543 | |||
| 544 | sub _get_pattern { | ||
| 545 | my ($self, $name) = @_; | ||
| 546 | return $self->patterns->{$name}; | ||
| 547 | } | ||
| 548 | |||
| 549 | sub _set_pattern { | ||
| 550 | my ($self, $name, $regexp) = @_; | ||
| 551 | croak "Missing pattern name" unless $name; | ||
| 552 | croak "pattern $name already exists!" if $self->patterns->{$name}; | ||
| 553 | croak "Missing pattern regexp for $name" unless $regexp; | ||
| 554 | $self->patterns->{$name} = $regexp; | ||
| 555 | } | ||
| 556 | |||
| 557 | =head1 METHODS | ||
| 558 | |||
| 559 | =head2 BUILD | ||
| 560 | |||
| 561 | Force creation of template class as soon as object is instantiated. | ||
| 562 | |||
| 563 | =head2 process [HASHREF] | ||
| 564 | |||
| 565 | Processes HTML template, manipulates the HTML tree based on the | ||
| 566 | specification, values and iterators. | ||
| 567 | |||
| 568 | Returns HTML output. | ||
| 569 | |||
| 570 | =cut | ||
| 571 | |||
| 572 | sub process { | ||
| 573 | my ($self, $params) = @_; | ||
| 574 | |||
| 575 | if ($self->i18n) { | ||
| 576 | # translate static text first | ||
| 577 | $self->template->translate($self->i18n, @{$self->translate_attributes}); | ||
| 578 | } | ||
| 579 | |||
| 580 | my $html = $self->_sub_process( | ||
| 581 | $self->template->{xml}, | ||
| 582 | $self->specification->{xml}->root, | ||
| 583 | $self->values, | ||
| 584 | $self->specification, | ||
| 585 | $self->template, | ||
| 586 | 0, | ||
| 587 | 0, | ||
| 588 | ); | ||
| 589 | |||
| 590 | if ($self->email_cids) { | ||
| 591 | $self->_cidify_html($html); | ||
| 592 | } | ||
| 593 | my $shtml = $html->sprint; | ||
| 594 | return $shtml; | ||
| 595 | } | ||
| 596 | |||
| 597 | sub _cidify_html { | ||
| 598 | my ($self, $html) = @_; | ||
| 599 | |||
| 600 | my %options; | ||
| 601 | if ($self->cids) { | ||
| 602 | %options = %{ $self->cids }; | ||
| 603 | } | ||
| 604 | |||
| 605 | foreach my $img ($html->descendants('img')) { | ||
| 606 | my $source = $img->att('src'); | ||
| 607 | |||
| 608 | if (defined $source && $source =~ /\S/ && $source !~ /^cid:/) { | ||
| 609 | my $cid = $source; | ||
| 610 | # to generate a cid, remove every character save for [a-zA-Z0-9] | ||
| 611 | # and use that. | ||
| 612 | $cid =~ s/[^0-9A-Za-z]//g; | ||
| 613 | next unless $cid; | ||
| 614 | |||
| 615 | # before processing, check what we have in the src | ||
| 616 | # url: | ||
| 617 | my $filename; | ||
| 618 | if ($source =~ m!https?://!) { | ||
| 619 | if (my $base = $options{base_url}) { | ||
| 620 | if ($source =~ m/^\s*\Q$base\E(.+?)\s*$/s) { | ||
| 621 | $filename = $1; | ||
| 622 | } | ||
| 623 | } | ||
| 624 | } | ||
| 625 | else { | ||
| 626 | $filename = $source; | ||
| 627 | } | ||
| 628 | |||
| 629 | # found? cidify the source | ||
| 630 | if ($filename) { | ||
| 631 | $img->set_att(src => "cid:$cid"); | ||
| 632 | $self->email_cids->{$cid} = { filename => $filename }; | ||
| 633 | } | ||
| 634 | } | ||
| 635 | } | ||
| 636 | } | ||
| 637 | |||
| 638 | sub _sub_process { | ||
| 639 | my ($self, $html, $spec_xml, $values, $spec, $root_template, $count, $level) = @_; | ||
| 640 | my ($template, %list_active); | ||
| 641 | # Use root spec or sub-spec | ||
| 642 | my $specification = $spec | ||
| 643 | || use_module( $self->specification_parser )->new() | ||
| 644 | ->parse( "<specification>" . $spec_xml->sprint . "</specification>" ); | ||
| 645 | |||
| 646 | if($root_template){ | ||
| 647 | $template = $root_template; | ||
| 648 | } | ||
| 649 | else { | ||
| 650 | $template = Template::Flute::HTML->new();; | ||
| 651 | $template->parse("<flutexml>".$html->sprint."</flutexml>", $specification, 1); | ||
| 652 | } | ||
| 653 | |||
| 654 | my $classes = $specification->{classes}; | ||
| 655 | my ($dbobj, $iter, $sth, $row, $lel, $query, %skip, %iter_names); | ||
| 656 | |||
| 657 | # Read one layer of spec | ||
| 658 | my $spec_elements = {}; | ||
| 659 | for my $elt ( $spec_xml->descendants() ){ | ||
| 660 | my $type = $elt->tag; | ||
| 661 | $spec_elements->{$type} ||= []; | ||
| 662 | |||
| 663 | # check whether to skip sublists on this level | ||
| 664 | if ($type eq 'list') { | ||
| 665 | if ($elt->parent->tag eq 'list' | ||
| 666 | && $elt->parent ne $spec_xml) { | ||
| 667 | $skip{$elt} = 1; | ||
| 668 | } | ||
| 669 | else { | ||
| 670 | push @{$iter_names{$elt->att('iterator')}}, $elt; | ||
| 671 | } | ||
| 672 | } | ||
| 673 | push @{$spec_elements->{$type}}, $elt; | ||
| 674 | |||
| 675 | } | ||
| 676 | |||
| 677 | while (my ($name, $value) = each %iter_names) { | ||
| 678 | next if $name =~ /\./; | ||
| 679 | |||
| 680 | if (@$value > 1) { | ||
| 681 | my $iter_cached = Template::Flute::Iterator::Cache->new( | ||
| 682 | iterator => $specification->iterator($name), | ||
| 683 | ); | ||
| 684 | $specification->set_iterator($name, $iter_cached); | ||
| 685 | }; | ||
| 686 | } | ||
| 687 | |||
| 688 | my $cut_container = 0; | ||
| 689 | |||
| 690 | # cut the elts in the template, *before* processing the lists | ||
| 691 | if ($level == 0) { | ||
| 692 | for my $container ($template->containers()) { | ||
| 693 | next if $container->list; | ||
| 694 | |||
| 695 | $container->set_values($values) if $values; | ||
| 696 | unless ($container->visible()) { | ||
| 697 | for my $elt (@{$container->elts()}) { | ||
| 698 | $elt->cut(); | ||
| 699 | } | ||
| 700 | } | ||
| 701 | } | ||
| 702 | } | ||
| 703 | elsif ($spec_xml->gi eq 'list') { | ||
| 704 | # we check whether the container is a child of this list | ||
| 705 | for my $container ($template->containers()) { | ||
| 706 | next if $container->list ne $spec_xml->att('name'); | ||
| 707 | |||
| 708 | $container->set_values($values) if $values; | ||
| 709 | unless ($container->visible()) { | ||
| 710 | $cut_container = 1; | ||
| 711 | for my $elt (@{$container->elts()}) { | ||
| 712 | $elt->cut(); | ||
| 713 | } | ||
| 714 | } | ||
| 715 | } | ||
| 716 | } | ||
| 717 | |||
| 718 | # List | ||
| 719 | for my $elt ( @{$spec_elements->{list}} ) { | ||
| 720 | next if exists $skip{$elt}; | ||
| 721 | |||
| 722 | my $spec_name = $elt->{'att'}->{'name'}; | ||
| 723 | my $spec_class = $elt->{'att'}->{'class'} ? $elt->{'att'}->{'class'} : $spec_name; | ||
| 724 | my $sep_copy; | ||
| 725 | my $iterator = $elt->{'att'}->{'iterator'} || ''; | ||
| 726 | my $sub_spec = $elt->copy(); | ||
| 727 | my $element_template = $classes->{$spec_class}->[0]->{elts}->[0]; | ||
| 728 | |||
| 729 | unless($element_template){ | ||
| 730 | next; | ||
| 731 | } | ||
| 732 | |||
| 733 | # collect the list of classes to see if the separator is | ||
| 734 | # inside or outside the list element | ||
| 735 | my %children_classes; | ||
| 736 | foreach my $child_element ($element_template->children) { | ||
| 737 | if (my $c = $child_element->att('class')) { | ||
| 738 | $children_classes{$c} = 1; | ||
| 739 | } | ||
| 740 | } | ||
| 741 | |||
| 742 | # determine where to paste | ||
| 743 | my ($list_paste_to, $paste_operation); | ||
| 744 | # if last child, append | ||
| 745 | if ($element_template->is_last_child) { | ||
| 746 | $list_paste_to = $element_template->parent; | ||
| 747 | $paste_operation = 'last_child'; | ||
| 748 | } | ||
| 749 | # if there is material, before the template element (we will cut that later) | ||
| 750 | elsif ($element_template->next_sibling) { | ||
| 751 | $list_paste_to = $element_template; | ||
| 752 | $paste_operation = 'before'; | ||
| 753 | } | ||
| 754 | else { | ||
| 755 | # list is root element in the template | ||
| 756 | $list_paste_to = $html; | ||
| 757 | $paste_operation = 'last_child'; | ||
| 758 | } | ||
| 759 | |||
| 760 | my @iter_steps = split(/\./, $iterator); | ||
| 761 | my $iter_ref = $values; | ||
| 762 | my $records; | ||
| 763 | my $spec_iter = $specification->iterator($iterator); | ||
| 764 | |||
| 765 | if ($spec_iter) { | ||
| 766 | $iter_ref->{$iterator} = $spec_iter; | ||
| 767 | } | ||
| 768 | |||
| 769 | for my $step (@iter_steps) { | ||
| 770 | if (defined blessed $iter_ref) { | ||
| 771 | $records = $iter_ref->$step; | ||
| 772 | $iter_ref = {}; | ||
| 773 | } | ||
| 774 | elsif (ref($iter_ref->{$step})) { | ||
| 775 | $records = $iter_ref->{$step}; | ||
| 776 | $iter_ref = $iter_ref->{$step}; | ||
| 777 | } | ||
| 778 | else { | ||
| 779 | $records = $iter_ref->{$step}; | ||
| 780 | $iter_ref = {}; | ||
| 781 | } | ||
| 782 | } | ||
| 783 | |||
| 784 | my $list = $template->{lists}->{$spec_name}; | ||
| 785 | my $count = 1; | ||
| 786 | my $iter_records; | ||
| 787 | |||
| 788 | if (defined blessed $records) { | ||
| 789 | # check whether this object can serve as iterator | ||
| 790 | if ($records->can('next') && $records->can('count')) { | ||
| 791 | $iter_records = $records; | ||
| 792 | } | ||
| 793 | else { | ||
| 794 | croak "Object cannot be used as iterator for list $spec_name: ", ref($records); | ||
| 795 | } | ||
| 796 | } | ||
| 797 | else { | ||
| 798 | $iter_records = Template::Flute::Iterator->new(@$records); | ||
| 799 | } | ||
| 800 | |||
| 801 | if ($list->{paging}) { | ||
| 802 | $iter_records->reset; | ||
| 803 | # replace the iterator with the paginator | ||
| 804 | $iter_records = $self->_paging($list, $iter_records); | ||
| 805 | } | ||
| 806 | |||
| 807 | |||
| 808 | |||
| 809 | if ($iter_records->count) { | ||
| 810 | $list_active{$spec_name} = 1; | ||
| 811 | } | ||
| 812 | else { | ||
| 813 | $list_active{$spec_name} = 0; | ||
| 814 | } | ||
| 815 | |||
| 816 | my $count_iterations = 0; | ||
| 817 | while (my $record_values = $iter_records->next) { | ||
| 818 | |||
| 819 | $count_iterations++; | ||
| 820 | last | ||
| 821 | if defined $list->{limit} | ||
| 822 | && $count_iterations > $list->{limit}; | ||
| 823 | |||
| 824 | # cut the separators away before copying | ||
| 825 | for my $sep (@{$list->{separators}}) { | ||
| 826 | for my $elt (@{$sep->{elts}}) { | ||
| 827 | $elt->cut(); | ||
| 828 | } | ||
| 829 | } | ||
| 830 | |||
| 831 | my $element = $element_template->copy(); | ||
| 832 | |||
| 833 | # make sure that we save and restore specification object | ||
| 834 | # otherwise it would be overwritten and can cause weird | ||
| 835 | # errors (GH #54) | ||
| 836 | |||
| 837 | my $old_spec = $self->specification; | ||
| 838 | $element = $self->_sub_process($element, $sub_spec, $record_values, undef, undef, $count, $level + 1); | ||
| 839 | $self->_set_specification($old_spec); | ||
| 840 | |||
| 841 | # Get rid of flutexml container and put it into position | ||
| 842 | my $current; | ||
| 843 | for my $e (reverse($element->cut_children())) { | ||
| 844 | $e->paste($paste_operation, $list_paste_to); | ||
| 845 | $current = $e; | ||
| 846 | } | ||
| 847 | |||
| 848 | # Add separator | ||
| 849 | if ($current && $list->{separators}) { | ||
| 850 | for my $sep (@{$list->{separators}}) { | ||
| 851 | |||
| 852 | if (my $every_x = $sep->{every}) { | ||
| 853 | unless ($count_iterations % $every_x == 0) { | ||
| 854 | # prevent the last separator to be removed | ||
| 855 | # if not last element. | ||
| 856 | $sep_copy = undef; | ||
| 857 | next; | ||
| 858 | } | ||
| 859 | } | ||
| 860 | for my $elt (@{$sep->{elts}}) { | ||
| 861 | $sep_copy = $elt->copy(); | ||
| 862 | my $operation = 'after'; | ||
| 863 | if ($children_classes{$sep_copy->att('class')}) { | ||
| 864 | $operation = 'last_child'; | ||
| 865 | } | ||
| 866 | $sep_copy->paste($operation, $current); | ||
| 867 | last; | ||
| 868 | } | ||
| 869 | } | ||
| 870 | } | ||
| 871 | $count++; | ||
| 872 | } | ||
| 873 | |||
| 874 | if (blessed $spec_iter && $spec_iter->isa('Template::Flute::Iterator::Cache')) { | ||
| 875 | $spec_iter->reset; | ||
| 876 | } | ||
| 877 | |||
| 878 | $element_template->cut(); # Remove template element | ||
| 879 | |||
| 880 | if ($sep_copy) { | ||
| 881 | # Remove last separator and original one(s) in the template | ||
| 882 | $sep_copy->cut(); | ||
| 883 | |||
| 884 | } | ||
| 885 | } | ||
| 886 | |||
| 887 | # Values | ||
| 888 | for my $elt ( @{$spec_elements->{value}}, @{$spec_elements->{param}}, @{$spec_elements->{field}} ){ | ||
| 889 | if ($elt->tag eq 'param') { | ||
| 890 | my $name = $spec_xml->att('name'); | ||
| 891 | |||
| 892 | # skip params on top level | ||
| 893 | next unless defined $name; | ||
| 894 | |||
| 895 | my $parent_name; | ||
| 896 | if ($elt->parent->gi eq 'container') { | ||
| 897 | next if $cut_container; | ||
| 898 | $parent_name = $name; | ||
| 899 | } | ||
| 900 | else { | ||
| 901 | $parent_name = $elt->parent->att('name'); | ||
| 902 | } | ||
| 903 | |||
| 904 | if ($name ne $parent_name) { | ||
| 905 | # don't process params of sublists again | ||
| 906 | next; | ||
| 907 | } | ||
| 908 | |||
| 909 | if (exists $list_active{$parent_name} && ! $list_active{$parent_name}) { | ||
| 910 | # don't process params for empty lists | ||
| 911 | next; | ||
| 912 | } | ||
| 913 | } | ||
| 914 | my $spec_id = $elt->{'att'}->{'id'}; | ||
| 915 | my $spec_name = $elt->{'att'}->{'name'}; | ||
| 916 | my $spec_class = $elt->{'att'}->{'class'} ? $elt->{'att'}->{'class'} : $spec_name; | ||
| 917 | |||
| 918 | # Use CLASS or ID if set | ||
| 919 | my $spec_clases = []; | ||
| 920 | if ($spec_id){ | ||
| 921 | $spec_clases = $specification->{ids}->{$spec_id}; | ||
| 922 | } | ||
| 923 | else { | ||
| 924 | $spec_clases = $classes->{$spec_class}; | ||
| 925 | } | ||
| 926 | |||
| 927 | for my $spec_class (@$spec_clases){ | ||
| 928 | # check if it's a form and it's already filled | ||
| 929 | if (exists $spec_class->{form} && $spec_class->{form}) { | ||
| 930 | my $form = $self->template->form($spec_class->{form}); | ||
| 931 | next if $form && $form->is_filled; | ||
| 932 | } | ||
| 933 | # check if we need an iterator for this element | ||
| 934 | if ($self->auto_iterators && $spec_class->{iterator}) { | ||
| 935 | my ($iter_name, $iter); | ||
| 936 | |||
| 937 | $iter_name = $spec_class->{iterator}; | ||
| 938 | |||
| 939 | unless ($specification->iterator($iter_name)) { | ||
| 940 | my $maybe_iter = $self->values->{$iter_name}; | ||
| 941 | |||
| 942 | if (defined blessed $maybe_iter) { | ||
| 943 | if ($maybe_iter->can('next') && | ||
| 944 | $maybe_iter->can('count')) { | ||
| 945 | $iter = $maybe_iter; | ||
| 946 | } | ||
| 947 | else { | ||
| 948 | croak "Object cannot be used as iterator for value $spec_name: ", ref($maybe_iter); | ||
| 949 | } | ||
| 950 | } | ||
| 951 | elsif (ref($self->values->{$iter_name}) eq 'ARRAY') { | ||
| 952 | $iter = Template::Flute::Iterator->new($self->values->{$iter_name}); | ||
| 953 | } | ||
| 954 | else { | ||
| 955 | $iter = Template::Flute::Iterator->new([]); | ||
| 956 | } | ||
| 957 | |||
| 958 | $specification->set_iterator($iter_name, $iter); | ||
| 959 | } | ||
| 960 | } | ||
| 961 | |||
| 962 | # Increment count | ||
| 963 | $spec_class->{increment} = Template::Flute::Increment->new( | ||
| 964 | increment => $spec_class->{increment}->{increment}, | ||
| 965 | start => $count | ||
| 966 | ) if $spec_class->{increment}; | ||
| 967 | |||
| 968 | my $field = $spec_class->{'field'}; | ||
| 969 | |||
| 970 | if (defined $field && ! ref($field) && $field =~ /\./) { | ||
| 971 | $spec_class->{'field'} = [split /\./, $field]; | ||
| 972 | } | ||
| 973 | |||
| 974 | $self->_replace_record($spec_name, $values, $spec_class, $spec_class->{elts}); | ||
| 975 | } | ||
| 976 | } | ||
| 977 | |||
| 978 | # cut again the invisible containers, after the values are interpolated | ||
| 979 | if ($level == 0) { | ||
| 980 | for my $container ($template->containers()) { | ||
| 981 | unless ($container->visible()) { | ||
| 982 | for my $elt (@{$container->elts()}) { | ||
| 983 | $elt->cut(); | ||
| 984 | } | ||
| 985 | } | ||
| 986 | } | ||
| 987 | } | ||
| 988 | |||
| 989 | return $count ? $template->{xml}->root() : $template->{xml}; | ||
| 990 | } | ||
| 991 | |||
| 992 | sub _replace_within_elts { | ||
| 993 | my ($param, $rep_str, $elt_handler, $elts) = @_; | ||
| 994 | my ($name, $zref); | ||
| 995 | for my $elt (@$elts) { | ||
| 996 | if ($elt_handler) { | ||
| 997 | $elt_handler->($elt, $rep_str); | ||
| 998 | next; | ||
| 999 | } | ||
| 1000 | |||
| 1001 | $name = $param->{name}; | ||
| 1002 | $zref = $elt->{"flute_$name"}; | ||
| 1003 | |||
| 1004 | if (! $elt->parent && $elt->former_parent) { | ||
| 1005 | # paste back a formerly cut element | ||
| 1006 | my $pos; | ||
| 1007 | |||
| 1008 | if (($pos = $elt->former_prev_sibling) && $pos->parent) { | ||
| 1009 | $elt->paste(after => $pos); | ||
| 1010 | } | ||
| 1011 | else { | ||
| 1012 | $elt->paste(first_child => $elt->former_parent); | ||
| 1013 | } | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | if ($zref->{rep_sub}) { | ||
| 1017 | # call subroutine to handle this element | ||
| 1018 | $zref->{rep_sub}->($elt, $rep_str); | ||
| 1019 | } elsif ($zref->{rep_att}) { | ||
| 1020 | # replace attribute instead of embedded text (e.g. for <input>) | ||
| 1021 | foreach my $replace_attr (_expand_elt_attributes($elt, $zref->{rep_att})) { | ||
| 1022 | if (exists $param->{op}) { | ||
| 1023 | if ($param->{op} eq 'toggle') { | ||
| 1024 | if ($rep_str) { | ||
| 1025 | $elt->set_att($replace_attr); | ||
| 1026 | } | ||
| 1027 | else { | ||
| 1028 | $elt->del_att($replace_attr); | ||
| 1029 | } | ||
| 1030 | next; | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | my $original_attribute = ''; | ||
| 1034 | |||
| 1035 | if (exists $zref->{rep_att_orig}->{$replace_attr}) { | ||
| 1036 | $original_attribute = $zref->{rep_att_orig}->{$replace_attr}; | ||
| 1037 | } | ||
| 1038 | |||
| 1039 | if (exists $param->{joiner}) { | ||
| 1040 | if ($rep_str) { | ||
| 1041 | if ($param->{op} eq 'append') { | ||
| 1042 | $elt->set_att($replace_attr, $original_attribute . $param->{joiner} . $rep_str); | ||
| 1043 | } | ||
| 1044 | elsif ($param->{op} eq 'prepend') { | ||
| 1045 | $elt->set_att($replace_attr, $rep_str . $param->{joiner} . $original_attribute); | ||
| 1046 | } | ||
| 1047 | } | ||
| 1048 | } | ||
| 1049 | else { | ||
| 1050 | my $rep_str_new; | ||
| 1051 | |||
| 1052 | if ($param->{op} eq 'append') { | ||
| 1053 | $rep_str_new = $rep_str ? ($original_attribute . $rep_str) : $original_attribute; | ||
| 1054 | } | ||
| 1055 | elsif ($param->{op} eq 'prepend') { | ||
| 1056 | $rep_str_new = $rep_str ? ($rep_str . $original_attribute) : $original_attribute; | ||
| 1057 | } | ||
| 1058 | |||
| 1059 | $elt->set_att($replace_attr, $rep_str_new); | ||
| 1060 | } | ||
| 1061 | next; | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | if (defined $rep_str) { | ||
| 1065 | $elt->set_att($replace_attr, $rep_str); | ||
| 1066 | } | ||
| 1067 | else { | ||
| 1068 | $elt->del_att($replace_attr); | ||
| 1069 | } | ||
| 1070 | } | ||
| 1071 | } elsif ($zref->{rep_elt}) { | ||
| 1072 | # use provided text element for replacement | ||
| 1073 | $zref->{rep_elt}->set_text($rep_str); | ||
| 1074 | } else { | ||
| 1075 | $elt->set_text($rep_str) if defined $rep_str; | ||
| 1076 | } | ||
| 1077 | } | ||
| 1078 | } | ||
| 1079 | |||
| 1080 | =head2 process_template | ||
| 1081 | |||
| 1082 | Processes HTML template and returns L<Template::Flute::HTML> object. | ||
| 1083 | |||
| 1084 | =cut | ||
| 1085 | |||
| 1086 | sub process_template { | ||
| 1087 | my ($self) = @_; | ||
| 1088 | |||
| 1089 | # unless ($self->{template}) { | ||
| 1090 | # $self->_bootstrap(); | ||
| 1091 | # } | ||
| 1092 | |||
| 1093 | return $self->template; | ||
| 1094 | } | ||
| 1095 | |||
| 1096 | |||
| 1097 | sub _replace_record { | ||
| 1098 | my ($self, $name, $values, $value, $elts) = @_; | ||
| 1099 | my ($key, $filter, $att_name, $att_spec, | ||
| 1100 | $att_tag_name, $att_tag_spec, %att_tags, $elt_handler, $raw, $rep_str,); | ||
| 1101 | |||
| 1102 | # determine value used for replacements | ||
| 1103 | $rep_str = $self->value($value, $values); | ||
| 1104 | #return undef if ((not defined $rep_str) and (defined $value->{target})); | ||
| 1105 | $raw = $rep_str; | ||
| 1106 | if ($self->_value_should_be_skipped($value, $rep_str)) { | ||
| 1107 | # do nothing | ||
| 1108 | return; | ||
| 1109 | } | ||
| 1110 | |||
| 1111 | if (exists $value->{op}) { | ||
| 1112 | if ($value->{op} eq 'toggle' && ! $value->{target}) { | ||
| 1113 | if (exists $value->{args} && $value->{args} eq 'static') { | ||
| 1114 | if ($rep_str) { | ||
| 1115 | # preserve static text, like a container | ||
| 1116 | return; | ||
| 1117 | } | ||
| 1118 | } | ||
| 1119 | |||
| 1120 | unless ($raw) { | ||
| 1121 | # remove corresponding HTML elements from tree | ||
| 1122 | for my $elt (@$elts) { | ||
| 1123 | $elt->cut(); | ||
| 1124 | } | ||
| 1125 | return; | ||
| 1126 | } | ||
| 1127 | $rep_str = '' unless defined $rep_str; | ||
| 1128 | } | ||
| 1129 | elsif ($value->{op} eq 'hook') { | ||
| 1130 | for my $elt (@$elts) { | ||
| 1131 | Template::Flute::HTML::hook_html($elt, $rep_str); | ||
| 1132 | } | ||
| 1133 | } | ||
| 1134 | elsif (ref($value->{op}) eq 'CODE') { | ||
| 1135 | $elt_handler = $value->{op}; | ||
| 1136 | } | ||
| 1137 | } | ||
| 1138 | #debug "$name has value "; | ||
| 1139 | #debug "'$rep_str'"; | ||
| 1140 | |||
| 1141 | # Template specified value if value defined | ||
| 1142 | if ($value->{value}) { | ||
| 1143 | if ($rep_str) { | ||
| 1144 | $rep_str = $value->{value}; | ||
| 1145 | } | ||
| 1146 | else { | ||
| 1147 | $rep_str = ''; | ||
| 1148 | } | ||
| 1149 | } | ||
| 1150 | |||
| 1151 | if (my $pattern = $value->{pattern}) { | ||
| 1152 | if (my $regexp = $self->_get_pattern($pattern)) { | ||
| 1153 | # if the value has a target operate on that attribute, otherwise | ||
| 1154 | # operate on the text, as expected | ||
| 1155 | if (my $attribute = $value->{target}) { | ||
| 1156 | $elt_handler = sub { | ||
| 1157 | my ($elt, $string) = @_; | ||
| 1158 | if (!defined($string)) { | ||
| 1159 | $string = ''; | ||
| 1160 | } | ||
| 1161 | foreach my $att (_expand_elt_attributes($elt, $attribute)) { | ||
| 1162 | my $newtext = $elt->att($att); | ||
| 1163 | if (!defined($newtext)) { | ||
| 1164 | $newtext = ''; | ||
| 1165 | } | ||
| 1166 | $newtext =~ s/$regexp/$string/g; | ||
| 1167 | $elt->set_att($att, $newtext) | ||
| 1168 | }; | ||
| 1169 | }; | ||
| 1170 | } | ||
| 1171 | else { | ||
| 1172 | $elt_handler = sub { | ||
| 1173 | my ($elt, $string) = @_; | ||
| 1174 | if (!defined($string)) { | ||
| 1175 | $string = ''; | ||
| 1176 | } | ||
| 1177 | my $newtext = $elt->text; | ||
| 1178 | if (!defined($newtext)) { | ||
| 1179 | $newtext = ''; | ||
| 1180 | } | ||
| 1181 | $newtext =~ s/$regexp/$string/g; | ||
| 1182 | $elt->set_text($newtext); | ||
| 1183 | }; | ||
| 1184 | } | ||
| 1185 | } | ||
| 1186 | else { | ||
| 1187 | croak "No pattern named $pattern!"; | ||
| 1188 | } | ||
| 1189 | |||
| 1190 | } | ||
| 1191 | |||
| 1192 | |||
| 1193 | if ($value->{increment}) { | ||
| 1194 | $rep_str = $value->{increment}->value(); | ||
| 1195 | $value->{increment}->increment(); | ||
| 1196 | } | ||
| 1197 | #return undef unless defined $rep_str; | ||
| 1198 | |||
| 1199 | if (ref($value->{op}) eq 'CODE') { | ||
| 1200 | _replace_within_elts($value, $rep_str, $value->{op}, $elts); | ||
| 1201 | } | ||
| 1202 | else { | ||
| 1203 | _replace_within_elts($value, $rep_str, $elt_handler, $elts); | ||
| 1204 | } | ||
| 1205 | } | ||
| 1206 | |||
| 1207 | sub _expand_elt_attributes { | ||
| 1208 | my ($elt, $attribute) = @_; | ||
| 1209 | if ($attribute eq '*') { | ||
| 1210 | my @attributes = keys %{ $elt->atts }; | ||
| 1211 | return grep { $_ ne 'class' and $_ ne 'id' and $_ ne 'name' } @attributes; | ||
| 1212 | } | ||
| 1213 | elsif ($attribute =~ m/,/) { | ||
| 1214 | return split(/\s*,\s*/, $attribute); | ||
| 1215 | } | ||
| 1216 | else { | ||
| 1217 | return $attribute; | ||
| 1218 | } | ||
| 1219 | } | ||
| 1220 | |||
| 1221 | |||
| 1222 | =head2 filter ELEMENT VALUE | ||
| 1223 | |||
| 1224 | Runs the filter used by ELEMENT on VALUE and returns the result. | ||
| 1225 | |||
| 1226 | =cut | ||
| 1227 | |||
| 1228 | sub filter { | ||
| 1229 | my ($self, $element, $value) = @_; | ||
| 1230 | my ($name, @filters); | ||
| 1231 | |||
| 1232 | $name = $element->{filter}; | ||
| 1233 | |||
| 1234 | @filters = grep {/\S/} split(/\s+/, $name); | ||
| 1235 | |||
| 1236 | if (@filters > 1) { | ||
| 1237 | # chain filters | ||
| 1238 | for my $f_name (@filters) { | ||
| 1239 | $value = $self->_filter($f_name, $element, $value); | ||
| 1240 | } | ||
| 1241 | |||
| 1242 | return $value; | ||
| 1243 | } | ||
| 1244 | else { | ||
| 1245 | return $self->_filter($name, $element, $value); | ||
| 1246 | } | ||
| 1247 | } | ||
| 1248 | |||
| 1249 | sub _filter { | ||
| 1250 | my ($self, $name, $element, $value) = @_; | ||
| 1251 | my ($filter, $mod_name, $class, $filter_obj, $filter_sub); | ||
| 1252 | |||
| 1253 | if (exists $self->_filter_subs->{$name}) { | ||
| 1254 | $filter = $self->_filter_subs->{$name}; | ||
| 1255 | return $filter->($value); | ||
| 1256 | } | ||
| 1257 | |||
| 1258 | unless (exists $self->_filter_objects->{$name}) { | ||
| 1259 | # try to bootstrap filter | ||
| 1260 | unless ($class = $self->_filter_class->{$name}) { | ||
| 1261 | $mod_name = join('', map {ucfirst($_)} split(/_/, $name)); | ||
| 1262 | $class = "Template::Flute::Filter::$mod_name"; | ||
| 1263 | } | ||
| 1264 | |||
| 1265 | $self->_filter_objects->{$name} = | ||
| 1266 | use_module($class)->new( options => $self->_filter_opts->{$name} ); | ||
| 1267 | } | ||
| 1268 | |||
| 1269 | $filter_obj = $self->_filter_objects->{$name}; | ||
| 1270 | |||
| 1271 | if ($filter_obj->can('twig')) { | ||
| 1272 | $element->{op} = sub {$filter_obj->twig(@_)}; | ||
| 1273 | } | ||
| 1274 | |||
| 1275 | return $filter_obj->filter($value); | ||
| 1276 | } | ||
| 1277 | |||
| 1278 | sub _paging { | ||
| 1279 | my ($self, $list, $iterator) = @_; | ||
| 1280 | |||
| 1281 | # turn iterator into paginator | ||
| 1282 | my $page_size = $list->{paging}->{page_size} || 20; | ||
| 1283 | |||
| 1284 | my ($iter, $pager); | ||
| 1285 | |||
| 1286 | if (defined blessed($iterator)) { | ||
| 1287 | # DBIx::Class::ResultSet objects have a pager method, but | ||
| 1288 | # it throws an error without a limit through the rows attribute | ||
| 1289 | if ($iterator->can('pager')) { | ||
| 1290 | if ($iterator->can('is_paged')) { | ||
| 1291 | if ($iterator->is_paged) { | ||
| 1292 | $pager = $iterator->pager; | ||
| 1293 | } | ||
| 1294 | } | ||
| 1295 | else { | ||
| 1296 | $pager = $iterator->pager; | ||
| 1297 | } | ||
| 1298 | } | ||
| 1299 | } | ||
| 1300 | |||
| 1301 | if ($pager) { | ||
| 1302 | $iter = Template::Flute::Pager->new(iterator => $pager, | ||
| 1303 | page_size => $page_size); | ||
| 1304 | } | ||
| 1305 | else { | ||
| 1306 | $iter = Template::Flute::Paginator->new(iterator => $iterator, | ||
| 1307 | page_size => $page_size); | ||
| 1308 | } | ||
| 1309 | |||
| 1310 | if ($iter->pages > 1) { | ||
| 1311 | my ($element_orig, $element_copy, %element_pos, $element_link, | ||
| 1312 | $paging_page, $paging_link, $slide_length, $element, $element_active, $paging_min, $paging_max); | ||
| 1313 | |||
| 1314 | $slide_length = $list->{paging}->{slide_length} || 0; | ||
| 1315 | |||
| 1316 | if (exists $list->{paging}->{page_value} and | ||
| 1317 | exists $self->values->{$list->{paging}->{page_value}}) { | ||
| 1318 | $paging_page = $self->values->{$list->{paging}->{page_value}}; | ||
| 1319 | } | ||
| 1320 | if (exists $list->{paging}->{link_value} and | ||
| 1321 | exists $self->values->{$list->{paging}->{link_value}}) { | ||
| 1322 | $paging_link = $self->values->{$list->{paging}->{link_value}}; | ||
| 1323 | } | ||
| 1324 | $paging_page ||= 1; | ||
| 1325 | |||
| 1326 | $iter->select_page($paging_page); | ||
| 1327 | # print "Page size is: " . $iter->page_size; | ||
| 1328 | |||
| 1329 | $paging_min = 1; | ||
| 1330 | $paging_max = $iter->pages; | ||
| 1331 | |||
| 1332 | if ($slide_length > 0) { | ||
| 1333 | # determine the page numbers to show up | ||
| 1334 | if ($iter->pages > $slide_length) { | ||
| 1335 | $paging_min = int($paging_page - $slide_length / 2); | ||
| 1336 | |||
| 1337 | if ($paging_min < 1) { | ||
| 1338 | $paging_min = 1; | ||
| 1339 | } | ||
| 1340 | |||
| 1341 | $paging_max = $paging_min + $slide_length - 1; | ||
| 1342 | } | ||
| 1343 | } | ||
| 1344 | |||
| 1345 | for my $type (qw/first previous next last active standard/) { | ||
| 1346 | if ($element = $list->{paging}->{elements}->{$type}) { | ||
| 1347 | $element_orig = shift @{$element->{elts}}; | ||
| 1348 | next unless $element_orig; | ||
| 1349 | |||
| 1350 | # cut any other elements | ||
| 1351 | for my $sf (@{$element->{elts}}) { | ||
| 1352 | $sf->cut; | ||
| 1353 | } | ||
| 1354 | } | ||
| 1355 | else { | ||
| 1356 | # skip processing of paging elements which aren't specified | ||
| 1357 | next; | ||
| 1358 | } | ||
| 1359 | |||
| 1360 | if ($element_orig->is_last_child()) { | ||
| 1361 | %element_pos = (last_child => $element_orig->parent()); | ||
| 1362 | } elsif ($element_orig->next_sibling()) { | ||
| 1363 | %element_pos = (before => $element_orig->next_sibling()); | ||
| 1364 | } else { | ||
| 1365 | croak "Neither last child nor next sibling."; | ||
| 1366 | } | ||
| 1367 | |||
| 1368 | if ($element->{type} eq 'active') { | ||
| 1369 | $element_active = $element_orig; | ||
| 1370 | } elsif ($element->{type} eq 'standard') { | ||
| 1371 | for (1 .. $iter->pages) { | ||
| 1372 | next if $_ < $paging_min || $_ > $paging_max; | ||
| 1373 | |||
| 1374 | if ($_ == $paging_page) { | ||
| 1375 | # Move active element here | ||
| 1376 | if ($element_active->{"flute_active"}->{rep_elt}) { | ||
| 1377 | $element_active->{"flute_active"}->{rep_elt}->set_text($_); | ||
| 1378 | } else { | ||
| 1379 | $element_active->set_text($_); | ||
| 1380 | } | ||
| 1381 | |||
| 1382 | $element_copy = $element_active->cut; | ||
| 1383 | $element_copy->paste(%element_pos); | ||
| 1384 | next; | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | # Adjust text | ||
| 1388 | if ($element_orig->{"flute_$element->{name}"}->{rep_elt}) { | ||
| 1389 | $element_orig->{"flute_$element->{name}"}->{rep_elt}->set_text($_); | ||
| 1390 | } else { | ||
| 1391 | $element_orig->set_text($_); | ||
| 1392 | } | ||
| 1393 | |||
| 1394 | # Adjust link | ||
| 1395 | if ($element_link = $element_orig->first_descendant('a')) { | ||
| 1396 | $self->_paging_link($element_link, $paging_link, $_); | ||
| 1397 | } | ||
| 1398 | |||
| 1399 | # Copy HTML element | ||
| 1400 | $element_copy = $element_orig->copy; | ||
| 1401 | $element_copy->paste(%element_pos); | ||
| 1402 | } | ||
| 1403 | |||
| 1404 | $element_orig->cut; | ||
| 1405 | } elsif ($element->{type} eq 'first') { | ||
| 1406 | if ($paging_page > 1) { | ||
| 1407 | # Adjust link | ||
| 1408 | if ($element_link = $element_orig->first_descendant('a')) { | ||
| 1409 | $self->_paging_link($element_link, $paging_link, 1); | ||
| 1410 | } | ||
| 1411 | } else { | ||
| 1412 | $element_orig->cut; | ||
| 1413 | } | ||
| 1414 | } elsif ($element->{type} eq 'last') { | ||
| 1415 | if ($paging_page < $iter->pages) { | ||
| 1416 | # Adjust link | ||
| 1417 | if ($element_link = $element_orig->first_descendant('a')) { | ||
| 1418 | $self->_paging_link($element_link, $paging_link, $iter->pages); | ||
| 1419 | } | ||
| 1420 | } else { | ||
| 1421 | $element_orig->cut; | ||
| 1422 | } | ||
| 1423 | } elsif ($element->{type} eq 'next') { | ||
| 1424 | if ($paging_page < $iter->pages) { | ||
| 1425 | # Adjust link | ||
| 1426 | if ($element_link = $element_orig->first_descendant('a')) { | ||
| 1427 | $self->_paging_link($element_link, $paging_link, $paging_page + 1); | ||
| 1428 | } | ||
| 1429 | } else { | ||
| 1430 | $element_orig->cut; | ||
| 1431 | } | ||
| 1432 | } elsif ($element->{type} eq 'previous') { | ||
| 1433 | if ($paging_page > 1) { | ||
| 1434 | # Adjust link | ||
| 1435 | if ($element_link = $element_orig->first_descendant('a')) { | ||
| 1436 | $self->_paging_link($element_link, $paging_link, $paging_page - 1); | ||
| 1437 | } | ||
| 1438 | } else { | ||
| 1439 | $element_orig->cut; | ||
| 1440 | } | ||
| 1441 | } | ||
| 1442 | } | ||
| 1443 | } else { | ||
| 1444 | # remove paging area | ||
| 1445 | for my $paging_elt (@{$list->{paging}->{elts}}) { | ||
| 1446 | $paging_elt->cut; | ||
| 1447 | } | ||
| 1448 | } | ||
| 1449 | return $iter; | ||
| 1450 | } | ||
| 1451 | |||
| 1452 | sub _paging_link { | ||
| 1453 | my ($self, $elt, $paging_link, $paging_page) = @_; | ||
| 1454 | my ($path, $uri); | ||
| 1455 | |||
| 1456 | if (ref($paging_link) =~ /^URI::/) { | ||
| 1457 | # add to path | ||
| 1458 | $uri = $paging_link->clone; | ||
| 1459 | if ($paging_page == 1) { | ||
| 1460 | $uri->path(join('/', $paging_link->path)); | ||
| 1461 | } | ||
| 1462 | else { | ||
| 1463 | $uri->path(join('/', $paging_link->path, $paging_page)); | ||
| 1464 | } | ||
| 1465 | $path = $uri->as_string; | ||
| 1466 | } | ||
| 1467 | elsif ($paging_link) { | ||
| 1468 | if ($paging_page == 1) { | ||
| 1469 | $path = "/$paging_link"; | ||
| 1470 | } | ||
| 1471 | else { | ||
| 1472 | $path = "/$paging_link/$paging_page"; | ||
| 1473 | } | ||
| 1474 | } | ||
| 1475 | else { | ||
| 1476 | $path = $paging_page; | ||
| 1477 | } | ||
| 1478 | |||
| 1479 | $elt->set_att(href => $path); | ||
| 1480 | } | ||
| 1481 | |||
| 1482 | =head2 value NAME | ||
| 1483 | |||
| 1484 | Returns the value for NAME. | ||
| 1485 | |||
| 1486 | =cut | ||
| 1487 | |||
| 1488 | sub value { | ||
| 1489 | my ($self, $value, $values) = @_; | ||
| 1490 | my ($raw_value, $ref_value, $rep_str, $record_is_object, $key); | ||
| 1491 | $ref_value = $values; | ||
| 1492 | $record_is_object = $self->_is_record_object($ref_value); | ||
| 1493 | |||
| 1494 | if ($self->scopes) { | ||
| 1495 | if (exists $value->{scope}) { | ||
| 1496 | $ref_value = $self->values->{$value->{scope}}; | ||
| 1497 | } | ||
| 1498 | } | ||
| 1499 | |||
| 1500 | if (exists $value->{include}) { | ||
| 1501 | my (%args, $include_file); | ||
| 1502 | |||
| 1503 | if ($self->template_file) { | ||
| 1504 | $include_file = Template::Flute::Utils::derive_filename | ||
| 1505 | ($self->template_file, $value->{include}, 1, | ||
| 1506 | pass_absolute => 1); | ||
| 1507 | } | ||
| 1508 | else { | ||
| 1509 | $include_file = $value->{include}; | ||
| 1510 | } | ||
| 1511 | |||
| 1512 | # process template and include it | ||
| 1513 | %args = ( | ||
| 1514 | template_file => $include_file, | ||
| 1515 | auto_iterators => $self->auto_iterators, | ||
| 1516 | i18n => $self->i18n, | ||
| 1517 | filters => $self->filters, | ||
| 1518 | values => $value->{field} | ||
| 1519 | ? $self->values->{ $value->{field} } | ||
| 1520 | : $self->{values}, | ||
| 1521 | uri => $self->uri, | ||
| 1522 | ); | ||
| 1523 | |||
| 1524 | $raw_value = Template::Flute->new(%args)->process(); | ||
| 1525 | } | ||
| 1526 | elsif (exists $value->{field}) { | ||
| 1527 | if (ref($value->{field}) eq 'ARRAY') { | ||
| 1528 | $raw_value = $ref_value; | ||
| 1529 | |||
| 1530 | for my $lookup (@{$value->{field}}) { | ||
| 1531 | if (ref($raw_value)) { | ||
| 1532 | if ($self->_is_record_object($raw_value)) { | ||
| 1533 | $raw_value = $raw_value->$lookup; | ||
| 1534 | } | ||
| 1535 | elsif (exists $raw_value->{$lookup}) { | ||
| 1536 | $raw_value = $raw_value->{$lookup}; | ||
| 1537 | } | ||
| 1538 | } | ||
| 1539 | else { | ||
| 1540 | $raw_value = ''; | ||
| 1541 | last; | ||
| 1542 | } | ||
| 1543 | } | ||
| 1544 | |||
| 1545 | if (ref $raw_value && ! $self->_is_record_object($raw_value)) { | ||
| 1546 | # second case: don't pass back stringified reference | ||
| 1547 | $raw_value = ''; | ||
| 1548 | } | ||
| 1549 | } | ||
| 1550 | else { | ||
| 1551 | $key = $value->{field}; | ||
| 1552 | $raw_value = $record_is_object ? $ref_value->$key : $ref_value->{$key}; | ||
| 1553 | } | ||
| 1554 | } | ||
| 1555 | else { | ||
| 1556 | $key = $value->{name}; | ||
| 1557 | $raw_value = $record_is_object ? $ref_value->$key : $ref_value->{$key}; | ||
| 1558 | |||
| 1559 | # if the value is undef, but the type is 'value', set it to | ||
| 1560 | # the empty string. this way we prevent template values to pop | ||
| 1561 | # up because no action is done somewhere else. | ||
| 1562 | if (!defined($raw_value) and $value->{type} eq 'value') { | ||
| 1563 | $raw_value = ''; | ||
| 1564 | } | ||
| 1565 | } | ||
| 1566 | |||
| 1567 | if ($value->{filter} | ||
| 1568 | and !$self->_value_should_be_skipped($value, $raw_value)) { | ||
| 1569 | $rep_str = $self->filter($value, $raw_value); | ||
| 1570 | } | ||
| 1571 | else { | ||
| 1572 | $rep_str = $raw_value; | ||
| 1573 | } | ||
| 1574 | |||
| 1575 | if (wantarray) { | ||
| 1576 | return ($raw_value, $rep_str); | ||
| 1577 | } | ||
| 1578 | |||
| 1579 | return $rep_str; | ||
| 1580 | } | ||
| 1581 | |||
| 1582 | # internal helpers | ||
| 1583 | |||
| 1584 | sub _is_record_object { | ||
| 1585 | my ($self, $record) = @_; | ||
| 1586 | my $class = blessed($record); | ||
| 1587 | return unless defined $class; | ||
| 1588 | |||
| 1589 | # it's an object. Check if we have it in the blacklist | ||
| 1590 | my @ignores = $self->_autodetect_ignores; | ||
| 1591 | my $is_good_object = 1; | ||
| 1592 | foreach my $i (@ignores) { | ||
| 1593 | if ($record->isa($i)) { | ||
| 1594 | $is_good_object = 0; | ||
| 1595 | last; | ||
| 1596 | } | ||
| 1597 | } | ||
| 1598 | return $is_good_object; | ||
| 1599 | } | ||
| 1600 | |||
| 1601 | sub _autodetect_ignores { | ||
| 1602 | my $self = shift; | ||
| 1603 | my @ignores; | ||
| 1604 | if ($self->autodetect and exists $self->autodetect->{disable}) { | ||
| 1605 | @ignores = @{ $self->autodetect->{disable} }; | ||
| 1606 | } | ||
| 1607 | foreach my $f (@ignores) { | ||
| 1608 | croak "empty string in the disabled autodetections" unless length($f); | ||
| 1609 | } | ||
| 1610 | return @ignores; | ||
| 1611 | } | ||
| 1612 | |||
| 1613 | sub _value_should_be_skipped { | ||
| 1614 | my ($self, $value, $replacement) = @_; | ||
| 1615 | if (my $skiptype = $value->{skip}) { | ||
| 1616 | if ($skiptype eq 'empty') { | ||
| 1617 | if (!defined($replacement) or | ||
| 1618 | $replacement =~ m/^\s*$/s) { | ||
| 1619 | return 1; | ||
| 1620 | } | ||
| 1621 | } | ||
| 1622 | else { | ||
| 1623 | croak "Unrecognized skip type $skiptype"; | ||
| 1624 | } | ||
| 1625 | } | ||
| 1626 | return; | ||
| 1627 | } | ||
| 1628 | |||
| 1629 | |||
| 1630 | =head2 set_values HASHREF | ||
| 1631 | |||
| 1632 | Sets hash reference of values to be used by the process method. | ||
| 1633 | Same as passing the hash reference as values argument to the | ||
| 1634 | constructor. | ||
| 1635 | |||
| 1636 | =head2 template | ||
| 1637 | |||
| 1638 | Returns HTML template object, see L<Template::Flute::HTML> for | ||
| 1639 | details. | ||
| 1640 | |||
| 1641 | =head2 specification | ||
| 1642 | |||
| 1643 | Returns specification object, see L<Template::Flute::Specification> for | ||
| 1644 | details. | ||
| 1645 | |||
| 1646 | =head2 patterns | ||
| 1647 | |||
| 1648 | Returns all patterns found in the specification. | ||
| 1649 | |||
| 1650 | =cut | ||
| 1651 | |||
| 1652 | # FIXME: (SysPete 28/4/16) (SysPete) What is scopes used for? I don't see | ||
| 1653 | # anything in the pod. | ||
| 1654 | |||
| 1655 | =head2 scopes | ||
| 1656 | |||
| 1657 | =head1 SPECIFICATION | ||
| 1658 | |||
| 1659 | The specification ties the elements in the HTML template to the data | ||
| 1660 | (variables, lists, forms) which is added to the template. | ||
| 1661 | |||
| 1662 | The default format for the specification is XML implemented by the | ||
| 1663 | L<Template::Flute::Specification::XML> module. You can use the Config::Scoped | ||
| 1664 | format implemented by L<Template::Flute::Specification::Scoped> module or | ||
| 1665 | write your own specification parser class. | ||
| 1666 | |||
| 1667 | =head2 COMMON ATTRIBUTES | ||
| 1668 | |||
| 1669 | Common attributes for specification elements are: | ||
| 1670 | |||
| 1671 | =over 4 | ||
| 1672 | |||
| 1673 | =item name | ||
| 1674 | |||
| 1675 | Name of element. | ||
| 1676 | |||
| 1677 | <value name="dancefloor"/> | ||
| 1678 | |||
| 1679 | =item class | ||
| 1680 | |||
| 1681 | Class of corresponding elements in the HTML template. | ||
| 1682 | |||
| 1683 | <value name="dancefloor" class="dancefloor-link"/> | ||
| 1684 | |||
| 1685 | If this attribute is omitted, the value of the name attribute | ||
| 1686 | is used to relate to the class in the HTML template. | ||
| 1687 | |||
| 1688 | =item id | ||
| 1689 | |||
| 1690 | Id of corresponding element in the HTML template. Overrides | ||
| 1691 | the class attribute for the specification element. | ||
| 1692 | |||
| 1693 | <value name="dancefloor" id="dancefloor-link"/> | ||
| 1694 | |||
| 1695 | =item target | ||
| 1696 | |||
| 1697 | HTML attribute to fill the value instead of replacing the body of | ||
| 1698 | the HTML element. | ||
| 1699 | |||
| 1700 | <value name="dancefloor" class="dancefloor-link" target="href"/> | ||
| 1701 | |||
| 1702 | =item joiner | ||
| 1703 | |||
| 1704 | String placed between the text and the appended value. The joiner | ||
| 1705 | isn't added if the value is empty. | ||
| 1706 | |||
| 1707 | =back | ||
| 1708 | |||
| 1709 | =head2 ELEMENTS | ||
| 1710 | |||
| 1711 | Possible elements in the specification are: | ||
| 1712 | |||
| 1713 | =over 4 | ||
| 1714 | |||
| 1715 | =item container | ||
| 1716 | |||
| 1717 | The first container is only shown in the output if the value C<billing_address> is set: | ||
| 1718 | |||
| 1719 | <container name="billing" value="billing_address" class="billingWrapper"> | ||
| 1720 | </container> | ||
| 1721 | |||
| 1722 | The second container is shown if the value C<warnings> or the value C<errors> is set: | ||
| 1723 | |||
| 1724 | <container name="account_errors" value="warnings|errors" class="infobox"> | ||
| 1725 | <value name="warnings"/> | ||
| 1726 | <value name="errors"/> | ||
| 1727 | </container> | ||
| 1728 | |||
| 1729 | =item list | ||
| 1730 | |||
| 1731 | =item separator | ||
| 1732 | |||
| 1733 | Separator elements for list are added after any list item in the output with | ||
| 1734 | the exception of the last one. | ||
| 1735 | |||
| 1736 | Example specification, HTML template and output: | ||
| 1737 | |||
| 1738 | <specification> | ||
| 1739 | <list name="list" iterator="tokens"> | ||
| 1740 | <param name="key"/> | ||
| 1741 | <separator name="sep"/> | ||
| 1742 | </list> | ||
| 1743 | </specification> | ||
| 1744 | |||
| 1745 | <div class="list"><span class="key">KEY</span></div><span class="sep"> | </span> | ||
| 1746 | |||
| 1747 | <div class="list"><span class="key">FOO</span></div><span class="sep"> | </span> | ||
| 1748 | <div class="list"><span class="key">BAR</span></div> | ||
| 1749 | |||
| 1750 | =item param | ||
| 1751 | |||
| 1752 | Param elements are replaced with the corresponding value from the list iterator. | ||
| 1753 | |||
| 1754 | The following operations are supported for param elements: | ||
| 1755 | |||
| 1756 | =over 4 | ||
| 1757 | |||
| 1758 | =item append | ||
| 1759 | |||
| 1760 | Appends the param value to the text found in the HTML template. | ||
| 1761 | |||
| 1762 | =item prepend | ||
| 1763 | |||
| 1764 | Prepends the param value to the text found in the HTML template. | ||
| 1765 | |||
| 1766 | =item target | ||
| 1767 | |||
| 1768 | The attribute to operate on. See below C<target> for C<value> for details. | ||
| 1769 | |||
| 1770 | =item toggle | ||
| 1771 | |||
| 1772 | When the C<args> attribute is set to C<tree>, it doesn't interpolate | ||
| 1773 | anything and just shows corresponding HTML element if param value is | ||
| 1774 | set. | ||
| 1775 | |||
| 1776 | With C<target> attribute, it simply toggles the target attribute. | ||
| 1777 | |||
| 1778 | Otherwise, if value is true, shows the HTML element and set its | ||
| 1779 | content to the value. If value is false, removes the HTML element. | ||
| 1780 | |||
| 1781 | So, if your element has children elements, you probably want to use | ||
| 1782 | the C<args="tree"> attribute (see below for an example). | ||
| 1783 | |||
| 1784 | =back | ||
| 1785 | |||
| 1786 | Other attributes for param elements are: | ||
| 1787 | |||
| 1788 | =over 4 | ||
| 1789 | |||
| 1790 | =item filter | ||
| 1791 | |||
| 1792 | Applies filter to param value. | ||
| 1793 | |||
| 1794 | =item increment | ||
| 1795 | |||
| 1796 | Uses value from increment instead of a value from the iterator. | ||
| 1797 | |||
| 1798 | <param name="pos" increment="1"> | ||
| 1799 | |||
| 1800 | =back | ||
| 1801 | |||
| 1802 | =item value | ||
| 1803 | |||
| 1804 | Value elements are replaced with a single value present in the values hash | ||
| 1805 | passed to the constructor of this class or later set with the | ||
| 1806 | L<set_values|/set_values HASHREF> method. | ||
| 1807 | |||
| 1808 | The following operations are supported for value elements: | ||
| 1809 | |||
| 1810 | =over 4 | ||
| 1811 | |||
| 1812 | =item append | ||
| 1813 | |||
| 1814 | Appends the value to the text found in the HTML template. | ||
| 1815 | |||
| 1816 | =item prepend | ||
| 1817 | |||
| 1818 | Prepends the value to the text found in the HTML template. | ||
| 1819 | |||
| 1820 | =item hook | ||
| 1821 | |||
| 1822 | Insert HTML residing in value as subtree of the corresponding HTML element. | ||
| 1823 | HTML will be parsed with L<XML::Twig>. See L</INSERT HTML> for an example. | ||
| 1824 | |||
| 1825 | =item keep | ||
| 1826 | |||
| 1827 | Preserves the text inside of the HTML element if value is false | ||
| 1828 | in the Perl sense. | ||
| 1829 | |||
| 1830 | =item toggle | ||
| 1831 | |||
| 1832 | Only shows corresponding HTML element if value is set. | ||
| 1833 | |||
| 1834 | =back | ||
| 1835 | |||
| 1836 | Other attributes for value elements are: | ||
| 1837 | |||
| 1838 | =over 4 | ||
| 1839 | |||
| 1840 | =item target | ||
| 1841 | |||
| 1842 | Specify the attribute to operate on instead of the tag content. It can | ||
| 1843 | be a named attribute (e.g., C<href>), the wildcard character(C<*>, | ||
| 1844 | meaning all the attributes found in the HTML template), or a comma | ||
| 1845 | separated list (e.g., C<alt,title>). | ||
| 1846 | |||
| 1847 | =item filter | ||
| 1848 | |||
| 1849 | Applies filter to value. | ||
| 1850 | |||
| 1851 | =item include | ||
| 1852 | |||
| 1853 | Processes the template file named in this attribute. This implies | ||
| 1854 | the hook operation. See L</INCLUDE FILES> for more information. | ||
| 1855 | |||
| 1856 | =back | ||
| 1857 | |||
| 1858 | =item form | ||
| 1859 | |||
| 1860 | Form elements are tied through specification to HTML forms. | ||
| 1861 | Attributes for form elements in addition to C<class> and C<id> are: | ||
| 1862 | |||
| 1863 | =over 4 | ||
| 1864 | |||
| 1865 | =item link | ||
| 1866 | |||
| 1867 | The link attribute can only have the value C<name> and allows to | ||
| 1868 | base the relationship between form specification elements and HTML | ||
| 1869 | form tags on the name HTML attribute instead of C<class>, which | ||
| 1870 | is usually more convenient. | ||
| 1871 | |||
| 1872 | =back | ||
| 1873 | |||
| 1874 | =item input | ||
| 1875 | |||
| 1876 | =item filter | ||
| 1877 | |||
| 1878 | =item sort | ||
| 1879 | |||
| 1880 | =item i18n | ||
| 1881 | |||
| 1882 | =item skip | ||
| 1883 | |||
| 1884 | This attribute (which can be provided to C<param> or C<value> | ||
| 1885 | specification elements) supports the following values: | ||
| 1886 | |||
| 1887 | =over 4 | ||
| 1888 | |||
| 1889 | =item empty | ||
| 1890 | |||
| 1891 | Do not replace the template string if the value or parameter is | ||
| 1892 | undefined, empty or just whitespace. | ||
| 1893 | |||
| 1894 | E.g. | ||
| 1895 | |||
| 1896 | <value name="cartline" skip="empty"/> | ||
| 1897 | <list name="items" iterator="items"> | ||
| 1898 | <param name="category" skip="empty"/> | ||
| 1899 | </list> | ||
| 1900 | |||
| 1901 | =back | ||
| 1902 | |||
| 1903 | =item pattern | ||
| 1904 | |||
| 1905 | You can define patterns in your specification to I<interpolate> the | ||
| 1906 | strings instead of replacing them. | ||
| 1907 | |||
| 1908 | A pattern is defined by the attributes C<name> and C<type> and its | ||
| 1909 | content. C<type> can be only C<string> or C<regexp>. | ||
| 1910 | |||
| 1911 | The interpolation happens if the C<value> and C<param> elements of the | ||
| 1912 | specification have an attribute C<pattern> set with the pattern's name. | ||
| 1913 | |||
| 1914 | Given this HTML: | ||
| 1915 | |||
| 1916 | <p class="cartline">There are 123 items in your shopping cart.</p> | ||
| 1917 | <ul> | ||
| 1918 | <li class="items"> | ||
| 1919 | <span class="number">1</span> | ||
| 1920 | <span class="category">in category 123</span> | ||
| 1921 | </li> | ||
| 1922 | </ul> | ||
| 1923 | |||
| 1924 | And this specification: | ||
| 1925 | |||
| 1926 | <specification> | ||
| 1927 | <pattern name="pxt" type="string">123</pattern> | ||
| 1928 | <list name="items" iterator="items"> | ||
| 1929 | <param name="number"/> | ||
| 1930 | <param name="category" pattern="pxt"/> | ||
| 1931 | </list> | ||
| 1932 | <value name="cartline" pattern="pxt"/> | ||
| 1933 | </specification> | ||
| 1934 | |||
| 1935 | In this example, in the cartline and category classes' text, only the | ||
| 1936 | template text "123" will be replaced by the value, not the whole | ||
| 1937 | element content, yielding such output: | ||
| 1938 | |||
| 1939 | <p class="cartline">There are 42 items in your shopping cart.</p> | ||
| 1940 | <ul> | ||
| 1941 | <li class="items"> | ||
| 1942 | <span class="number">1</span> | ||
| 1943 | <span class="category">in category tofu</span> | ||
| 1944 | </li> | ||
| 1945 | <li class="items"> | ||
| 1946 | <span class="number">2</span> | ||
| 1947 | <span class="category">in category pizza</span> | ||
| 1948 | </li> | ||
| 1949 | </ul> | ||
| 1950 | |||
| 1951 | Note: All matches of the pattern are subject to replacement, starting | ||
| 1952 | with version 0.025. | ||
| 1953 | |||
| 1954 | =back | ||
| 1955 | |||
| 1956 | =head1 SIMPLE OPERATORS | ||
| 1957 | |||
| 1958 | =head2 append | ||
| 1959 | |||
| 1960 | Appends the value to the text inside a HTML element or to an attribute | ||
| 1961 | if C<target> has been specified. This can be used in C<value> and C<param> | ||
| 1962 | specification elements. | ||
| 1963 | |||
| 1964 | The example shows how to add a HTML class to elements in a list: | ||
| 1965 | |||
| 1966 | HTML: | ||
| 1967 | |||
| 1968 | <ul class="nav-sub"> | ||
| 1969 | <li class="category"><a href="" class="catname">Medicine</a></li> | ||
| 1970 | </ul> | ||
| 1971 | |||
| 1972 | XML: | ||
| 1973 | |||
| 1974 | <specification> | ||
| 1975 | <list name="category" iterator="categories"> | ||
| 1976 | <param name="name" class="catname"/> | ||
| 1977 | <param name="catname" field="uri" target="href"/> | ||
| 1978 | <param name="css" class="catname" target="class" op="append" joiner=" "/> | ||
| 1979 | </list> | ||
| 1980 | </specification> | ||
| 1981 | |||
| 1982 | =head1 CONTAINERS | ||
| 1983 | |||
| 1984 | Conditional processing like C<IF> or C<ELSE> is done with the help of containers. | ||
| 1985 | |||
| 1986 | =head2 Display image only if present | ||
| 1987 | |||
| 1988 | In this example we want to show an image only on | ||
| 1989 | a certain condition: | ||
| 1990 | |||
| 1991 | HTML: | ||
| 1992 | |||
| 1993 | <span class="banner-box"> | ||
| 1994 | <img class="banner" src=""/> | ||
| 1995 | </span> | ||
| 1996 | |||
| 1997 | XML: | ||
| 1998 | |||
| 1999 | <container name="banner-box" value="banner"> | ||
| 2000 | <value name="banner" target="src"/> | ||
| 2001 | </container> | ||
| 2002 | |||
| 2003 | Source code: | ||
| 2004 | |||
| 2005 | if ($organization eq 'Big One') { | ||
| 2006 | $values{banner} = 'banners/big_one.png'; | ||
| 2007 | } | ||
| 2008 | |||
| 2009 | =head2 Display link in a list only if present | ||
| 2010 | |||
| 2011 | In this example we want so show a link only if | ||
| 2012 | an URL is available: | ||
| 2013 | |||
| 2014 | HTML: | ||
| 2015 | |||
| 2016 | <div class="linklist"> | ||
| 2017 | <span class="name">Name</span> | ||
| 2018 | <div class="link"> | ||
| 2019 | <a href="#" class="url">Goto ...</a> | ||
| 2020 | </div> | ||
| 2021 | </div> | ||
| 2022 | |||
| 2023 | XML: | ||
| 2024 | |||
| 2025 | <specification name="link"> | ||
| 2026 | <list name="links" class="linklist" iterator="links"> | ||
| 2027 | <param name="name"/> | ||
| 2028 | <param name="url" target="href"/> | ||
| 2029 | <container name="link" class="link" value="url"/> | ||
| 2030 | </list> | ||
| 2031 | </specification> | ||
| 2032 | |||
| 2033 | Source code: | ||
| 2034 | |||
| 2035 | @records = ({name => 'Link', url => 'http://localhost/'}, | ||
| 2036 | {name => 'No Link'}, | ||
| 2037 | {name => 'Another Link', url => 'http://localhost/'}, | ||
| 2038 | ); | ||
| 2039 | |||
| 2040 | $flute = Template::Flute->new(specification => $spec_xml, | ||
| 2041 | template => $template, | ||
| 2042 | iterators => {links => \@records}); | ||
| 2043 | |||
| 2044 | $output = $flute->process(); | ||
| 2045 | |||
| 2046 | =head1 ITERATORS | ||
| 2047 | |||
| 2048 | Template::Flute uses iterators to retrieve list elements and insert them into | ||
| 2049 | the document tree. This abstraction relieves us from worrying about where | ||
| 2050 | the data actually comes from. We basically just need an array of hash | ||
| 2051 | references and an iterator class with a next and a count method. For your | ||
| 2052 | convenience you can create an iterator from L<Template::Flute::Iterator> | ||
| 2053 | class very easily. | ||
| 2054 | |||
| 2055 | =head2 DROPDOWNS | ||
| 2056 | |||
| 2057 | Iterators can be used for dropdowns (HTML <select> elements) as well. | ||
| 2058 | |||
| 2059 | Template: | ||
| 2060 | |||
| 2061 | <select class="color"></select> | ||
| 2062 | |||
| 2063 | Specification: | ||
| 2064 | |||
| 2065 | <value name="color" iterator="colors"/> | ||
| 2066 | |||
| 2067 | Code: | ||
| 2068 | |||
| 2069 | @colors = ({value => 'red', label => 'Red'}, | ||
| 2070 | {value => 'black', label => 'Black'}); | ||
| 2071 | |||
| 2072 | $flute = Template::Flute->new(template => $html, | ||
| 2073 | specification => $spec, | ||
| 2074 | iterators => {colors => \@colors}, | ||
| 2075 | values => {color => 'black'}, | ||
| 2076 | ); | ||
| 2077 | |||
| 2078 | HTML output: | ||
| 2079 | |||
| 2080 | <select class="color"> | ||
| 2081 | <option value="red">Red</option> | ||
| 2082 | <option value="black" selected="selected">Black</option> | ||
| 2083 | </select> | ||
| 2084 | |||
| 2085 | =head3 Default value for dropdowns | ||
| 2086 | |||
| 2087 | You can specify the dropdown item which is selected by | ||
| 2088 | default with the C<iterator_default>) attribute. | ||
| 2089 | |||
| 2090 | Template: | ||
| 2091 | |||
| 2092 | <select class="color"></select> | ||
| 2093 | |||
| 2094 | Specification: | ||
| 2095 | |||
| 2096 | <value name="color" iterator="colors" iterator_default="black"/> | ||
| 2097 | |||
| 2098 | Code: | ||
| 2099 | |||
| 2100 | @colors = ({value => 'red', label => 'Red'}, | ||
| 2101 | {value => 'black', label => 'Black'}); | ||
| 2102 | |||
| 2103 | $flute = Template::Flute->new(template => $html, | ||
| 2104 | specification => $spec, | ||
| 2105 | iterators => {colors => \@colors}, | ||
| 2106 | ); | ||
| 2107 | |||
| 2108 | HTML output: | ||
| 2109 | |||
| 2110 | <select class="color"> | ||
| 2111 | <option value="red">Red</option> | ||
| 2112 | <option value="black" selected="selected">Black</option> | ||
| 2113 | </select> | ||
| 2114 | |||
| 2115 | =head3 Custom iterators for dropdowns | ||
| 2116 | |||
| 2117 | By default, the iterator for a dropdown is an arrayref of hashrefs | ||
| 2118 | with two hardcoded keys: C<value> and (optionally) C<label>. You can | ||
| 2119 | override this behaviour in the specification with | ||
| 2120 | C<iterator_value_key> and C<iterator_name_key> to use your own | ||
| 2121 | hashref's keys from the iterator, instead of C<value> and C<label>. | ||
| 2122 | |||
| 2123 | Specification: | ||
| 2124 | |||
| 2125 | <specification> | ||
| 2126 | <value name="color" iterator="colors" | ||
| 2127 | iterator_value_key="code" iterator_name_key="name"/> | ||
| 2128 | </specification> | ||
| 2129 | |||
| 2130 | Template: | ||
| 2131 | |||
| 2132 | <html> | ||
| 2133 | <select class="color"> | ||
| 2134 | <option value="example">Example</option> | ||
| 2135 | </select> | ||
| 2136 | </html> | ||
| 2137 | |||
| 2138 | Code: | ||
| 2139 | |||
| 2140 | @colors = ({code => 'red', name => 'Red'}, | ||
| 2141 | {code => 'black', name => 'Black'}, | ||
| 2142 | ); | ||
| 2143 | |||
| 2144 | $flute = Template::Flute->new(template => $html, | ||
| 2145 | specification => $spec, | ||
| 2146 | iterators => {colors => \@colors}, | ||
| 2147 | values => { color => 'black' }, | ||
| 2148 | ); | ||
| 2149 | |||
| 2150 | $out = $flute->process(); | ||
| 2151 | |||
| 2152 | Output: | ||
| 2153 | |||
| 2154 | <html> | ||
| 2155 | <head></head> | ||
| 2156 | <body> | ||
| 2157 | <select class="color"> | ||
| 2158 | <option value="red">Red</option> | ||
| 2159 | <option selected="selected" value="black">Black</option> | ||
| 2160 | </select> | ||
| 2161 | </body> | ||
| 2162 | </html> | ||
| 2163 | |||
| 2164 | |||
| 2165 | =head3 Limit lists | ||
| 2166 | |||
| 2167 | Sometimes you may wish to limit the number or iterations through you list. | ||
| 2168 | |||
| 2169 | Specification: | ||
| 2170 | |||
| 2171 | <specification> | ||
| 2172 | <list name="images" iterator="images" limit="1"> | ||
| 2173 | <param name="image" target="src" field="image_url" /> | ||
| 2174 | </list> | ||
| 2175 | </specification> | ||
| 2176 | |||
| 2177 | Template: | ||
| 2178 | |||
| 2179 | <div class="images"> | ||
| 2180 | <img class="image" src="/images/bottle.jpg" /> | ||
| 2181 | </div> | ||
| 2182 | |||
| 2183 | Code: | ||
| 2184 | |||
| 2185 | $images = [ | ||
| 2186 | { image_url => '/images/bottle1.jpg' }, | ||
| 2187 | { image_url => '/images/bottle2.jpg' }, | ||
| 2188 | { image_url => '/images/bottle3.jpg' }, | ||
| 2189 | ]; | ||
| 2190 | |||
| 2191 | $flute = Template::Flute->new( | ||
| 2192 | template => $html, | ||
| 2193 | specification => $spec, | ||
| 2194 | values => { images => $images }, | ||
| 2195 | ); | ||
| 2196 | |||
| 2197 | $out = $flute->process; | ||
| 2198 | |||
| 2199 | Output: | ||
| 2200 | |||
| 2201 | <html><head></head><body> | ||
| 2202 | <div class="images"> | ||
| 2203 | <img class="image" src="/images/bottle1.jpg" /> | ||
| 2204 | </div> | ||
| 2205 | </body></html> | ||
| 2206 | |||
| 2207 | =head1 LISTS | ||
| 2208 | |||
| 2209 | Lists can be accessed after parsing the specification and the HTML template | ||
| 2210 | through the HTML template object: | ||
| 2211 | |||
| 2212 | $flute->template->lists(); | ||
| 2213 | |||
| 2214 | $flute->template->list('cart'); | ||
| 2215 | |||
| 2216 | Only lists present in the specification and the HTML template can be | ||
| 2217 | addressed in this way. | ||
| 2218 | |||
| 2219 | See L<Template::Flute::List> for details about lists. | ||
| 2220 | |||
| 2221 | =head1 OBJECTS AND STRUCTURES | ||
| 2222 | |||
| 2223 | You can pass objects and hashrefs as values. To access a key or an | ||
| 2224 | accessor, you have to use a dotted notation with C<field>. An example | ||
| 2225 | for both hashrefs and objects follows. | ||
| 2226 | |||
| 2227 | Specification: | ||
| 2228 | |||
| 2229 | <specification> | ||
| 2230 | <value name="object" field="myobject.method" /> | ||
| 2231 | <value name="struct" field="mystruct.key" /> | ||
| 2232 | </specification> | ||
| 2233 | |||
| 2234 | |||
| 2235 | HTML: | ||
| 2236 | |||
| 2237 | <html> | ||
| 2238 | <body> | ||
| 2239 | <span class="object">Welcome back!</span> | ||
| 2240 | <span class="struct">Another one</span> | ||
| 2241 | </body> | ||
| 2242 | </html> | ||
| 2243 | |||
| 2244 | |||
| 2245 | Code: | ||
| 2246 | |||
| 2247 | package My::Object; | ||
| 2248 | sub new { | ||
| 2249 | my $class = shift; | ||
| 2250 | bless {}, $class; | ||
| 2251 | } | ||
| 2252 | sub method { | ||
| 2253 | return "Hello from the method"; | ||
| 2254 | } | ||
| 2255 | package main; | ||
| 2256 | my $flute = Template::Flute->new( | ||
| 2257 | specification => $spec, | ||
| 2258 | template => $html, | ||
| 2259 | values => { | ||
| 2260 | myobject => My::Object->new, | ||
| 2261 | mystruct => { key => "Hello from hash" }, | ||
| 2262 | } | ||
| 2263 | ); | ||
| 2264 | |||
| 2265 | C<process> will return: | ||
| 2266 | |||
| 2267 | <html> | ||
| 2268 | <head></head> | ||
| 2269 | <body> | ||
| 2270 | <span class="object">Hello from the method</span> | ||
| 2271 | <span class="struct">Hello from hash</span> | ||
| 2272 | </body> | ||
| 2273 | </html> | ||
| 2274 | |||
| 2275 | Sometimes you need to treat an object like an hashref. How to do that | ||
| 2276 | is explained under the C<autodetect> option for the constructor. | ||
| 2277 | |||
| 2278 | =head1 FORMS | ||
| 2279 | |||
| 2280 | Forms can be accessed after parsing the specification and the HTML template | ||
| 2281 | through the HTML template object: | ||
| 2282 | |||
| 2283 | $flute->template->forms(); | ||
| 2284 | |||
| 2285 | $flute->template->form('edit_content'); | ||
| 2286 | |||
| 2287 | Only forms present in the specification and the HTML template can be | ||
| 2288 | addressed in this way. | ||
| 2289 | |||
| 2290 | See L<Template::Flute::Form> for details about forms. | ||
| 2291 | |||
| 2292 | =head1 FILTERS | ||
| 2293 | |||
| 2294 | Filters are used to change the display of value and param elements in | ||
| 2295 | the resulting HTML output: | ||
| 2296 | |||
| 2297 | <value name="billing_address" filter="eol"/> | ||
| 2298 | |||
| 2299 | <param name="price" filter="currency"/> | ||
| 2300 | |||
| 2301 | The following filters are included: | ||
| 2302 | |||
| 2303 | =over 4 | ||
| 2304 | |||
| 2305 | =item upper | ||
| 2306 | |||
| 2307 | Uppercase filter, see L<Template::Flute::Filter::Upper>. | ||
| 2308 | |||
| 2309 | =item strip | ||
| 2310 | |||
| 2311 | Strips whitespace at the beginning at the end, | ||
| 2312 | see L<Template::Flute::Filter::Strip>. | ||
| 2313 | |||
| 2314 | =item eol | ||
| 2315 | |||
| 2316 | Filter preserving line breaks, see L<Template::Flute::Filter::Eol>. | ||
| 2317 | |||
| 2318 | =item nobreak_single | ||
| 2319 | |||
| 2320 | Filter replacing missing text with no-break space, | ||
| 2321 | see L<Template::Flute::Filter::NobreakSingle>. | ||
| 2322 | |||
| 2323 | =item currency | ||
| 2324 | |||
| 2325 | Currency filter, see L<Template::Flute::Filter::Currency>. | ||
| 2326 | Requires L<Number::Format> module. | ||
| 2327 | |||
| 2328 | =item date | ||
| 2329 | |||
| 2330 | Date filter, see L<Template::Flute::Filter::Date>. | ||
| 2331 | Requires L<DateTime> and L<DateTime::Format::ISO8601> modules. | ||
| 2332 | |||
| 2333 | =item country_name | ||
| 2334 | |||
| 2335 | Country name filter, see L<Template::Flute::Filter::CountryName>. | ||
| 2336 | Requires L<Locales> module. | ||
| 2337 | |||
| 2338 | =item language_name | ||
| 2339 | |||
| 2340 | Language name filter, see L<Template::Flute::Filter::LanguageName>. | ||
| 2341 | Requires L<Locales> module. | ||
| 2342 | |||
| 2343 | =item json_var | ||
| 2344 | |||
| 2345 | JSON to Javascript variable filter, see L<Template::Flute::Filter::JsonVar>. | ||
| 2346 | Requires L<JSON> module. | ||
| 2347 | |||
| 2348 | =item lower_dash | ||
| 2349 | |||
| 2350 | Replaces spaces with dashes (-) and makes lowercase. | ||
| 2351 | see L<Template::Flute::Filter::LowerDash>. | ||
| 2352 | |||
| 2353 | =item markdown | ||
| 2354 | |||
| 2355 | Turns text in Markdown format into HTML. | ||
| 2356 | see L<Template::Flute::Filter::Markdown>. | ||
| 2357 | |||
| 2358 | Requires L<Text::Markdown> and L<HTML::Scrubber> modules. | ||
| 2359 | |||
| 2360 | =back | ||
| 2361 | |||
| 2362 | Filter classes are loaded at runtime for efficiency and to keep the | ||
| 2363 | number of dependencies for Template::Flute as small as possible. | ||
| 2364 | |||
| 2365 | See above for prerequisites needed by the included filter classes. | ||
| 2366 | |||
| 2367 | =head2 Chained Filters | ||
| 2368 | |||
| 2369 | Filters can also be chained: | ||
| 2370 | |||
| 2371 | <value name="note" filter="upper eol"/> | ||
| 2372 | |||
| 2373 | Example template: | ||
| 2374 | |||
| 2375 | <div class="note"> | ||
| 2376 | This is a note. | ||
| 2377 | </div> | ||
| 2378 | |||
| 2379 | With the following value: | ||
| 2380 | |||
| 2381 | Update now! | ||
| 2382 | Avoid security hazards! | ||
| 2383 | |||
| 2384 | The HTML output would look like: | ||
| 2385 | |||
| 2386 | <div class="note"> | ||
| 2387 | UPDATE NOW!<br /> | ||
| 2388 | AVOID SECURITY HAZARDS! | ||
| 2389 | </div> | ||
| 2390 | |||
| 2391 | =head1 INSERT HTML AND INCLUDE FILES | ||
| 2392 | |||
| 2393 | =head2 INSERT HTML | ||
| 2394 | |||
| 2395 | HTML can be generated in the code or retrieved from a database | ||
| 2396 | and inserted into the template through the C<hook> operation: | ||
| 2397 | |||
| 2398 | <value name="description" op="hook"/> | ||
| 2399 | |||
| 2400 | The result replaces the inner HTML of the following C<div> tag: | ||
| 2401 | |||
| 2402 | <div class="description"> | ||
| 2403 | Sample content | ||
| 2404 | </div> | ||
| 2405 | |||
| 2406 | =head2 INCLUDE FILES | ||
| 2407 | |||
| 2408 | Files, especially components for web pages can be processed and included | ||
| 2409 | through value elements with the include attribute: | ||
| 2410 | |||
| 2411 | <value name="sidebar" include="component.html"/> | ||
| 2412 | |||
| 2413 | The result replaces the inner HTML of the following C<div> tag: | ||
| 2414 | |||
| 2415 | <div class="sidebar"> | ||
| 2416 | Sample content | ||
| 2417 | </div> | ||
| 2418 | |||
| 2419 | =head1 INSTALLATION | ||
| 2420 | |||
| 2421 | C<Template::Flute> can be installed from the latest release on CPAN, or if | ||
| 2422 | you wish for the very latest version, you can also install from the sources | ||
| 2423 | on GitHub. | ||
| 2424 | |||
| 2425 | =head2 FROM CPAN | ||
| 2426 | |||
| 2427 | To install from CPAN, simply use the C<cpanm> utility: | ||
| 2428 | |||
| 2429 | $ cpanm Template::Flute | ||
| 2430 | |||
| 2431 | =head2 FROM SOURCE | ||
| 2432 | |||
| 2433 | To install from source, first clone the repository, install the required | ||
| 2434 | dependencies, and build: | ||
| 2435 | |||
| 2436 | $ git clone https://github.com/racke/Template-Flute | ||
| 2437 | $ cd Template-Flute | ||
| 2438 | $ cpanm --installdeps . | ||
| 2439 | $ perl Makefile.PL | ||
| 2440 | $ make | ||
| 2441 | $ make test # optional, but still a good idea | ||
| 2442 | $ make install | ||
| 2443 | |||
| 2444 | =head1 AUTHOR | ||
| 2445 | |||
| 2446 | Stefan Hornburg (Racke), <racke@linuxia.de> | ||
| 2447 | |||
| 2448 | =head1 BUGS | ||
| 2449 | |||
| 2450 | Please report any bugs or feature requests at L<https://github.com/racke/Template-Flute/issues>. | ||
| 2451 | |||
| 2452 | =head1 SUPPORT | ||
| 2453 | |||
| 2454 | You can find documentation for this module with the perldoc command. | ||
| 2455 | |||
| 2456 | perldoc Template::Flute | ||
| 2457 | |||
| 2458 | You can also look for information at: | ||
| 2459 | |||
| 2460 | =over 4 | ||
| 2461 | |||
| 2462 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 2463 | |||
| 2464 | L<http://annocpan.org/dist/Template-Flute> | ||
| 2465 | |||
| 2466 | =item * CPAN Ratings | ||
| 2467 | |||
| 2468 | L<http://cpanratings.perl.org/d/Template-Flute> | ||
| 2469 | |||
| 2470 | =item * Search CPAN | ||
| 2471 | |||
| 2472 | L<http://search.cpan.org/dist/Template-Flute/> | ||
| 2473 | |||
| 2474 | =back | ||
| 2475 | |||
| 2476 | =head1 ACKNOWLEDGEMENTS | ||
| 2477 | |||
| 2478 | Thanks to Nitish Bezzala (GH #157). | ||
| 2479 | |||
| 2480 | Thanks to Mohammad S Anwar (GH #156). | ||
| 2481 | |||
| 2482 | Thanks to Paul Cochrane for his tremendous amount of pull requests | ||
| 2483 | issued during the GitHub challenge. | ||
| 2484 | |||
| 2485 | Thanks to Peter Mottram (GH #81, #87). | ||
| 2486 | |||
| 2487 | Thanks to William Carr (GH #86, #91). | ||
| 2488 | |||
| 2489 | Thanks to David Precious (bigpresh) for writing a much clearer introduction for | ||
| 2490 | Template::Flute. | ||
| 2491 | |||
| 2492 | Thanks to Grega Pompe for proper implementation of nested lists and | ||
| 2493 | a documentation fix. | ||
| 2494 | |||
| 2495 | Thanks to Jeff Boes for spotting a typo in the documentation of the | ||
| 2496 | Template::Flute::Filter::JsonVar class. | ||
| 2497 | |||
| 2498 | Thanks to Ton Verhagen for being a big supporter of my projects in all aspects. | ||
| 2499 | |||
| 2500 | Thanks to Sam Batschelet (GH #14, #93). | ||
| 2501 | |||
| 2502 | Thanks to Terrence Brannon for spotting a documentation mix-up. | ||
| 2503 | |||
| 2504 | =head1 HISTORY | ||
| 2505 | |||
| 2506 | Template::Flute was initially named Template::Zoom. I renamed the module because of | ||
| 2507 | a request from Matt S. Trout, author of the L<HTML::Zoom> module. | ||
| 2508 | |||
| 2509 | =head1 LICENSE AND COPYRIGHT | ||
| 2510 | |||
| 2511 | Copyright 2010-2019 Stefan Hornburg (Racke) <racke@linuxia.de>. | ||
| 2512 | |||
| 2513 | This program is free software; you can redistribute it and/or modify it | ||
| 2514 | under the terms of either: the GNU General Public License as published | ||
| 2515 | by the Free Software Foundation; or the Artistic License. | ||
| 2516 | |||
| 2517 | See http://dev.perl.org/licenses/ for more information. | ||
| 2518 | |||
| 2519 | =cut | ||
| 2520 | |||
| 2521 | 1; | ||