| 1 | #============================================================= -*-Perl-*- | ||
| 2 | # | ||
| 3 | # Template::View | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # A custom view of a template processing context. Can be used to | ||
| 7 | # implement custom "skins". | ||
| 8 | # | ||
| 9 | # AUTHOR | ||
| 10 | # Andy Wardley <abw@kfs.org> | ||
| 11 | # | ||
| 12 | # COPYRIGHT | ||
| 13 | # Copyright (C) 2000-2022 Andy Wardley. All Rights Reserved. | ||
| 14 | # | ||
| 15 | # This module is free software; you can redistribute it and/or | ||
| 16 | # modify it under the same terms as Perl itself. | ||
| 17 | # | ||
| 18 | # TODO | ||
| 19 | # * allowing print to have a hash ref as final args will cause problems | ||
| 20 | # if you do this: [% view.print(hash1, hash2, hash3) %]. Current | ||
| 21 | # work-around is to do [% view.print(hash1); view.print(hash2); | ||
| 22 | # view.print(hash3) %] or [% view.print(hash1, hash2, hash3, { }) %] | ||
| 23 | # | ||
| 24 | #============================================================================ | ||
| 25 | |||
| 26 | package Template::View; | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | use warnings; | ||
| 30 | use base 'Template::Base'; | ||
| 31 | |||
| 32 | our $VERSION = '3.100'; | ||
| 33 | our $DEBUG = 0 unless defined $DEBUG; | ||
| 34 | our @BASEARGS = qw( context ); | ||
| 35 | our $AUTOLOAD; | ||
| 36 | our $MAP = { | ||
| 37 | HASH => 'hash', | ||
| 38 | ARRAY => 'list', | ||
| 39 | TEXT => 'text', | ||
| 40 | default => '', | ||
| 41 | }; | ||
| 42 | |||
| 43 | |||
| 44 | #------------------------------------------------------------------------ | ||
| 45 | # _init(\%config) | ||
| 46 | # | ||
| 47 | # Initialisation method called by the Template::Base class new() | ||
| 48 | # constructor. $self->{ context } has already been set, by virtue of | ||
| 49 | # being named in @BASEARGS. Remaining config arguments are presented | ||
| 50 | # as a hash reference. | ||
| 51 | #------------------------------------------------------------------------ | ||
| 52 | |||
| 53 | sub _init { | ||
| 54 | my ($self, $config) = @_; | ||
| 55 | |||
| 56 | # move 'context' somewhere more private | ||
| 57 | $self->{ _CONTEXT } = $self->{ context }; | ||
| 58 | delete $self->{ context }; | ||
| 59 | |||
| 60 | # generate table mapping object types to templates | ||
| 61 | my $map = $config->{ map } || { }; | ||
| 62 | $map->{ default } = $config->{ default } unless defined $map->{ default }; | ||
| 63 | $self->{ map } = { | ||
| 64 | %$MAP, | ||
| 65 | %$map, | ||
| 66 | }; | ||
| 67 | |||
| 68 | # local BLOCKs definition table | ||
| 69 | $self->{ _BLOCKS } = $config->{ blocks } || { }; | ||
| 70 | |||
| 71 | # name of presentation method which printed objects might provide | ||
| 72 | $self->{ method } = defined $config->{ method } | ||
| 73 | ? $config->{ method } | ||
| 74 | : 'present'; | ||
| 75 | |||
| 76 | # view is sealed by default preventing variable update after | ||
| 77 | # definition, however we don't actually seal a view until the | ||
| 78 | # END of the view definition | ||
| 79 | my $sealed = $config->{ sealed }; | ||
| 80 | $sealed = 1 unless defined $sealed; | ||
| 81 | $self->{ sealed } = $sealed ? 1 : 0; | ||
| 82 | |||
| 83 | # copy remaining config items from $config or set defaults | ||
| 84 | foreach my $arg (qw( base prefix suffix notfound silent )) { | ||
| 85 | $self->{ $arg } = $config->{ $arg } || ''; | ||
| 86 | } | ||
| 87 | |||
| 88 | # check that any base specified is defined | ||
| 89 | return $self->error('Invalid base specified for view') | ||
| 90 | if exists $config->{ base } && ! $self->{ base }; | ||
| 91 | |||
| 92 | # name of data item used by view() | ||
| 93 | $self->{ item } = $config->{ item } || 'item'; | ||
| 94 | |||
| 95 | # map methods of form ${include_prefix}_foobar() to include('foobar')? | ||
| 96 | $self->{ include_prefix } = $config->{ include_prefix } || 'include_'; | ||
| 97 | # what about mapping foobar() to include('foobar')? | ||
| 98 | $self->{ include_naked } = defined $config->{ include_naked } | ||
| 99 | ? $config->{ include_naked } | ||
| 100 | : 1; | ||
| 101 | |||
| 102 | # map methods of form ${view_prefix}_foobar() to include('foobar')? | ||
| 103 | $self->{ view_prefix } = $config->{ view_prefix } || 'view_'; | ||
| 104 | # what about mapping foobar() to view('foobar')? | ||
| 105 | $self->{ view_naked } = $config->{ view_naked } || 0; | ||
| 106 | |||
| 107 | # the view is initially unsealed, allowing directives in the initial | ||
| 108 | # view template to create data items via the AUTOLOAD; once sealed via | ||
| 109 | # call to seal(), the AUTOLOAD will not update any internal items. | ||
| 110 | delete @$config{ qw( | ||
| 111 | base method map default prefix suffix notfound item | ||
| 112 | include_prefix include_naked silent sealed | ||
| 113 | view_prefix view_naked blocks | ||
| 114 | ) }; | ||
| 115 | $config = { %{ $self->{ base }->{ data } }, %$config } | ||
| 116 | if $self->{ base }; | ||
| 117 | $self->{ data } = $config; | ||
| 118 | $self->{ SEALED } = 0; | ||
| 119 | |||
| 120 | return $self; | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | #------------------------------------------------------------------------ | ||
| 125 | # seal() | ||
| 126 | # unseal() | ||
| 127 | # | ||
| 128 | # Seal or unseal the view to allow/prevent new data items from being | ||
| 129 | # automatically created by the AUTOLOAD method. | ||
| 130 | #------------------------------------------------------------------------ | ||
| 131 | |||
| 132 | sub seal { | ||
| 133 | my $self = shift; | ||
| 134 | $self->{ SEALED } = $self->{ sealed }; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub unseal { | ||
| 138 | my $self = shift; | ||
| 139 | $self->{ SEALED } = 0; | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | #------------------------------------------------------------------------ | ||
| 144 | # clone(\%config) | ||
| 145 | # | ||
| 146 | # Cloning method which takes a copy of $self and then applies to it any | ||
| 147 | # modifications specified in the $config hash passed as an argument. | ||
| 148 | # Configuration items may also be specified as a list of "name => $value" | ||
| 149 | # arguments. Returns a reference to the cloned Template::View object. | ||
| 150 | # | ||
| 151 | # NOTE: may need to copy BLOCKS??? | ||
| 152 | #------------------------------------------------------------------------ | ||
| 153 | |||
| 154 | sub clone { | ||
| 155 | my $self = shift; | ||
| 156 | my $clone = bless { %$self }, ref $self; | ||
| 157 | my $config = ref $_[0] eq 'HASH' ? shift : { @_ }; | ||
| 158 | |||
| 159 | # merge maps | ||
| 160 | $clone->{ map } = { | ||
| 161 | %{ $self->{ map } }, | ||
| 162 | %{ $config->{ map } || { } }, | ||
| 163 | }; | ||
| 164 | |||
| 165 | # "map => { default=>'xxx' }" can be specified as "default => 'xxx'" | ||
| 166 | $clone->{ map }->{ default } = $config->{ default } | ||
| 167 | if defined $config->{ default }; | ||
| 168 | |||
| 169 | # update any remaining config items | ||
| 170 | my @args = qw( | ||
| 171 | base prefix suffix notfound item method include_prefix | ||
| 172 | include_naked view_prefix view_naked | ||
| 173 | ); | ||
| 174 | foreach my $arg (@args) { | ||
| 175 | $clone->{ $arg } = $config->{ $arg } if defined $config->{ $arg }; | ||
| 176 | } | ||
| 177 | push(@args, qw( default map )); | ||
| 178 | delete @$config{ @args }; | ||
| 179 | |||
| 180 | # anything left is data | ||
| 181 | my $data = $clone->{ data } = { %{ $self->{ data } } }; | ||
| 182 | @$data{ keys %$config } = values %$config; | ||
| 183 | |||
| 184 | return $clone; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | #------------------------------------------------------------------------ | ||
| 189 | # print(@items, ..., \%config) | ||
| 190 | # | ||
| 191 | # Prints @items in turn by mapping each to an appropriate template using | ||
| 192 | # the internal 'map' hash. If an entry isn't found and the item is an | ||
| 193 | # object that implements the method named in the internal 'method' item, | ||
| 194 | # (default: 'present'), then the method will be called passing a reference | ||
| 195 | # to $self, against which the presenter method may make callbacks (e.g. | ||
| 196 | # to view_item()). If the presenter method isn't implemented, then the | ||
| 197 | # 'default' map entry is consulted and used if defined. The final argument | ||
| 198 | # may be a reference to a hash array providing local overrides to the internal | ||
| 199 | # defaults for various items (prefix, suffix, etc). In the presence | ||
| 200 | # of this parameter, a clone of the current object is first made, applying | ||
| 201 | # any configuration updates, and control is then delegated to it. | ||
| 202 | #------------------------------------------------------------------------ | ||
| 203 | |||
| 204 | sub print { | ||
| 205 | my $self = shift; | ||
| 206 | |||
| 207 | # if final config hash is specified then create a clone and delegate to it | ||
| 208 | # NOTE: potential problem when called print(\%data_hash1, \%data_hash2); | ||
| 209 | if ((scalar @_ > 1) && (ref $_[-1] eq 'HASH')) { | ||
| 210 | my $cfg = pop @_; | ||
| 211 | my $clone = $self->clone($cfg) | ||
| 212 | || return; | ||
| 213 | return $clone->print(@_) | ||
| 214 | || $self->error($clone->error()); | ||
| 215 | } | ||
| 216 | my ($item, $type, $template, $present); | ||
| 217 | my $method = $self->{ method }; | ||
| 218 | my $map = $self->{ map }; | ||
| 219 | my $output = ''; | ||
| 220 | |||
| 221 | # print each argument | ||
| 222 | foreach $item (@_) { | ||
| 223 | my $newtype; | ||
| 224 | |||
| 225 | if (! ($type = ref $item)) { | ||
| 226 | # non-references are TEXT | ||
| 227 | $type = 'TEXT'; | ||
| 228 | $template = $map->{ $type }; | ||
| 229 | } | ||
| 230 | elsif (! defined ($template = $map->{ $type })) { | ||
| 231 | # no specific map entry for object, maybe it implements a | ||
| 232 | # 'present' (or other) method? | ||
| 233 | if ( $method && UNIVERSAL::can($item, $method) ) { | ||
| 234 | $present = $item->$method($self); ## call item method | ||
| 235 | # undef returned indicates error, note that we expect | ||
| 236 | # $item to have called error() on the view | ||
| 237 | return unless defined $present; | ||
| 238 | $output .= $present; | ||
| 239 | next; ## NEXT | ||
| 240 | } | ||
| 241 | elsif ( ref($item) eq 'HASH' | ||
| 242 | && defined($newtype = $item->{$method}) | ||
| 243 | && defined($template = $map->{"$method=>$newtype"})) { | ||
| 244 | } | ||
| 245 | elsif ( defined($newtype) | ||
| 246 | && defined($template = $map->{"$method=>*"}) ) { | ||
| 247 | $template =~ s/\*/$newtype/; | ||
| 248 | } | ||
| 249 | elsif (! ($template = $map->{ default }) ) { | ||
| 250 | # default not defined, so construct template name from type | ||
| 251 | ($template = $type) =~ s/\W+/_/g; | ||
| 252 | } | ||
| 253 | } | ||
| 254 | # else { | ||
| 255 | # $self->DEBUG("defined map type for $type: $template\n"); | ||
| 256 | # } | ||
| 257 | $self->DEBUG("printing view '", $template || '', "', $item\n") if $DEBUG; | ||
| 258 | $output .= $self->view($template, $item) | ||
| 259 | if $template; | ||
| 260 | } | ||
| 261 | return $output; | ||
| 262 | } | ||
| 263 | |||
| 264 | |||
| 265 | #------------------------------------------------------------------------ | ||
| 266 | # view($template, $item, \%vars) | ||
| 267 | # | ||
| 268 | # Wrapper around include() which expects a template name, $template, | ||
| 269 | # followed by a data item, $item, and optionally, a further hash array | ||
| 270 | # of template variables. The $item is added as an entry to the $vars | ||
| 271 | # hash (which is created empty if not passed as an argument) under the | ||
| 272 | # name specified by the internal 'item' member, which is appropriately | ||
| 273 | # 'item' by default. Thus an external object present() method can | ||
| 274 | # callback against this object method, simply passing a data item to | ||
| 275 | # be displayed. The external object doesn't have to know what the | ||
| 276 | # view expects the item to be called in the $vars hash. | ||
| 277 | #------------------------------------------------------------------------ | ||
| 278 | |||
| 279 | sub view { | ||
| 280 | my ($self, $template, $item) = splice(@_, 0, 3); | ||
| 281 | my $vars = ref $_[0] eq 'HASH' ? shift : { @_ }; | ||
| 282 | $vars->{ $self->{ item } } = $item if defined $item; | ||
| 283 | $self->include($template, $vars); | ||
| 284 | } | ||
| 285 | |||
| 286 | |||
| 287 | #------------------------------------------------------------------------ | ||
| 288 | # include($template, \%vars) | ||
| 289 | # | ||
| 290 | # INCLUDE a template, $template, mapped according to the current prefix, | ||
| 291 | # suffix, default, etc., where $vars is an optional hash reference | ||
| 292 | # containing template variable definitions. If the template isn't found | ||
| 293 | # then the method will default to any 'notfound' template, if defined | ||
| 294 | # as an internal item. | ||
| 295 | #------------------------------------------------------------------------ | ||
| 296 | |||
| 297 | sub include { | ||
| 298 | my ($self, $template, $vars) = @_; | ||
| 299 | my $context = $self->{ _CONTEXT }; | ||
| 300 | |||
| 301 | $template = $self->template($template); | ||
| 302 | |||
| 303 | $vars = { } unless ref $vars eq 'HASH'; | ||
| 304 | $vars->{ view } ||= $self; | ||
| 305 | |||
| 306 | $context->include( $template, $vars ); | ||
| 307 | |||
| 308 | # DEBUGGING | ||
| 309 | # my $out = $context->include( $template, $vars ); | ||
| 310 | # print STDERR "VIEW return [$out]\n"; | ||
| 311 | # return $out; | ||
| 312 | } | ||
| 313 | |||
| 314 | |||
| 315 | #------------------------------------------------------------------------ | ||
| 316 | # template($template) | ||
| 317 | # | ||
| 318 | # Returns a compiled template for the specified template name, according | ||
| 319 | # to the current configuration parameters. | ||
| 320 | #------------------------------------------------------------------------ | ||
| 321 | |||
| 322 | sub template { | ||
| 323 | my ($self, $name) = @_; | ||
| 324 | my $context = $self->{ _CONTEXT }; | ||
| 325 | return $context->throw( | ||
| 326 | Template::Constants::ERROR_VIEW, | ||
| 327 | "no view template specified" | ||
| 328 | ) unless $name; | ||
| 329 | |||
| 330 | my $notfound = $self->{ notfound }; | ||
| 331 | my $base = $self->{ base }; | ||
| 332 | my ($template, $block, $error); | ||
| 333 | |||
| 334 | return $block | ||
| 335 | if ($block = $self->{ _BLOCKS }->{ $name }); | ||
| 336 | |||
| 337 | # try the named template | ||
| 338 | $template = $self->template_name($name); | ||
| 339 | $self->DEBUG("looking for $template\n") if $DEBUG; | ||
| 340 | eval { $template = $context->template($template) }; | ||
| 341 | |||
| 342 | # try asking the base view if not found | ||
| 343 | if (($error = $@) && $base) { | ||
| 344 | $self->DEBUG("asking base for $name\n") if $DEBUG; | ||
| 345 | eval { $template = $base->template($name) }; | ||
| 346 | } | ||
| 347 | |||
| 348 | # try the 'notfound' template (if defined) if that failed | ||
| 349 | if (($error = $@) && $notfound) { | ||
| 350 | unless ($template = $self->{ _BLOCKS }->{ $notfound }) { | ||
| 351 | $notfound = $self->template_name($notfound); | ||
| 352 | $self->DEBUG("not found, looking for $notfound\n") if $DEBUG; | ||
| 353 | eval { $template = $context->template($notfound) }; | ||
| 354 | |||
| 355 | return $context->throw(Template::Constants::ERROR_VIEW, $error) | ||
| 356 | if $@; # return first error | ||
| 357 | } | ||
| 358 | } | ||
| 359 | elsif ($error) { | ||
| 360 | $self->DEBUG("no 'notfound'\n") | ||
| 361 | if $DEBUG; | ||
| 362 | return $context->throw(Template::Constants::ERROR_VIEW, $error); | ||
| 363 | } | ||
| 364 | return $template; | ||
| 365 | } | ||
| 366 | |||
| 367 | |||
| 368 | #------------------------------------------------------------------------ | ||
| 369 | # template_name($template) | ||
| 370 | # | ||
| 371 | # Returns the name of the specified template with any appropriate prefix | ||
| 372 | # and/or suffix added. | ||
| 373 | #------------------------------------------------------------------------ | ||
| 374 | |||
| 375 | sub template_name { | ||
| 376 | my ($self, $template) = @_; | ||
| 377 | $template = $self->{ prefix } . $template . $self->{ suffix } | ||
| 378 | if $template; | ||
| 379 | |||
| 380 | $self->DEBUG("template name: $template\n") if $DEBUG; | ||
| 381 | return $template; | ||
| 382 | } | ||
| 383 | |||
| 384 | |||
| 385 | #------------------------------------------------------------------------ | ||
| 386 | # default($val) | ||
| 387 | # | ||
| 388 | # Special case accessor to retrieve/update 'default' as an alias for | ||
| 389 | # '$map->{ default }'. | ||
| 390 | #------------------------------------------------------------------------ | ||
| 391 | |||
| 392 | sub default { | ||
| 393 | my $self = shift; | ||
| 394 | return @_ | ||
| 395 | ? ($self->{ map }->{ default } = shift) | ||
| 396 | : $self->{ map }->{ default }; | ||
| 397 | } | ||
| 398 | |||
| 399 | |||
| 400 | #------------------------------------------------------------------------ | ||
| 401 | # AUTOLOAD | ||
| 402 | # | ||
| 403 | # Returns/updates public internal data items (i.e. not prefixed '_' or | ||
| 404 | # '.') or presents a view if the method matches the view_prefix item, | ||
| 405 | # e.g. view_foo(...) => view('foo', ...). Similarly, the | ||
| 406 | # include_prefix is used, if defined, to map include_foo(...) to | ||
| 407 | # include('foo', ...). If that fails then the entire method name will | ||
| 408 | # be used as the name of a template to include iff the include_named | ||
| 409 | # parameter is set (default: 1). Last attempt is to match the entire | ||
| 410 | # method name to a view() call, iff view_naked is set. Otherwise, a | ||
| 411 | # 'view' exception is raised reporting the error "no such view member: | ||
| 412 | # $method". | ||
| 413 | #------------------------------------------------------------------------ | ||
| 414 | |||
| 415 | sub AUTOLOAD { | ||
| 416 | my $self = shift; | ||
| 417 | my $item = $AUTOLOAD; | ||
| 418 | $item =~ s/.*:://; | ||
| 419 | return if $item eq 'DESTROY'; | ||
| 420 | |||
| 421 | my $starts_with = substr($item,0,1); | ||
| 422 | if ($starts_with eq '.' || $starts_with eq '_') { | ||
| 423 | return $self->{ _CONTEXT }->throw(Template::Constants::ERROR_VIEW, | ||
| 424 | "attempt to view private member: $item"); | ||
| 425 | } | ||
| 426 | elsif (exists $self->{ $item }) { | ||
| 427 | # update existing config item (e.g. 'prefix') if unsealed | ||
| 428 | return $self->{ _CONTEXT }->throw(Template::Constants::ERROR_VIEW, | ||
| 429 | "cannot update config item in sealed view: $item") | ||
| 430 | if @_ && $self->{ SEALED }; | ||
| 431 | $self->DEBUG("accessing item: $item\n") if $DEBUG; | ||
| 432 | return @_ ? ($self->{ $item } = shift) : $self->{ $item }; | ||
| 433 | } | ||
| 434 | elsif (exists $self->{ data }->{ $item }) { | ||
| 435 | # get/update existing data item (must be unsealed to update) | ||
| 436 | if (@_ && $self->{ SEALED }) { | ||
| 437 | return $self->{ _CONTEXT }->throw( | ||
| 438 | Template::Constants::ERROR_VIEW, | ||
| 439 | "cannot update item in sealed view: $item" | ||
| 440 | ) unless $self->{ silent }; | ||
| 441 | # ignore args if silent | ||
| 442 | @_ = (); | ||
| 443 | } | ||
| 444 | $self->DEBUG(@_ | ||
| 445 | ? "updating data item: $item <= $_[0]\n" | ||
| 446 | : "returning data item: $item\n" | ||
| 447 | ) if $DEBUG; | ||
| 448 | return @_ | ||
| 449 | ? ($self->{ data }->{ $item } = shift) | ||
| 450 | : $self->{ data }->{ $item }; | ||
| 451 | } | ||
| 452 | elsif (@_ && ! $self->{ SEALED }) { | ||
| 453 | # set data item if unsealed | ||
| 454 | $self->DEBUG("setting unsealed data: $item => @_\n") if $DEBUG; | ||
| 455 | $self->{ data }->{ $item } = shift; | ||
| 456 | } | ||
| 457 | elsif ($item =~ s/^$self->{ view_prefix }//) { | ||
| 458 | $self->DEBUG("returning view($item)\n") if $DEBUG; | ||
| 459 | return $self->view($item, @_); | ||
| 460 | } | ||
| 461 | elsif ($item =~ s/^$self->{ include_prefix }//) { | ||
| 462 | $self->DEBUG("returning include($item)\n") if $DEBUG; | ||
| 463 | return $self->include($item, @_); | ||
| 464 | } | ||
| 465 | elsif ($self->{ include_naked }) { | ||
| 466 | $self->DEBUG("returning naked include($item)\n") if $DEBUG; | ||
| 467 | return $self->include($item, @_); | ||
| 468 | } | ||
| 469 | elsif ($self->{ view_naked }) { | ||
| 470 | $self->DEBUG("returning naked view($item)\n") if $DEBUG; | ||
| 471 | return $self->view($item, @_); | ||
| 472 | } | ||
| 473 | else { | ||
| 474 | return $self->{ _CONTEXT }->throw( | ||
| 475 | Template::Constants::ERROR_VIEW, | ||
| 476 | "no such view member: $item" | ||
| 477 | ); | ||
| 478 | } | ||
| 479 | } | ||
| 480 | |||
| 481 | |||
| 482 | 1; | ||
| 483 | |||
| 484 | |||
| 485 | __END__ |