Template::View(3)
β¨ NAME β¨
okay so this is Template::View(3), bestie β a User Contributed Perl Documentation page for Template::View, aka a customised view of a template processing context. That's the whole name vibe. We're making template contexts look specific and cute. Slay.
π¦ SYNOPSIS π¦
# define a view
[% VIEW view
# some standard args
prefix => 'my_',
suffix => '.tt2',
notfound => 'no_such_file'
...
# any other data
title => 'My View title'
other_item => 'Joe Random Data'
...
%]
# add new data definitions, via 'my' self reference
[% my.author = "$abw.name <$abw.email>" %]
[% my.copy = "© Copyright 2000 $my.author" %]
# define a local block
[% BLOCK header %]
This is the header block, title: [% title or my.title %]
[% END %]
[% END %]
# access data items for view
[% view.title %]
[% view.other_item %]
# access blocks directly ('include_naked' option, set by default)
[% view.header %]
[% view.header(title => 'New Title') %]
# non-local templates have prefix/suffix attached
[% view.footer %] # => [% INCLUDE my_footer.tt2 %]
# more verbose form of block access
[% view.include( 'header', title => 'The Header Title' ) %]
[% view.include_header( title => 'The Header Title' ) %]
# very short form of above ('include_naked' option, set by default)
[% view.header( title => 'The Header Title' ) %]
# non-local templates have prefix/suffix attached
[% view.footer %] # => [% INCLUDE my_footer.tt2 %]
# fallback on the 'notfound' template ('my_no_such_file.tt2')
# if template not found
[% view.include('missing') %]
[% view.include_missing %]
[% view.missing %]
# print() includes a template relevant to argument type
[% view.print("some text") %] # type=TEXT, template='text'
[% BLOCK my_text.tt2 %] # 'text' with prefix/suffix
Text: [% item %]
[% END %]
# now print() a hash ref, mapped to 'hash' template
[% view.print(some_hash_ref) %] # type=HASH, template='hash'
[% BLOCK my_hash.tt2 %] # 'hash' with prefix/suffix
hash keys: [% item.keys.sort.join(', ')
[% END %]
# now print() a list ref, mapped to 'list' template
[% view.print(my_list_ref) %] # type=ARRAY, template='list'
[% BLOCK my_list.tt2 %] # 'list' with prefix/suffix
list: [% item.join(', ') %]
[% END %]
# print() maps 'My::Object' to 'My_Object'
[% view.print(myobj) %]
[% BLOCK my_My_Object.tt2 %]
[% item.this %], [% item.that %]
[% END %]
# update mapping table
[% view.map.ARRAY = 'my_list_template' %]
[% view.map.TEXT = 'my_text_block' %]
# change prefix, suffix, item name, etc.
[% view.prefix = 'your_' %]
[% view.default = 'anyobj' %]
...
π DESCRIPTION π
The page literally says:
TODO
That's it. That's the description. Iconic unfinished manuscript energy. No extra lore, no secret notes β just TODO, and we respect the assignment.
π METHODS π
π§ new($context, \%config)
Creates a shiny new Template::View that presents a custom view of the $context object you handed it. You can also pass a reference to a hash array of configuration options as the second argument.
prefixβ Prefix added to all template names.
[% USE view(prefix => 'my_') %]
[% view.view('foo', a => 20) %] # => my_foo
suffixβ Suffix added to all template names.
[% USE view(suffix => '.tt2') %]
[% view.view('foo', a => 20) %] # => foo.tt2
mapβ Hash array mapping reference types to template names. Theprint()method uses this to determine which template to use to present any particular item. TheTEXT,HASHandARRAYitems default to'test','hash'and'list'appropriately.
[% USE view(map => { ARRAY => 'my_list',
HASH => 'your_hash',
My::Foo => 'my_foo', } ) %]
[% view.print(some_text) %] # => text
[% view.print(a_list) %] # => my_list
[% view.print(a_hash) %] # => your_hash
[% view.print(a_foo) %] # => my_foo
[% BLOCK text %]
Text: [% item %]
[% END %]
[% BLOCK my_list %]
list: [% item.join(', ') %]
[% END %]
[% BLOCK your_hash %]
hash keys: [% item.keys.sort.join(', ')
[% END %]
[% BLOCK my_foo %]
Foo: [% item.this %], [% item.that %]
[% END %]
methodβ Name of a method which objects passed toprint()may provide for presenting themselves to the view. If a specific map entry can't be found for an object reference and it supports the method (default:'present') then the method will be called, passing the view as an argument. The object can then make callbacks against the view to present itself.
package Foo;
sub present {
my ($self, $view) = @_;
return "a regular view of a Foo\n";
}
sub debug {
my ($self, $view) = @_;
return "a debug view of a Foo\n";
}
In a template:
[% USE view %]
[% view.print(my_foo_object) %] # a regular view of a Foo
[% USE view(method => 'debug') %]
[% view.print(my_foo_object) %] # a debug view of a Foo
defaultβ Default template to use if no specific map entry is found for an item.
[% USE view(default => 'my_object') %]
[% view.print(objref) %] # => my_object
If no map entry or default is provided then the view will attempt to construct a template name from the object class, substituting any sequence of non-word characters to single underscores, e.g.
# 'fubar' is an object of class Foo::Bar
[% view.print(fubar) %] # => Foo_Bar
Any current prefix and suffix will be added to both the default template name and any name constructed from the object class.
notfoundβ Fallback template to use if any other isn't found.itemβ Name of the template variable to which theprint()method assigns the current item. Defaults to'item'.
[% USE view %]
[% BLOCK list %]
[% item.join(', ') %]
[% END %]
[% view.print(a_list) %]
[% USE view(item => 'thing') %]
[% BLOCK list %]
[% thing.join(', ') %]
[% END %]
[% view.print(a_list) %]
view_prefixβ Prefix of methods which should be mapped toview()byAUTOLOAD. Defaults to'view_'.
[% USE view %]
[% view.view_header() %] # => view('header')
[% USE view(view_prefix => 'show_me_the_' %]
[% view.show_me_the_header() %] # => view('header')
view_nakedβ Flag to indicate if any attempt should be made to map method names to template names where they don't match theview_prefix. Defaults to0.
[% USE view(view_naked => 1) %]
[% view.header() %] # => view('header')
π
print( $obj1, $obj2, ... \%config)
TODO
π
view( $template, \%vars, \%config );
TODO
π AUTHOR π
This page is by Andy Wardley <abw@wardley.org> <http://wardley.org/>. Whole credit line, kept exact, no editing the icon.
π COPYRIGHT π
Copyright (C) 2000-2022 Andy Wardley. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π₯ SEE ALSO π₯
Template::Pluginβ go look, bestie.
okay bye bestie π β this is Template::View(3) from perl v5.42.0, dated 2024-06-23. mwah β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Template-Toolkit 3.102.0-bp160.1.13 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Template-Toolkit 3.102.0-bp160.1.13 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Template-Toolkit 3.102.0-bp160.1.13 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Template-Toolkit 3.102.0-bp160.1.13 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms