| 1 | 48 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 48 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 48 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Template::Declare; | ||||||||||||||||||||||||||||
| 6 | 48 | ▶ | use Template::Declare::Buffer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 48 | ▶ | use Class::ISA; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 48 | ▶ | use String::BufferStack; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 48 | our $VERSION = '0.47'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 48 | ▶ | use base 'Class::Data::Inheritable'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 48 | __PACKAGE__->mk_classdata('dispatch_to'); | |||||||||||||||||||||||||||
| 14 | 48 | __PACKAGE__->mk_classdata('postprocessor'); | |||||||||||||||||||||||||||
| 15 | 48 | __PACKAGE__->mk_classdata('strict'); | |||||||||||||||||||||||||||
| 16 | 48 | __PACKAGE__->mk_classdata('templates'); | |||||||||||||||||||||||||||
| 17 | 48 | __PACKAGE__->mk_classdata('private_templates'); | |||||||||||||||||||||||||||
| 18 | 48 | __PACKAGE__->mk_classdata('buffer'); | |||||||||||||||||||||||||||
| 19 | 48 | __PACKAGE__->mk_classdata('imported_into'); | |||||||||||||||||||||||||||
| 20 | 48 | __PACKAGE__->mk_classdata('around_template'); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 48 | __PACKAGE__->dispatch_to( [] ); | |||||||||||||||||||||||||||
| 23 | 426 | ▶ | __PACKAGE__->postprocessor( sub { return wantarray ? @_ : $_[0] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 48 | __PACKAGE__->templates( {} ); | |||||||||||||||||||||||||||
| 25 | 48 | __PACKAGE__->private_templates( {} ); | |||||||||||||||||||||||||||
| 26 | 48 | __PACKAGE__->buffer( String::BufferStack->new ); | |||||||||||||||||||||||||||
| 27 | 48 | __PACKAGE__->around_template( undef ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 72 | ▶ | *String::BufferStack::data = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 30 | 72 | my $ref = shift; | |||||||||||||||||||||||||||
| 31 | 72 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | warn "Template::Declare->buffer->data called with argument; this usage is deprecated"; | |||||||||||||||||||||||||||
| 33 | 0 | ${$ref->buffer_ref} = join("", @_); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 72 | return $ref->buffer; | |||||||||||||||||||||||||||
| 36 | 48 | }; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 48 | our $TEMPLATE_VARS; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 NAME | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Template::Declare - Perlish declarative templates | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Here's an example of basic HTML usage: | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | package MyApp::Templates; | ||||||||||||||||||||||||||||
| 49 | use Template::Declare::Tags; # defaults to 'HTML' | ||||||||||||||||||||||||||||
| 50 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | template simple => sub { | ||||||||||||||||||||||||||||
| 53 | html { | ||||||||||||||||||||||||||||
| 54 | head {} | ||||||||||||||||||||||||||||
| 55 | body { | ||||||||||||||||||||||||||||
| 56 | p { 'Hello, world wide web!' } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | package main; | ||||||||||||||||||||||||||||
| 62 | use Template::Declare; | ||||||||||||||||||||||||||||
| 63 | Template::Declare->init( dispatch_to => ['MyApp::Templates'] ); | ||||||||||||||||||||||||||||
| 64 | print Template::Declare->show( 'simple' ); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | And here's the output: | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | <html> | ||||||||||||||||||||||||||||
| 69 | <head></head> | ||||||||||||||||||||||||||||
| 70 | <body> | ||||||||||||||||||||||||||||
| 71 | <p>Hello, world wide web! | ||||||||||||||||||||||||||||
| 72 | </p> | ||||||||||||||||||||||||||||
| 73 | </body> | ||||||||||||||||||||||||||||
| 74 | </html> | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | C<Template::Declare> is a pure-Perl declarative HTML/XUL/RDF/XML templating | ||||||||||||||||||||||||||||
| 79 | system. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Yes. Another one. There are many others like it, but this one is ours. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | A few key features and buzzwords: | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =over | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =item * | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | All templates are 100% pure Perl code | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =item * | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Simple declarative syntax | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =item * | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | No angle brackets | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =item * | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | "Native" XML namespace and declaration support | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =item * | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Mixins | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =item * | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | Inheritance | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =item * | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Delegation | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =item * | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Public and private templates | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =back | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 GLOSSARY | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =over | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =item template class | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | A subclass of Template::Declare in which one or more templates are defined | ||||||||||||||||||||||||||||
| 128 | using the C<template> keyword, or that inherits templates from a super class. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =item template | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Created with the C<template> keyword, a template is a subroutine that uses | ||||||||||||||||||||||||||||
| 133 | C<tags> to generate output. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =item attribute | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | An XML element attribute. For example, in C<< <img src="foo.png" /> >>, C<src> | ||||||||||||||||||||||||||||
| 138 | is an attribute of the C<img> element. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =item tag | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | A subroutine that generates XML element-style output. Tag subroutines execute | ||||||||||||||||||||||||||||
| 143 | blocks that generate the output, and can call other tags to generate a | ||||||||||||||||||||||||||||
| 144 | properly hierarchical structure. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =item tag set | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | A collection of related tags defined in a subclass of | ||||||||||||||||||||||||||||
| 149 | L<Template::Declare::TagSet> for a particular purpose, and which can be | ||||||||||||||||||||||||||||
| 150 | imported into a template class. For example, | ||||||||||||||||||||||||||||
| 151 | L<Template::Declare::TagSet::HTML> defines tags for emitting HTML elements. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =item wrapper | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | A subroutine that wraps the output from a template. Useful for wrapping | ||||||||||||||||||||||||||||
| 156 | template output in common headers and footers, for example. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =item dispatch class | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | A template class that has been passed to L<C<init()>|/init> via the | ||||||||||||||||||||||||||||
| 161 | C<dispatch_to> parameter. When L<show|/"show TEMPLATE"> is called, only | ||||||||||||||||||||||||||||
| 162 | templates defined in or mixed into the dispatch classes will be executed. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item path | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | The name specified for a template when it is created by the C<template> | ||||||||||||||||||||||||||||
| 167 | keyword, or when a template is mixed into a template class. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =item mixin | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | A template mixed into a template class via L</mix>. Mixed-in templates may be | ||||||||||||||||||||||||||||
| 172 | mixed in under prefix paths to distinguish them from the templates defined in | ||||||||||||||||||||||||||||
| 173 | the dispatch classes. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =item alias | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | A template aliased into a template class via L</alias>. Aliased templates may | ||||||||||||||||||||||||||||
| 178 | be added under prefix paths to distinguish them from the templates defined in | ||||||||||||||||||||||||||||
| 179 | the dispatch classes. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =item package variable | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | Variables defined when mixing templates into a template class. These variables | ||||||||||||||||||||||||||||
| 184 | are available only to the mixed-in templates; they are not even accessible | ||||||||||||||||||||||||||||
| 185 | from the template class in which the templates were defined. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =item helper | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | A subroutine used in templates to assist in the generation of output, or in | ||||||||||||||||||||||||||||
| 190 | template classes to assist in the mixing-in of templates. Output helpers | ||||||||||||||||||||||||||||
| 191 | include C<outs()> for rending text output and C<xml_decl()> for rendering XML | ||||||||||||||||||||||||||||
| 192 | declarations. Mixin helpers include C<into> for specifying a template class to | ||||||||||||||||||||||||||||
| 193 | mix into, and C<under> for specifying a path prefix under which to mix | ||||||||||||||||||||||||||||
| 194 | templates. | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =back | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 USAGE | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Like other Perl templating systems, there are two parts to Template::Declare: | ||||||||||||||||||||||||||||
| 201 | the templates and the code that loads and executes the templates. Unlike other | ||||||||||||||||||||||||||||
| 202 | template systems, the templates are written in Perl classes. A simple HTML | ||||||||||||||||||||||||||||
| 203 | example is in the L</SYNOPSIS>. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head2 A slightly more advanced example | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | In this example, we'll show off how to set attributes on HTML tags, how to | ||||||||||||||||||||||||||||
| 208 | call other templates, and how to declare a I<private> template that can't be | ||||||||||||||||||||||||||||
| 209 | called directly. We'll also show passing arguments to templates. First, the | ||||||||||||||||||||||||||||
| 210 | template class: | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | package MyApp::Templates; | ||||||||||||||||||||||||||||
| 213 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 214 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | private template 'util/header' => sub { | ||||||||||||||||||||||||||||
| 217 | head { | ||||||||||||||||||||||||||||
| 218 | title { 'This is a webpage' }; | ||||||||||||||||||||||||||||
| 219 | meta { | ||||||||||||||||||||||||||||
| 220 | attr { generator => "This is not your father's frontpage" } | ||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | }; | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | private template 'util/footer' => sub { | ||||||||||||||||||||||||||||
| 226 | my $self = shift; | ||||||||||||||||||||||||||||
| 227 | my $time = shift || gmtime; | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | div { | ||||||||||||||||||||||||||||
| 230 | attr { id => "footer"}; | ||||||||||||||||||||||||||||
| 231 | "Page last generated at $time." | ||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | }; | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | template simple => sub { | ||||||||||||||||||||||||||||
| 236 | my $self = shift; | ||||||||||||||||||||||||||||
| 237 | my $user = shift || 'world wide web'; | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | html { | ||||||||||||||||||||||||||||
| 240 | show('util/header'); | ||||||||||||||||||||||||||||
| 241 | body { | ||||||||||||||||||||||||||||
| 242 | img { src is 'hello.jpg' } | ||||||||||||||||||||||||||||
| 243 | p { | ||||||||||||||||||||||||||||
| 244 | attr { class => 'greeting'}; | ||||||||||||||||||||||||||||
| 245 | "Hello, $user!" | ||||||||||||||||||||||||||||
| 246 | }; | ||||||||||||||||||||||||||||
| 247 | }; | ||||||||||||||||||||||||||||
| 248 | show('util/footer', 'noon'); | ||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | }; | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | A few notes on this example: | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =over | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | =item * | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | Since no parameter was passed to C<use Template::Declare::Tags>, the HTML tags | ||||||||||||||||||||||||||||
| 259 | are imported by default. | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =item * | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | The C<private> keyword indicates that a template is private. That means that | ||||||||||||||||||||||||||||
| 264 | it can only be executed by other templates within the template class in which | ||||||||||||||||||||||||||||
| 265 | it's declared. By default, C<< Template::Declare->show >> will not dispatch to | ||||||||||||||||||||||||||||
| 266 | it. | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =item * | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | The two private templates have longer paths than we've seen before: | ||||||||||||||||||||||||||||
| 271 | C<util/header> and C<util/footer>. They must of course be called by their full | ||||||||||||||||||||||||||||
| 272 | path names. You can put any characters you like into template names, but the | ||||||||||||||||||||||||||||
| 273 | use of Unix filesystem-style paths is the most common (following on the | ||||||||||||||||||||||||||||
| 274 | example of L<HTML::Mason|HTML::Mason>). | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =item * | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | The first argument to a template is a class name. This can be useful for | ||||||||||||||||||||||||||||
| 279 | calling methods defined in the class. | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =item * | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | The C<show> sub executes another template. In this example, the C<simple> | ||||||||||||||||||||||||||||
| 284 | template calls C<show('util/header')> and C<show('util/footer')> in order to | ||||||||||||||||||||||||||||
| 285 | execute those private templates in the appropriate places. | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =item * | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | Additional arguments to C<show> are passed on to the template being executed. | ||||||||||||||||||||||||||||
| 290 | Here, C<show('util/footer', 'noon')> is passing "noon" to the C<util/footer> | ||||||||||||||||||||||||||||
| 291 | template, with the result that the "last generated at" string will display | ||||||||||||||||||||||||||||
| 292 | "noon" instead of the default C<gmtime>. | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | =item * | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | In the same way, note that the C<simple> template expects an additional | ||||||||||||||||||||||||||||
| 297 | argument, a user name. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =item * | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | In addition to using C<attr> to declare attributes for an element, you can | ||||||||||||||||||||||||||||
| 302 | use C<is>, as in | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | img { src is 'hello.jpg' } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =back | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | Now for executing the template: | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | package main; | ||||||||||||||||||||||||||||
| 311 | use Template::Declare; | ||||||||||||||||||||||||||||
| 312 | Template::Declare->init( dispatch_to => ['MyApp::Templates'] ); | ||||||||||||||||||||||||||||
| 313 | print Template::Declare->show( '/simple', 'TD user'); | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | We've told Template::Declare to dispatch to templates defined in our template | ||||||||||||||||||||||||||||
| 316 | class. And note how an additional argument is passed to C<show()>; that | ||||||||||||||||||||||||||||
| 317 | argument, "TD user", will be passed to the C<simple> template, where it will | ||||||||||||||||||||||||||||
| 318 | be used in the C<$user> variable. | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | The output looks like this: | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | <html> | ||||||||||||||||||||||||||||
| 323 | <head> | ||||||||||||||||||||||||||||
| 324 | <title>This is a webpage</title> | ||||||||||||||||||||||||||||
| 325 | <meta generator="This is not your father's frontpage" /> | ||||||||||||||||||||||||||||
| 326 | </head> | ||||||||||||||||||||||||||||
| 327 | <body> | ||||||||||||||||||||||||||||
| 328 | <img src="hello.jpg" /> | ||||||||||||||||||||||||||||
| 329 | <p class="greeting">Hello, TD user!</p> | ||||||||||||||||||||||||||||
| 330 | </body> | ||||||||||||||||||||||||||||
| 331 | <div id="footer">Page last generated at Thu Sep 3 20:56:14 2009.</div> | ||||||||||||||||||||||||||||
| 332 | </html> | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | Note that the single quote in C<father's> was quoted for you. We sanitize | ||||||||||||||||||||||||||||
| 335 | your output for you to help prevent cross-site scripting attacks. | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | =head2 XUL | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | Template::Declare isn't limited to just HTML. Let's do XUL! | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | package MyApp::Templates; | ||||||||||||||||||||||||||||
| 342 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 343 | use Template::Declare::Tags 'XUL'; | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | template main => sub { | ||||||||||||||||||||||||||||
| 346 | xml_decl { 'xml', version => '1.0' }; | ||||||||||||||||||||||||||||
| 347 | xml_decl { | ||||||||||||||||||||||||||||
| 348 | 'xml-stylesheet', | ||||||||||||||||||||||||||||
| 349 | href => "chrome://global/skin/", | ||||||||||||||||||||||||||||
| 350 | type => "text/css" | ||||||||||||||||||||||||||||
| 351 | }; | ||||||||||||||||||||||||||||
| 352 | groupbox { | ||||||||||||||||||||||||||||
| 353 | caption { attr { label => 'Colors' } } | ||||||||||||||||||||||||||||
| 354 | radiogroup { | ||||||||||||||||||||||||||||
| 355 | for my $id ( qw< orange violet yellow > ) { | ||||||||||||||||||||||||||||
| 356 | radio { | ||||||||||||||||||||||||||||
| 357 | attr { | ||||||||||||||||||||||||||||
| 358 | id => $id, | ||||||||||||||||||||||||||||
| 359 | label => ucfirst($id), | ||||||||||||||||||||||||||||
| 360 | $id eq 'violet' ? (selected => 'true') : () | ||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | } # for | ||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | }; | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | The first thing to do in a template class is to subclass Template::Declare | ||||||||||||||||||||||||||||
| 369 | itself. This is required so that Template::Declare always knows that it's | ||||||||||||||||||||||||||||
| 370 | dealing with templates. The second thing is to C<use Template::Declare::Tags> | ||||||||||||||||||||||||||||
| 371 | to import the set of tag subroutines you need to generate the output you want. | ||||||||||||||||||||||||||||
| 372 | In this case, we've imported tags to support the creation of XUL. Other tag | ||||||||||||||||||||||||||||
| 373 | sets include HTML (the default), and RDF. | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | Templates are created using the C<template> keyword: | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | template main => sub { ... }; | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | The first argument is the name of the template, also known as its I<path>. In | ||||||||||||||||||||||||||||
| 380 | this case, the template's path is C<main> (or C</main>, both are allowed (to | ||||||||||||||||||||||||||||
| 381 | keep both PHP and L<HTML::Mason> fans happy). The second argument is an | ||||||||||||||||||||||||||||
| 382 | anonymous subroutine that uses the tag subs (and any other necessary code) to | ||||||||||||||||||||||||||||
| 383 | generate the output for the template. | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | The tag subs imported into your class take blocks as arguments, while a | ||||||||||||||||||||||||||||
| 386 | number of helper subs take other arguments. For example, the C<xml_decl> | ||||||||||||||||||||||||||||
| 387 | helper takes as its first argument the name of the XML declaration to be | ||||||||||||||||||||||||||||
| 388 | output, and then a hash of the attributes of that declaration: | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | xml_decl { 'xml', version => '1.0' }; | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | Tag subs are used by simply passing a block to them that generates the output. | ||||||||||||||||||||||||||||
| 393 | Said block may of course execute other tag subs in order to represent the | ||||||||||||||||||||||||||||
| 394 | hierarchy required in your output. Here, the C<radiogroup> tag calls the | ||||||||||||||||||||||||||||
| 395 | C<radio> tag for each of three different colors: | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | radiogroup { | ||||||||||||||||||||||||||||
| 398 | for my $id ( qw< orange violet yellow > ) { | ||||||||||||||||||||||||||||
| 399 | radio { | ||||||||||||||||||||||||||||
| 400 | attr { | ||||||||||||||||||||||||||||
| 401 | id => $id, | ||||||||||||||||||||||||||||
| 402 | label => ucfirst($id), | ||||||||||||||||||||||||||||
| 403 | $id eq 'violet' ? (selected => 'true') : () | ||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | } # for | ||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | Note the C<attr> sub. This helper function is used to add attributes to the | ||||||||||||||||||||||||||||
| 410 | element created by the tag in which they appear. In the previous example, the | ||||||||||||||||||||||||||||
| 411 | C<id>, C<label>, and C<selected> attributes are added to each C<radio> | ||||||||||||||||||||||||||||
| 412 | output. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | Once you've written your templates, you'll want to execute them. You do so by | ||||||||||||||||||||||||||||
| 415 | telling Template::Declare what template classes to dispatch to and then asking | ||||||||||||||||||||||||||||
| 416 | it to show you the output from a template: | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | package main; | ||||||||||||||||||||||||||||
| 419 | Template::Declare->init( dispatch_to => ['MyApp::Templates'] ); | ||||||||||||||||||||||||||||
| 420 | print Template::Declare->show( 'main' ); | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | The path passed to C<show> can be either C<main> or </main>, as you prefer. In | ||||||||||||||||||||||||||||
| 423 | either event, the output would look like this: | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | <?xml version="1.0"?> | ||||||||||||||||||||||||||||
| 426 | <?xml-stylesheet href="chrome://global/skin/" type="text/css"?> | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | <groupbox> | ||||||||||||||||||||||||||||
| 429 | <caption label="Colors" /> | ||||||||||||||||||||||||||||
| 430 | <radiogroup> | ||||||||||||||||||||||||||||
| 431 | <radio id="orange" label="Orange" /> | ||||||||||||||||||||||||||||
| 432 | <radio id="violet" label="Violet" selected="true" /> | ||||||||||||||||||||||||||||
| 433 | <radio id="yellow" label="Yellow" /> | ||||||||||||||||||||||||||||
| 434 | </radiogroup> | ||||||||||||||||||||||||||||
| 435 | </groupbox> | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =head2 Postprocessing | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | Sometimes you just want simple syntax for inline elements. The following shows | ||||||||||||||||||||||||||||
| 440 | how to use a postprocessor to emphasize text _like this_. | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | package MyApp::Templates; | ||||||||||||||||||||||||||||
| 443 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 444 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | template before => sub { | ||||||||||||||||||||||||||||
| 447 | h1 { | ||||||||||||||||||||||||||||
| 448 | outs "Welcome to "; | ||||||||||||||||||||||||||||
| 449 | em { "my" }; | ||||||||||||||||||||||||||||
| 450 | outs " site. It's "; | ||||||||||||||||||||||||||||
| 451 | em { "great" }; | ||||||||||||||||||||||||||||
| 452 | outs "!"; | ||||||||||||||||||||||||||||
| 453 | }; | ||||||||||||||||||||||||||||
| 454 | }; | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | template after => sub { | ||||||||||||||||||||||||||||
| 457 | h1 { "Welcome to _my_ site. It's _great_!" }; | ||||||||||||||||||||||||||||
| 458 | h2 { outs_raw "This is _not_ emphasized." }; | ||||||||||||||||||||||||||||
| 459 | img { src is '/foo/_bar_baz.png' }; | ||||||||||||||||||||||||||||
| 460 | }; | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | Here we've defined two templates in our template class, with the paths | ||||||||||||||||||||||||||||
| 463 | C<before> and C<after>. The one new thing to note is the use of the C<outs> | ||||||||||||||||||||||||||||
| 464 | and C<outs_raw> subs. C<outs> XML-encodes its argument and outputs it. You can | ||||||||||||||||||||||||||||
| 465 | also just specify a string to be output within a tag call, but if you need to | ||||||||||||||||||||||||||||
| 466 | mix tags and plain text within a tag call, as in the C<before> template here, | ||||||||||||||||||||||||||||
| 467 | you'll need to use C<outs> to get things to output as you would expect. | ||||||||||||||||||||||||||||
| 468 | C<outs_raw> is the same, except that it does no XML encoding. | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | Now let's have a look at how we use these templates with a post-processor: | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | package main; | ||||||||||||||||||||||||||||
| 473 | use Template::Declare; | ||||||||||||||||||||||||||||
| 474 | Template::Declare->init( | ||||||||||||||||||||||||||||
| 475 | dispatch_to => ['MyApp::Templates'], | ||||||||||||||||||||||||||||
| 476 | postprocessor => \&emphasize, | ||||||||||||||||||||||||||||
| 477 | strict => 1, | ||||||||||||||||||||||||||||
| 478 | ); | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | print Template::Declare->show( 'before' ); | ||||||||||||||||||||||||||||
| 481 | print Template::Declare->show( 'after' ); | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | sub emphasize { | ||||||||||||||||||||||||||||
| 484 | my $text = shift; | ||||||||||||||||||||||||||||
| 485 | $text =~ s{_(.+?)_}{<em>$1</em>}g; | ||||||||||||||||||||||||||||
| 486 | return $text; | ||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | As usual, we've told Template::Declare to dispatch to our template class. A | ||||||||||||||||||||||||||||
| 490 | new parameter to C<init()> is C<postprocessor>, which is a code reference that | ||||||||||||||||||||||||||||
| 491 | should expect the template output as an argument. It can then transform that | ||||||||||||||||||||||||||||
| 492 | text however it sees fit before returning it for final output. In this | ||||||||||||||||||||||||||||
| 493 | example, the C<emphasize> subroutine looks for text that's emphasized using | ||||||||||||||||||||||||||||
| 494 | _underscores_ and turns them into C<< <em>emphasis</em> >> HTML elements. | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | We then execute both the C<before> and the C<after> templates with the output | ||||||||||||||||||||||||||||
| 497 | ending up as: | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | <h1>Welcome to | ||||||||||||||||||||||||||||
| 500 | <em>my</em> site. It's | ||||||||||||||||||||||||||||
| 501 | <em>great</em>!</h1> | ||||||||||||||||||||||||||||
| 502 | <h1>Welcome to <em>my</em> site. It's <em>great</em>!</h1> | ||||||||||||||||||||||||||||
| 503 | <h2>This is _not_ emphasized.</h2> | ||||||||||||||||||||||||||||
| 504 | <img src="/foo/_bar_baz.png" /> | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | The thing to note here is that text passed to C<outs_raw> is not passed | ||||||||||||||||||||||||||||
| 507 | through the postprocessor, and neither are attribute values (like the C<img>'s | ||||||||||||||||||||||||||||
| 508 | C<src>). | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | =head2 Inheritance | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | Templates are really just methods. You can subclass your template packages to | ||||||||||||||||||||||||||||
| 513 | override some of those methods: | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | package MyApp::Templates::GenericItem; | ||||||||||||||||||||||||||||
| 516 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 517 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | template 'list' => sub { | ||||||||||||||||||||||||||||
| 520 | my ($self, @items) = @_; | ||||||||||||||||||||||||||||
| 521 | div { | ||||||||||||||||||||||||||||
| 522 | show('item', $_) for @items; | ||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||
| 524 | }; | ||||||||||||||||||||||||||||
| 525 | template 'item' => sub { | ||||||||||||||||||||||||||||
| 526 | my ($self, $item) = @_; | ||||||||||||||||||||||||||||
| 527 | span { $item } | ||||||||||||||||||||||||||||
| 528 | }; | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | package MyApp::Templates::BlogPost; | ||||||||||||||||||||||||||||
| 531 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 532 | use base 'MyApp::Templates::GenericItem'; | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | template 'item' => sub { | ||||||||||||||||||||||||||||
| 535 | my ($self, $post) = @_; | ||||||||||||||||||||||||||||
| 536 | h1 { $post->title } | ||||||||||||||||||||||||||||
| 537 | div { $post->body } | ||||||||||||||||||||||||||||
| 538 | }; | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | Here we have two template classes; the second, C<MyApp::Templates::BlogPost>, | ||||||||||||||||||||||||||||
| 541 | inherits from the first, C<MyApp::Templates::GenericItem>. Note also that | ||||||||||||||||||||||||||||
| 542 | C<MyApp::Templates::BlogPost> overrides the C<item> template. So execute these | ||||||||||||||||||||||||||||
| 543 | templates: | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | package main; | ||||||||||||||||||||||||||||
| 546 | use Template::Declare; | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | Template::Declare->init( dispatch_to => ['MyApp::Templates::GenericItem'] ); | ||||||||||||||||||||||||||||
| 549 | print Template::Declare->show( 'list', 'foo', 'bar', 'baz' ); | ||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | Template::Declare->init( dispatch_to => ['MyApp::Templates::BlogPost'] ); | ||||||||||||||||||||||||||||
| 552 | my $post = My::Post->new(title => 'Hello', body => 'first post'); | ||||||||||||||||||||||||||||
| 553 | print Template::Declare->show( 'item', $post ); | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | First we execute the C<list> template in the base class, passing in some | ||||||||||||||||||||||||||||
| 556 | items, and then we re-C<init()> Template::Declare and execute I<its> C<list> | ||||||||||||||||||||||||||||
| 557 | template with an appropriate argument. Here's the output: | ||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | <div> | ||||||||||||||||||||||||||||
| 560 | <span>foo</span> | ||||||||||||||||||||||||||||
| 561 | <span>bar</span> | ||||||||||||||||||||||||||||
| 562 | <span>baz</span> | ||||||||||||||||||||||||||||
| 563 | </div> | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | <h1>Hello</h1> | ||||||||||||||||||||||||||||
| 566 | <div>first post</div> | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | So the override of the C<list> template in the subclass works as expected. For | ||||||||||||||||||||||||||||
| 569 | another example, see L<Jifty::View::Declare::CRUD>. | ||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | =head2 Wrappers | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | There are two levels of wrappers in Template::Declare: template wrappers and | ||||||||||||||||||||||||||||
| 574 | smart tag wrappers. | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | =head3 Template Wrappers | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | C<create_wrapper> declares a wrapper subroutine that can be called like a tag | ||||||||||||||||||||||||||||
| 579 | sub, but can optionally take arguments to be passed to the wrapper sub. For | ||||||||||||||||||||||||||||
| 580 | example, if you wanted to wrap all of the output of a template in the usual | ||||||||||||||||||||||||||||
| 581 | HTML headers and footers, you can do something like this: | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | package MyApp::Templates; | ||||||||||||||||||||||||||||
| 584 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 585 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | BEGIN { | ||||||||||||||||||||||||||||
| 588 | create_wrapper wrap => sub { | ||||||||||||||||||||||||||||
| 589 | my $code = shift; | ||||||||||||||||||||||||||||
| 590 | my %params = @_; | ||||||||||||||||||||||||||||
| 591 | html { | ||||||||||||||||||||||||||||
| 592 | head { title { outs "Hello, $params{user}!"} }; | ||||||||||||||||||||||||||||
| 593 | body { | ||||||||||||||||||||||||||||
| 594 | $code->(); | ||||||||||||||||||||||||||||
| 595 | div { outs 'This is the end, my friend' }; | ||||||||||||||||||||||||||||
| 596 | }; | ||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||
| 598 | }; | ||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | template inner => sub { | ||||||||||||||||||||||||||||
| 602 | wrap { | ||||||||||||||||||||||||||||
| 603 | h1 { outs "Hello, Jesse, s'up?" }; | ||||||||||||||||||||||||||||
| 604 | } user => 'Jesse'; | ||||||||||||||||||||||||||||
| 605 | }; | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | Note how the C<wrap> wrapper function is available for calling after it has | ||||||||||||||||||||||||||||
| 608 | been declared in a C<BEGIN> block. Also note how you can pass arguments to the | ||||||||||||||||||||||||||||
| 609 | function after the closing brace (you don't need a comma there!). | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | The output from the "inner" template will look something like this: | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | <html> | ||||||||||||||||||||||||||||
| 614 | <head> | ||||||||||||||||||||||||||||
| 615 | <title>Hello, Jesse!</title> | ||||||||||||||||||||||||||||
| 616 | </head> | ||||||||||||||||||||||||||||
| 617 | <body> | ||||||||||||||||||||||||||||
| 618 | <h1>Hello, Jesse, s'up?</h1> | ||||||||||||||||||||||||||||
| 619 | <div>This is the end, my friend</div> | ||||||||||||||||||||||||||||
| 620 | </body> | ||||||||||||||||||||||||||||
| 621 | </html> | ||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | =head3 Tag Wrappers | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | Tag wrappers are similar to template wrappers, but mainly function as syntax | ||||||||||||||||||||||||||||
| 626 | sugar for creating subroutines that behave just like tags but are allowed to | ||||||||||||||||||||||||||||
| 627 | contain arbitrary Perl code and to dispatch to other tags. To create one, | ||||||||||||||||||||||||||||
| 628 | simply create a named subroutine with the prototype C<(&)> so that its | ||||||||||||||||||||||||||||
| 629 | interface is the same as tags. Within it, use | ||||||||||||||||||||||||||||
| 630 | L<C<smart_tag_wrapper>|Template::Declare::Tags/"smart_tag_wrapper"> to do the | ||||||||||||||||||||||||||||
| 631 | actual execution, like so: | ||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | package My::Template; | ||||||||||||||||||||||||||||
| 634 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 635 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | sub myform (&) { | ||||||||||||||||||||||||||||
| 638 | my $code = shift; | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | smart_tag_wrapper { | ||||||||||||||||||||||||||||
| 641 | my %params = @_; # set using 'with' | ||||||||||||||||||||||||||||
| 642 | form { | ||||||||||||||||||||||||||||
| 643 | attr { %{ $params{attr} } }; | ||||||||||||||||||||||||||||
| 644 | $code->(); | ||||||||||||||||||||||||||||
| 645 | input { attr { type => 'submit', value => $params{value} } }; | ||||||||||||||||||||||||||||
| 646 | }; | ||||||||||||||||||||||||||||
| 647 | }; | ||||||||||||||||||||||||||||
| 648 | } | ||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | template edit_prefs => sub { | ||||||||||||||||||||||||||||
| 651 | with( | ||||||||||||||||||||||||||||
| 652 | attr => { id => 'edit_prefs', action => 'edit.html' }, | ||||||||||||||||||||||||||||
| 653 | value => 'Save' | ||||||||||||||||||||||||||||
| 654 | ), myform { | ||||||||||||||||||||||||||||
| 655 | label { 'Time Zone' }; | ||||||||||||||||||||||||||||
| 656 | input { type is 'text'; name is 'tz' }; | ||||||||||||||||||||||||||||
| 657 | }; | ||||||||||||||||||||||||||||
| 658 | }; | ||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||
| 660 | Note in the C<edit_prefs> template that we've used | ||||||||||||||||||||||||||||
| 661 | L<C<with>|Template::Declare::Tags/"with"> to set up parameters to be passed to | ||||||||||||||||||||||||||||
| 662 | the smart wrapper. C<smart_tag_wrapper()> is the device that allows you to | ||||||||||||||||||||||||||||
| 663 | receive those parameters, and also handles the magic of making sure that the | ||||||||||||||||||||||||||||
| 664 | tags you execute within it are properly output. Here we've used C<myform> | ||||||||||||||||||||||||||||
| 665 | similarly to C<form>, only C<myform> does something different with the | ||||||||||||||||||||||||||||
| 666 | C<with()> arguments and outputs a submit element. | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | Executing this template: | ||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||
| 670 | Template::Declare->init( dispatch_to => ['My::Template'] ); | ||||||||||||||||||||||||||||
| 671 | print Template::Declare->show('edit_prefs'); | ||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | Yields this output: | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | <form action="edit.html" id="edit_prefs"> | ||||||||||||||||||||||||||||
| 676 | <label>Time Zone</label> | ||||||||||||||||||||||||||||
| 677 | <input type="text" name="tz" /> | ||||||||||||||||||||||||||||
| 678 | <input type="submit" value="Save" /> | ||||||||||||||||||||||||||||
| 679 | </form> | ||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||
| 681 | =head2 Class Search Dispatching | ||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | The classes passed via the C<dispatch_to> parameter to C<init()> specify all | ||||||||||||||||||||||||||||
| 684 | of the templates that can be executed by subsequent calls to C<show()>. | ||||||||||||||||||||||||||||
| 685 | Template::Declare searches through these classes in order to find those | ||||||||||||||||||||||||||||
| 686 | templates. Thus it can be useful, when you're creating your template classes | ||||||||||||||||||||||||||||
| 687 | and determining which to use for particular class to C<show()>, to have | ||||||||||||||||||||||||||||
| 688 | templates that override other templates. This is similar to how an operating | ||||||||||||||||||||||||||||
| 689 | system will search all the paths in the C<$PATH> environment variable for a | ||||||||||||||||||||||||||||
| 690 | program to run, and to L<HTML::Mason> component roots or L<Template::Toolkit>'s | ||||||||||||||||||||||||||||
| 691 | C<INCLUDE_PATH> parameter. | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | For example, say you have this template class that defines a template that | ||||||||||||||||||||||||||||
| 694 | you'll use for displaying images on your Web site. | ||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||
| 696 | package MyApp::UI::Standard; | ||||||||||||||||||||||||||||
| 697 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 698 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||
| 700 | template image => sub { | ||||||||||||||||||||||||||||
| 701 | my ($self, $src, $title) = @_; | ||||||||||||||||||||||||||||
| 702 | img { | ||||||||||||||||||||||||||||
| 703 | src is $src; | ||||||||||||||||||||||||||||
| 704 | title is $title; | ||||||||||||||||||||||||||||
| 705 | }; | ||||||||||||||||||||||||||||
| 706 | }; | ||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||
| 708 | As usual, you can use it like so: | ||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||
| 710 | my @template_classes = 'MyApp::UI::Standard'; | ||||||||||||||||||||||||||||
| 711 | Template::Declare->init( dispatch_to => \@template_classes ); | ||||||||||||||||||||||||||||
| 712 | print Template::Declare->show('image', 'foo.png', 'Foo'); | ||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||
| 714 | We're explicitly using a reference to C<@template_classes> so that we can | ||||||||||||||||||||||||||||
| 715 | manage this list ourselves. | ||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||
| 717 | The output of this will be: | ||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||
| 719 | <div class="std"> | ||||||||||||||||||||||||||||
| 720 | <img src="foo.png" title="Foo" /> | ||||||||||||||||||||||||||||
| 721 | <p class="caption"></p> | ||||||||||||||||||||||||||||
| 722 | </div> | ||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||
| 724 | But say that in some sections of your site you need to have a more formal | ||||||||||||||||||||||||||||
| 725 | treatment of your photos. Maybe you publish photos from a wire service and | ||||||||||||||||||||||||||||
| 726 | need to provide an appropriate credit. You might write the template class like | ||||||||||||||||||||||||||||
| 727 | so: | ||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||
| 729 | package MyApp::UI::Formal; | ||||||||||||||||||||||||||||
| 730 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 731 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||
| 733 | template image => sub { | ||||||||||||||||||||||||||||
| 734 | my ($self, $src, $title, $credit, $caption) = @_; | ||||||||||||||||||||||||||||
| 735 | div { | ||||||||||||||||||||||||||||
| 736 | class is 'formal'; | ||||||||||||||||||||||||||||
| 737 | img { | ||||||||||||||||||||||||||||
| 738 | src is $src; | ||||||||||||||||||||||||||||
| 739 | title is $title; | ||||||||||||||||||||||||||||
| 740 | }; | ||||||||||||||||||||||||||||
| 741 | p { | ||||||||||||||||||||||||||||
| 742 | class is 'credit'; | ||||||||||||||||||||||||||||
| 743 | outs "Photo by $credit"; | ||||||||||||||||||||||||||||
| 744 | }; | ||||||||||||||||||||||||||||
| 745 | p { | ||||||||||||||||||||||||||||
| 746 | class is 'caption'; | ||||||||||||||||||||||||||||
| 747 | outs $caption; | ||||||||||||||||||||||||||||
| 748 | }; | ||||||||||||||||||||||||||||
| 749 | }; | ||||||||||||||||||||||||||||
| 750 | }; | ||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||
| 753 | This, too, will work as expected, but the useful bit comes in when you're | ||||||||||||||||||||||||||||
| 754 | mixing and matching template classes to pass to C<dispatch_to> before | ||||||||||||||||||||||||||||
| 755 | rendering a page. Maybe you always pass C<MyApp::UI::Standard> to | ||||||||||||||||||||||||||||
| 756 | C<dispatch_to> because it has all of your standard formatting templates. | ||||||||||||||||||||||||||||
| 757 | But when the code realizes that a particular page needs the more formal | ||||||||||||||||||||||||||||
| 758 | treatment, you can prepend the formal class to the list: | ||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||
| 760 | unshift @template_classes, 'MyApp::UI::Formal'; | ||||||||||||||||||||||||||||
| 761 | print Template::Declare->show( | ||||||||||||||||||||||||||||
| 762 | 'image', | ||||||||||||||||||||||||||||
| 763 | 'ap.png', | ||||||||||||||||||||||||||||
| 764 | 'AP Photo', | ||||||||||||||||||||||||||||
| 765 | 'Clark Kent', | ||||||||||||||||||||||||||||
| 766 | 'Big news' | ||||||||||||||||||||||||||||
| 767 | ); | ||||||||||||||||||||||||||||
| 768 | shift @template_classes; | ||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||
| 770 | In this way, the formal C<image> template will be found first, yielding | ||||||||||||||||||||||||||||
| 771 | this output: | ||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||
| 773 | <div class="formal"> | ||||||||||||||||||||||||||||
| 774 | <img src="ap.png" title="AP Photo" /> | ||||||||||||||||||||||||||||
| 775 | <p class="credit">Photo by Clark Kent</p> | ||||||||||||||||||||||||||||
| 776 | <p class="caption">Big news</p> | ||||||||||||||||||||||||||||
| 777 | </div> | ||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||
| 779 | At the end, we've shifted the formal template class off the C<dispatch_to> | ||||||||||||||||||||||||||||
| 780 | list in order to restore the template classes to the default configuration, | ||||||||||||||||||||||||||||
| 781 | ready for the next request. | ||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||
| 783 | =head2 Template Composition | ||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||
| 785 | There are two methods of template composition: mixins and delegation. Their | ||||||||||||||||||||||||||||
| 786 | interfaces are very similar, the only difference being the template invocant. | ||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||
| 788 | =head3 Mixins | ||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||
| 790 | Let's start with a mixin. | ||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||
| 792 | package MyApp::UtilTemplates; | ||||||||||||||||||||||||||||
| 793 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 794 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||
| 796 | template content => sub { | ||||||||||||||||||||||||||||
| 797 | my $self = shift; | ||||||||||||||||||||||||||||
| 798 | my @paras = @_; | ||||||||||||||||||||||||||||
| 799 | h1 { $self->get_title }; | ||||||||||||||||||||||||||||
| 800 | div { | ||||||||||||||||||||||||||||
| 801 | id is 'content'; | ||||||||||||||||||||||||||||
| 802 | p { $_ } for @paras; | ||||||||||||||||||||||||||||
| 803 | }; | ||||||||||||||||||||||||||||
| 804 | }; | ||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||
| 806 | package MyApp::Templates; | ||||||||||||||||||||||||||||
| 807 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 808 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 809 | mix MyApp::UtilTemplates under '/util'; | ||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||
| 811 | sub get_title { 'Kashmir' } | ||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||
| 813 | template story => sub { | ||||||||||||||||||||||||||||
| 814 | my $self = shift; | ||||||||||||||||||||||||||||
| 815 | html { | ||||||||||||||||||||||||||||
| 816 | head { | ||||||||||||||||||||||||||||
| 817 | title { "My Site: " . $self->get_title }; | ||||||||||||||||||||||||||||
| 818 | }; | ||||||||||||||||||||||||||||
| 819 | body { | ||||||||||||||||||||||||||||
| 820 | show( 'util/content' => 'first paragraph', 'second paragraph' ); | ||||||||||||||||||||||||||||
| 821 | }; | ||||||||||||||||||||||||||||
| 822 | }; | ||||||||||||||||||||||||||||
| 823 | }; | ||||||||||||||||||||||||||||
| 824 | |||||||||||||||||||||||||||||
| 825 | The first template class, C<MyApp::UtilTemplates>, defines a utility template, | ||||||||||||||||||||||||||||
| 826 | called C<content>, for outputting the contents of page. Note its call to | ||||||||||||||||||||||||||||
| 827 | C<< $self->get_title >> even though it doesn't have a C<get_title> method. This | ||||||||||||||||||||||||||||
| 828 | is part of the mixin's "contract": it requires that the class it's mixed into | ||||||||||||||||||||||||||||
| 829 | have a C<get_title()> method. | ||||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||
| 831 | The second template class, C<MyApp::Templates>, mixes C<MyApp::UtilTemplates> | ||||||||||||||||||||||||||||
| 832 | into itself under the path C</util> and defines a C<get_title()> method as | ||||||||||||||||||||||||||||
| 833 | required by the mixin. Then, its C<story> template calls the mixed-in template | ||||||||||||||||||||||||||||
| 834 | as C<util/content>, because the C<content> template was mixed into the current | ||||||||||||||||||||||||||||
| 835 | template under C</util>. Get it? | ||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||
| 837 | Now we can use the usual template invocation: | ||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||
| 839 | package main; | ||||||||||||||||||||||||||||
| 840 | Template::Declare->init( dispatch_to => ['MyApp::Templates'] ); | ||||||||||||||||||||||||||||
| 841 | print Template::Declare->show('story'); | ||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||
| 843 | To appreciate our output: | ||||||||||||||||||||||||||||
| 844 | |||||||||||||||||||||||||||||
| 845 | <html> | ||||||||||||||||||||||||||||
| 846 | <head> | ||||||||||||||||||||||||||||
| 847 | <title>My Site: Kashmir</title> | ||||||||||||||||||||||||||||
| 848 | </head> | ||||||||||||||||||||||||||||
| 849 | <body> | ||||||||||||||||||||||||||||
| 850 | <h1>Kashmir</h1> | ||||||||||||||||||||||||||||
| 851 | <div id="content"> | ||||||||||||||||||||||||||||
| 852 | <p>fist paragraph</p> | ||||||||||||||||||||||||||||
| 853 | <p>second paragraph</p> | ||||||||||||||||||||||||||||
| 854 | </div> | ||||||||||||||||||||||||||||
| 855 | </body> | ||||||||||||||||||||||||||||
| 856 | </html> | ||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||
| 858 | Mixins are a very useful tool for template authors to add reusable | ||||||||||||||||||||||||||||
| 859 | functionality to their template classes. But it's important to pay attention to | ||||||||||||||||||||||||||||
| 860 | the mixin contracts so that you're sure to implement the required API in your | ||||||||||||||||||||||||||||
| 861 | template class (here, the C<get_title()> method). | ||||||||||||||||||||||||||||
| 862 | |||||||||||||||||||||||||||||
| 863 | =head3 Aliases | ||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||
| 865 | Aliases are very similar to mixins, but implement delegation as a composition | ||||||||||||||||||||||||||||
| 866 | pattern, rather than mixins. The upshot is that there is no contract provided | ||||||||||||||||||||||||||||
| 867 | by an aliased class: it just works. This is because the invocant is the class | ||||||||||||||||||||||||||||
| 868 | from which the aliases are imported, and therefore it will dispatch to methods | ||||||||||||||||||||||||||||
| 869 | defined in the aliased class. | ||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||
| 871 | For example, say that you wanted to output a sidebar on pages that need one | ||||||||||||||||||||||||||||
| 872 | (perhaps your CMS has sidebar things). We can define a template class that | ||||||||||||||||||||||||||||
| 873 | has a template for that: | ||||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||
| 875 | package MyApp::UI::Stuff; | ||||||||||||||||||||||||||||
| 876 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 877 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||
| 879 | sub img_path { '/ui/css' } | ||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||
| 881 | template sidebar => sub { | ||||||||||||||||||||||||||||
| 882 | my ($self, $thing) = @_; | ||||||||||||||||||||||||||||
| 883 | div { | ||||||||||||||||||||||||||||
| 884 | class is 'sidebar'; | ||||||||||||||||||||||||||||
| 885 | img { src is $self->img_path . '/sidebar.png' }; | ||||||||||||||||||||||||||||
| 886 | p { $_->content } for $thing->get_things; | ||||||||||||||||||||||||||||
| 887 | }; | ||||||||||||||||||||||||||||
| 888 | }; | ||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||
| 890 | Note the use of the C<img_path()> method defined in the template class and | ||||||||||||||||||||||||||||
| 891 | used by the C<sidebar> template. Now let's use it: | ||||||||||||||||||||||||||||
| 892 | |||||||||||||||||||||||||||||
| 893 | package MyApp::Render; | ||||||||||||||||||||||||||||
| 894 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 895 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 896 | alias MyApp::UI::Stuff under '/stuff'; | ||||||||||||||||||||||||||||
| 897 | |||||||||||||||||||||||||||||
| 898 | template page => sub { | ||||||||||||||||||||||||||||
| 899 | my ($self, $page) = @_; | ||||||||||||||||||||||||||||
| 900 | h1 { $page->title }; | ||||||||||||||||||||||||||||
| 901 | for my $thing ($page->get_things) { | ||||||||||||||||||||||||||||
| 902 | if ($thing->is('paragraph')) { | ||||||||||||||||||||||||||||
| 903 | p { $thing->content }; | ||||||||||||||||||||||||||||
| 904 | } elsif ($thing->is('sidebar')) { | ||||||||||||||||||||||||||||
| 905 | show( '/stuff/sidebar' => $thing ); | ||||||||||||||||||||||||||||
| 906 | } | ||||||||||||||||||||||||||||
| 907 | } | ||||||||||||||||||||||||||||
| 908 | }; | ||||||||||||||||||||||||||||
| 909 | |||||||||||||||||||||||||||||
| 910 | Here our rendering template class has aliased C<MyApp::UI::Stuff> under | ||||||||||||||||||||||||||||
| 911 | C</stuff>. So the C<page> template calls C<show('/stuff/sidebar')> to invoke | ||||||||||||||||||||||||||||
| 912 | the sidebar template. If we run this: | ||||||||||||||||||||||||||||
| 913 | |||||||||||||||||||||||||||||
| 914 | Template::Declare->init( dispatch_to => ['MyApp::Render'] ); | ||||||||||||||||||||||||||||
| 915 | print Template::Declare->show( page => $page ); | ||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||
| 917 | We get output as you might expect: | ||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||
| 919 | <h1>My page title</h1> | ||||||||||||||||||||||||||||
| 920 | <p>Page paragraph</p> | ||||||||||||||||||||||||||||
| 921 | <div class="sidebar"> | ||||||||||||||||||||||||||||
| 922 | <img src="/ui/css/sidebar.png" /> | ||||||||||||||||||||||||||||
| 923 | <p>Sidebar paragraph</p> | ||||||||||||||||||||||||||||
| 924 | <p>Another paragraph</p> | ||||||||||||||||||||||||||||
| 925 | </div> | ||||||||||||||||||||||||||||
| 926 | |||||||||||||||||||||||||||||
| 927 | Now, let's say that you have political stuff that you want to use a different | ||||||||||||||||||||||||||||
| 928 | image for in the sidebar. If that's the only difference, we can subclass | ||||||||||||||||||||||||||||
| 929 | C<MyApp::UI::Stuff> and just override the C<img_path()> method: | ||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||
| 931 | package MyApp::UI::Stuff::Politics; | ||||||||||||||||||||||||||||
| 932 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 933 | use base 'MyApp::UI::Stuff'; | ||||||||||||||||||||||||||||
| 934 | |||||||||||||||||||||||||||||
| 935 | sub img_path { '/politics/ui/css' } | ||||||||||||||||||||||||||||
| 936 | |||||||||||||||||||||||||||||
| 937 | Now let's mix that into a politics template class: | ||||||||||||||||||||||||||||
| 938 | |||||||||||||||||||||||||||||
| 939 | package MyApp::Render::Politics; | ||||||||||||||||||||||||||||
| 940 | use Template::Declare::Tags; | ||||||||||||||||||||||||||||
| 941 | use base 'Template::Declare'; | ||||||||||||||||||||||||||||
| 942 | alias MyApp::UI::Stuff::Politics under '/politics'; | ||||||||||||||||||||||||||||
| 943 | |||||||||||||||||||||||||||||
| 944 | template page => sub { | ||||||||||||||||||||||||||||
| 945 | my ($self, $page) = @_; | ||||||||||||||||||||||||||||
| 946 | h1 { $page->title }; | ||||||||||||||||||||||||||||
| 947 | for my $thing ($page->get_things) { | ||||||||||||||||||||||||||||
| 948 | if ($thing->is('paragraph')) { | ||||||||||||||||||||||||||||
| 949 | p { $thing->content }; | ||||||||||||||||||||||||||||
| 950 | } elsif ($thing->is('sidebar')) { | ||||||||||||||||||||||||||||
| 951 | show( '/politics/sidebar' => $thing ); | ||||||||||||||||||||||||||||
| 952 | } | ||||||||||||||||||||||||||||
| 953 | } | ||||||||||||||||||||||||||||
| 954 | }; | ||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||
| 956 | The only difference between this template class and C<MyApp::Render> is that | ||||||||||||||||||||||||||||
| 957 | it aliases C<MyApp::UI::Stuff::Politics> under C</politics>, and then calls | ||||||||||||||||||||||||||||
| 958 | C<show('/politics/sidebar')> in the C<page> template. Running this template: | ||||||||||||||||||||||||||||
| 959 | |||||||||||||||||||||||||||||
| 960 | Template::Declare->init( dispatch_to => ['MyApp::Render::Politics'] ); | ||||||||||||||||||||||||||||
| 961 | print Template::Declare->show( page => $page ); | ||||||||||||||||||||||||||||
| 962 | |||||||||||||||||||||||||||||
| 963 | Yields output using the value of the subclass's C<img_path()> method -- that | ||||||||||||||||||||||||||||
| 964 | is, the sidebar image is now F</politics/ui/css/sidebar.png> instead of | ||||||||||||||||||||||||||||
| 965 | F</ui/css/sidebar.png>: | ||||||||||||||||||||||||||||
| 966 | |||||||||||||||||||||||||||||
| 967 | <h1>My page title</h1> | ||||||||||||||||||||||||||||
| 968 | <p>Page paragraph</p> | ||||||||||||||||||||||||||||
| 969 | <div class="sidebar"> | ||||||||||||||||||||||||||||
| 970 | <img src="/politics/ui/css/sidebar.png" /> | ||||||||||||||||||||||||||||
| 971 | <p>Sidebar paragraph</p> | ||||||||||||||||||||||||||||
| 972 | <p>Another paragraph</p> | ||||||||||||||||||||||||||||
| 973 | </div> | ||||||||||||||||||||||||||||
| 974 | |||||||||||||||||||||||||||||
| 975 | =head3 Other Tricks | ||||||||||||||||||||||||||||
| 976 | |||||||||||||||||||||||||||||
| 977 | The delegation behavior of C<alias> actually makes it a decent choice for | ||||||||||||||||||||||||||||
| 978 | template authors to mix and match libraries of template classes as | ||||||||||||||||||||||||||||
| 979 | appropriate, without worrying about side effects. You can even alias templates | ||||||||||||||||||||||||||||
| 980 | in one template class into another template class if you're not the author of | ||||||||||||||||||||||||||||
| 981 | that class by using the C<into> keyword: | ||||||||||||||||||||||||||||
| 982 | |||||||||||||||||||||||||||||
| 983 | alias My::UI::Widgets into Your::UI::View under '/widgets'; | ||||||||||||||||||||||||||||
| 984 | |||||||||||||||||||||||||||||
| 985 | Now the templates defined in C<Your::UI::View> are available in | ||||||||||||||||||||||||||||
| 986 | C<My::UI::Widgets> under C</widgets>. The C<mix> method supports this syntax | ||||||||||||||||||||||||||||
| 987 | as well, though it's not necessarily recommended, given that you would not be | ||||||||||||||||||||||||||||
| 988 | able to fulfill any contracts unless you re-opened the class into which you | ||||||||||||||||||||||||||||
| 989 | mixed the templates. But in any case, authors of framework view classes might | ||||||||||||||||||||||||||||
| 990 | find this functionality useful for automatically aliasing template classes | ||||||||||||||||||||||||||||
| 991 | into a single dispatch template class. | ||||||||||||||||||||||||||||
| 992 | |||||||||||||||||||||||||||||
| 993 | Another trick is to alias or mix your templates with package variables | ||||||||||||||||||||||||||||
| 994 | specific to the composition. Do so via the C<setting> keyword: | ||||||||||||||||||||||||||||
| 995 | |||||||||||||||||||||||||||||
| 996 | package My::Templates; | ||||||||||||||||||||||||||||
| 997 | mix Some::Mixin under '/mymix', setting { name => 'Larry' }; | ||||||||||||||||||||||||||||
| 998 | |||||||||||||||||||||||||||||
| 999 | The templates mixed from C<Some::Mixin> into C<My::Templates> have package | ||||||||||||||||||||||||||||
| 1000 | variables set for them that are accessible I<only> from their mixed-in paths. | ||||||||||||||||||||||||||||
| 1001 | For example, if you define this template in C<Some::Mixin>: | ||||||||||||||||||||||||||||
| 1002 | |||||||||||||||||||||||||||||
| 1003 | template howdy => sub { | ||||||||||||||||||||||||||||
| 1004 | my $self = shift; | ||||||||||||||||||||||||||||
| 1005 | outs "Howdy, " . $self->package_variable('name') || 'Jesse'; | ||||||||||||||||||||||||||||
| 1006 | }; | ||||||||||||||||||||||||||||
| 1007 | |||||||||||||||||||||||||||||
| 1008 | Then C<show('mymix/howdy')> called on C<My::Templates> will output "Howdy, | ||||||||||||||||||||||||||||
| 1009 | Larry", while the output from C<show('howdy')> will output "Howdy, Jesse". In | ||||||||||||||||||||||||||||
| 1010 | other words, package variables defined for the mixed-in templates are | ||||||||||||||||||||||||||||
| 1011 | available only to the mixins and not to the original. The same functionality | ||||||||||||||||||||||||||||
| 1012 | exists for C<alias> as well. | ||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||
| 1014 | =begin comment | ||||||||||||||||||||||||||||
| 1015 | |||||||||||||||||||||||||||||
| 1016 | =head2 Tag Sets | ||||||||||||||||||||||||||||
| 1017 | |||||||||||||||||||||||||||||
| 1018 | Wherein we will eventually provide a brief tutorial on creating custom tag sets. | ||||||||||||||||||||||||||||
| 1019 | |||||||||||||||||||||||||||||
| 1020 | =end comment | ||||||||||||||||||||||||||||
| 1021 | |||||||||||||||||||||||||||||
| 1022 | =head2 Indentation configuration | ||||||||||||||||||||||||||||
| 1023 | |||||||||||||||||||||||||||||
| 1024 | By default, Template::Declare renders a readable XML adding end of lines and a | ||||||||||||||||||||||||||||
| 1025 | one column indentation. This behavior could break a webpage design or add a | ||||||||||||||||||||||||||||
| 1026 | significant amount of chars to your XML output. This could be changed by | ||||||||||||||||||||||||||||
| 1027 | overwriting the default values. So, | ||||||||||||||||||||||||||||
| 1028 | |||||||||||||||||||||||||||||
| 1029 | $Template::Declare::Tags::TAG_INDENTATION = 0; | ||||||||||||||||||||||||||||
| 1030 | $Template::Declare::Tags::EOL = ""; | ||||||||||||||||||||||||||||
| 1031 | say Template::Declare->show('main'); | ||||||||||||||||||||||||||||
| 1032 | |||||||||||||||||||||||||||||
| 1033 | will render | ||||||||||||||||||||||||||||
| 1034 | |||||||||||||||||||||||||||||
| 1035 | <html><body><p>hi</p></body></html> | ||||||||||||||||||||||||||||
| 1036 | |||||||||||||||||||||||||||||
| 1037 | |||||||||||||||||||||||||||||
| 1038 | =head1 METHODS | ||||||||||||||||||||||||||||
| 1039 | |||||||||||||||||||||||||||||
| 1040 | =head2 init | ||||||||||||||||||||||||||||
| 1041 | |||||||||||||||||||||||||||||
| 1042 | This I<class method> initializes the C<Template::Declare> system. | ||||||||||||||||||||||||||||
| 1043 | |||||||||||||||||||||||||||||
| 1044 | =over | ||||||||||||||||||||||||||||
| 1045 | |||||||||||||||||||||||||||||
| 1046 | =item dispatch_to | ||||||||||||||||||||||||||||
| 1047 | |||||||||||||||||||||||||||||
| 1048 | An array reference of classes to search for templates. Template::Declare will | ||||||||||||||||||||||||||||
| 1049 | search this list of classes in order to find a template path. | ||||||||||||||||||||||||||||
| 1050 | |||||||||||||||||||||||||||||
| 1051 | =item roots | ||||||||||||||||||||||||||||
| 1052 | |||||||||||||||||||||||||||||
| 1053 | B<Deprecated.> Just like C<dispatch_to>, only the classes are searched in | ||||||||||||||||||||||||||||
| 1054 | reverse order. Maintained for backward compatibility and for the pleasure of | ||||||||||||||||||||||||||||
| 1055 | those who want to continue using Template::Declare the way that Jesse's | ||||||||||||||||||||||||||||
| 1056 | "crack-addled brain" intended. | ||||||||||||||||||||||||||||
| 1057 | |||||||||||||||||||||||||||||
| 1058 | =item postprocessor | ||||||||||||||||||||||||||||
| 1059 | |||||||||||||||||||||||||||||
| 1060 | A coderef called to postprocess the HTML or XML output of your templates. This | ||||||||||||||||||||||||||||
| 1061 | is to alleviate using Tags for simple text markup. | ||||||||||||||||||||||||||||
| 1062 | |||||||||||||||||||||||||||||
| 1063 | =item around_template | ||||||||||||||||||||||||||||
| 1064 | |||||||||||||||||||||||||||||
| 1065 | A coderef called B<instead> of rendering each template. The coderef will | ||||||||||||||||||||||||||||
| 1066 | receive four arguments: a coderef to invoke to render the template, the | ||||||||||||||||||||||||||||
| 1067 | template's path, an arrayref of the arguments to the template, and the coderef | ||||||||||||||||||||||||||||
| 1068 | of the template itself. You can use this for instrumentation. For example: | ||||||||||||||||||||||||||||
| 1069 | |||||||||||||||||||||||||||||
| 1070 | Template::Declare->init(around_template => sub { | ||||||||||||||||||||||||||||
| 1071 | my ($orig, $path, $args, $code) = @_; | ||||||||||||||||||||||||||||
| 1072 | my $start = time; | ||||||||||||||||||||||||||||
| 1073 | $orig->(); | ||||||||||||||||||||||||||||
| 1074 | warn "Rendering $path took " . (time - $start) . " seconds."; | ||||||||||||||||||||||||||||
| 1075 | }); | ||||||||||||||||||||||||||||
| 1076 | |||||||||||||||||||||||||||||
| 1077 | =item strict | ||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||
| 1079 | Die in exceptional situations, such as when a template can't be found, rather | ||||||||||||||||||||||||||||
| 1080 | than just warn. False by default for backward compatibility. The default may | ||||||||||||||||||||||||||||
| 1081 | be changed in the future, so specifying the value explicitly is recommended. | ||||||||||||||||||||||||||||
| 1082 | |||||||||||||||||||||||||||||
| 1083 | =back | ||||||||||||||||||||||||||||
| 1084 | |||||||||||||||||||||||||||||
| 1085 | =cut | ||||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||
| 1087 | 67 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 1088 | 67 | my $class = shift; | |||||||||||||||||||||||||||
| 1089 | 67 | my %args = (@_); | |||||||||||||||||||||||||||
| 1090 | |||||||||||||||||||||||||||||
| 1091 | 67 | ▶ | if ( $args{dispatch_to} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 1092 | 66 | $class->dispatch_to( $args{dispatch_to} ); | |||||||||||||||||||||||||||
| 1093 | } elsif ( $args{roots} ) { | ||||||||||||||||||||||||||||
| 1094 | 1 | $class->roots( $args{roots} ); | |||||||||||||||||||||||||||
| 1095 | } | ||||||||||||||||||||||||||||
| 1096 | |||||||||||||||||||||||||||||
| 1097 | 67 | ▶ | $class->strict( $args{strict} ) if exists $args{strict}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1098 | 67 | ▶ | $class->postprocessor( $args{postprocessor} ) if $args{postprocessor}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1099 | 67 | ▶ | $class->around_template( $args{around_template} ) if $args{around_template}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1100 | } | ||||||||||||||||||||||||||||
| 1101 | |||||||||||||||||||||||||||||
| 1102 | =head2 show TEMPLATE_NAME | ||||||||||||||||||||||||||||
| 1103 | |||||||||||||||||||||||||||||
| 1104 | Template::Declare->show( 'howdy', name => 'Larry' ); | ||||||||||||||||||||||||||||
| 1105 | my $output = Template::Declare->show('index'); | ||||||||||||||||||||||||||||
| 1106 | |||||||||||||||||||||||||||||
| 1107 | Call C<show> with a C<template_name> and C<Template::Declare> will render that | ||||||||||||||||||||||||||||
| 1108 | template. Subsequent arguments will be passed to the template. Content | ||||||||||||||||||||||||||||
| 1109 | generated by C<show()> can be accessed via the C<output()> method if the | ||||||||||||||||||||||||||||
| 1110 | output method you've chosen returns content instead of outputting it directly. | ||||||||||||||||||||||||||||
| 1111 | |||||||||||||||||||||||||||||
| 1112 | If called in scalar context, this method will also just return the content | ||||||||||||||||||||||||||||
| 1113 | when available. | ||||||||||||||||||||||||||||
| 1114 | |||||||||||||||||||||||||||||
| 1115 | =cut | ||||||||||||||||||||||||||||
| 1116 | |||||||||||||||||||||||||||||
| 1117 | 101 | ▶ | sub show { | ||||||||||||||||||||||||||
|
Subroutine
show: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1118 | 101 | my $class = shift; | |||||||||||||||||||||||||||
| 1119 | 101 | my $template = shift; | |||||||||||||||||||||||||||
| 1120 | 101 | local %Template::Declare::Tags::ELEMENT_ID_CACHE = (); | |||||||||||||||||||||||||||
| 1121 | 101 | return Template::Declare::Tags::show_page($template => @_); | |||||||||||||||||||||||||||
| 1122 | } | ||||||||||||||||||||||||||||
| 1123 | |||||||||||||||||||||||||||||
| 1124 | =head2 Template Composition | ||||||||||||||||||||||||||||
| 1125 | |||||||||||||||||||||||||||||
| 1126 | Sometimes you want to mix templates from one class into another class, or | ||||||||||||||||||||||||||||
| 1127 | delegate template execution to a class of templates. C<alias()> and C<mix()> | ||||||||||||||||||||||||||||
| 1128 | are your keys to doing so. | ||||||||||||||||||||||||||||
| 1129 | |||||||||||||||||||||||||||||
| 1130 | =head3 mix | ||||||||||||||||||||||||||||
| 1131 | |||||||||||||||||||||||||||||
| 1132 | mix Some::Clever::Mixin under '/mixin'; | ||||||||||||||||||||||||||||
| 1133 | mix Some::Other::Mixin under '/otmix', setting { name => 'Larry' }; | ||||||||||||||||||||||||||||
| 1134 | mix My::Mixin into My::View, under '/mymix'; | ||||||||||||||||||||||||||||
| 1135 | |||||||||||||||||||||||||||||
| 1136 | Mixes templates from one template class into another class. When the mixed-in | ||||||||||||||||||||||||||||
| 1137 | template is called, its invocant will be the class into which it was mixed. | ||||||||||||||||||||||||||||
| 1138 | This type of composition is known as a "mixin" in object-oriented parlance. | ||||||||||||||||||||||||||||
| 1139 | See L<Template Composition|/"Template Composition"> for extended examples and | ||||||||||||||||||||||||||||
| 1140 | a comparison to C<alias>. | ||||||||||||||||||||||||||||
| 1141 | |||||||||||||||||||||||||||||
| 1142 | The first parameter is the name of the template class to be mixed in. The | ||||||||||||||||||||||||||||
| 1143 | C<under> keyword tells C<mix> where to put the templates. For example, | ||||||||||||||||||||||||||||
| 1144 | a C<foo> template in C<Some::Clever::Mixin> will be mixed in as C<mixin/foo>. | ||||||||||||||||||||||||||||
| 1145 | |||||||||||||||||||||||||||||
| 1146 | The C<setting> keyword specifies package variables available only to the | ||||||||||||||||||||||||||||
| 1147 | mixed-in copies of templates. These are available to the templates as | ||||||||||||||||||||||||||||
| 1148 | C<< $self->package_variable($varname) >>. | ||||||||||||||||||||||||||||
| 1149 | |||||||||||||||||||||||||||||
| 1150 | The C<into> keyword tells C<mix> into what class to mix the templates. Without | ||||||||||||||||||||||||||||
| 1151 | this keyword, C<mix> will mix them into the calling class. | ||||||||||||||||||||||||||||
| 1152 | |||||||||||||||||||||||||||||
| 1153 | For those who prefer a direct OO syntax for mixins, just call C<mix()> as a | ||||||||||||||||||||||||||||
| 1154 | method on the class to be mixed in. To replicate the above three examples | ||||||||||||||||||||||||||||
| 1155 | without the use of the sugar: | ||||||||||||||||||||||||||||
| 1156 | |||||||||||||||||||||||||||||
| 1157 | Some::Clever::Mixin->mix( '/mixin' ); | ||||||||||||||||||||||||||||
| 1158 | Some::Other::Mixin->mix( '/otmix', { name => 'Larry' } ); | ||||||||||||||||||||||||||||
| 1159 | My::Mixin->mix( 'My::View', '/mymix' ); | ||||||||||||||||||||||||||||
| 1160 | |||||||||||||||||||||||||||||
| 1161 | =cut | ||||||||||||||||||||||||||||
| 1162 | |||||||||||||||||||||||||||||
| 1163 | 11 | ▶ | sub mix { | ||||||||||||||||||||||||||
|
Subroutine
mix: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1164 | 11 | my $mixin = shift; | |||||||||||||||||||||||||||
| 1165 | 11 | my ($into, @args) = _into(@_); | |||||||||||||||||||||||||||
| 1166 | 11 | $mixin->_import($into, $into, @args); | |||||||||||||||||||||||||||
| 1167 | } | ||||||||||||||||||||||||||||
| 1168 | |||||||||||||||||||||||||||||
| 1169 | =head3 alias | ||||||||||||||||||||||||||||
| 1170 | |||||||||||||||||||||||||||||
| 1171 | alias Some::Clever:Templates under '/delegate'; | ||||||||||||||||||||||||||||
| 1172 | alias Some::Other::Templates under '/send_to', { name => 'Larry' }; | ||||||||||||||||||||||||||||
| 1173 | alias UI::Stuff into My::View, under '/mystuff'; | ||||||||||||||||||||||||||||
| 1174 | |||||||||||||||||||||||||||||
| 1175 | Aliases templates from one template class into another class. When an alias | ||||||||||||||||||||||||||||
| 1176 | called, its invocant will be the class from which it was aliased. This type of | ||||||||||||||||||||||||||||
| 1177 | composition is known as "delegation" in object-oriented parlance. See | ||||||||||||||||||||||||||||
| 1178 | L<Template Composition|/"Template Composition"> for extended examples and a | ||||||||||||||||||||||||||||
| 1179 | comparison to C<mix>. | ||||||||||||||||||||||||||||
| 1180 | |||||||||||||||||||||||||||||
| 1181 | The first parameter is the name of the template class to alias. The C<under> | ||||||||||||||||||||||||||||
| 1182 | keyword tells C<alias> where to put the templates. For example, a C<foo> | ||||||||||||||||||||||||||||
| 1183 | template in C<Some::Clever::Templates> will be aliased as C<delegate/foo>. | ||||||||||||||||||||||||||||
| 1184 | |||||||||||||||||||||||||||||
| 1185 | The C<setting> keyword specifies package variables available only to the | ||||||||||||||||||||||||||||
| 1186 | aliases. These are available to the templates as | ||||||||||||||||||||||||||||
| 1187 | C<< $self->package_variable($varname) >>. | ||||||||||||||||||||||||||||
| 1188 | |||||||||||||||||||||||||||||
| 1189 | The C<into> keyword tells C<alias> into what class to alias the templates. | ||||||||||||||||||||||||||||
| 1190 | Without this keyword, C<alias> will alias them into the calling class. | ||||||||||||||||||||||||||||
| 1191 | |||||||||||||||||||||||||||||
| 1192 | For those who prefer a direct OO syntax for mixins, just call C<alias()> as a | ||||||||||||||||||||||||||||
| 1193 | method on the class to be mixed in. To replicate the above three examples | ||||||||||||||||||||||||||||
| 1194 | without the use of the sugar: | ||||||||||||||||||||||||||||
| 1195 | |||||||||||||||||||||||||||||
| 1196 | Some::Clever:Templates->alias( '/delegate' ); | ||||||||||||||||||||||||||||
| 1197 | Some::Other::Templates->alias( '/send_to', { name => 'Larry' } ); | ||||||||||||||||||||||||||||
| 1198 | UI::Stuff->alias( 'My::View', '/mystuff' ); | ||||||||||||||||||||||||||||
| 1199 | |||||||||||||||||||||||||||||
| 1200 | =cut | ||||||||||||||||||||||||||||
| 1201 | |||||||||||||||||||||||||||||
| 1202 | 15 | ▶ | sub alias { | ||||||||||||||||||||||||||
|
Subroutine
alias: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1203 | 15 | my $mixin = shift; | |||||||||||||||||||||||||||
| 1204 | 15 | my ($into, @args) = _into(@_); | |||||||||||||||||||||||||||
| 1205 | 15 | $mixin->_import($into, undef, @args); | |||||||||||||||||||||||||||
| 1206 | } | ||||||||||||||||||||||||||||
| 1207 | |||||||||||||||||||||||||||||
| 1208 | =head3 package_variable( VARIABLE ) | ||||||||||||||||||||||||||||
| 1209 | |||||||||||||||||||||||||||||
| 1210 | $td->package_variable( $varname => $value ); | ||||||||||||||||||||||||||||
| 1211 | $value = $td->package_variable( $varname ); | ||||||||||||||||||||||||||||
| 1212 | |||||||||||||||||||||||||||||
| 1213 | Returns a value set for a mixed-in template's variable, if any were specified | ||||||||||||||||||||||||||||
| 1214 | when the template was mixed-in. See L</mix> for details. | ||||||||||||||||||||||||||||
| 1215 | |||||||||||||||||||||||||||||
| 1216 | =cut | ||||||||||||||||||||||||||||
| 1217 | |||||||||||||||||||||||||||||
| 1218 | 10 | ▶ | sub package_variable { | ||||||||||||||||||||||||||
|
Subroutine
package_variable: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 1219 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 1220 | 10 | my $var = shift; | |||||||||||||||||||||||||||
| 1221 | 10 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1222 | 0 | $TEMPLATE_VARS->{$self}->{$var} = shift; | |||||||||||||||||||||||||||
| 1223 | } | ||||||||||||||||||||||||||||
| 1224 | 10 | return $TEMPLATE_VARS->{$self}->{$var}; | |||||||||||||||||||||||||||
| 1225 | } | ||||||||||||||||||||||||||||
| 1226 | |||||||||||||||||||||||||||||
| 1227 | =head3 package_variables( VARIABLE ) | ||||||||||||||||||||||||||||
| 1228 | |||||||||||||||||||||||||||||
| 1229 | $td->package_variables( $variables ); | ||||||||||||||||||||||||||||
| 1230 | $variables = $td->package_variables; | ||||||||||||||||||||||||||||
| 1231 | |||||||||||||||||||||||||||||
| 1232 | Get or set a hash reference of variables for a mixed-in template. See | ||||||||||||||||||||||||||||
| 1233 | L</mix> for details. | ||||||||||||||||||||||||||||
| 1234 | |||||||||||||||||||||||||||||
| 1235 | =cut | ||||||||||||||||||||||||||||
| 1236 | |||||||||||||||||||||||||||||
| 1237 | 0 | ▶ | sub package_variables { | ||||||||||||||||||||||||||
|
Subroutine
package_variables: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 1238 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 1239 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1240 | 0 | %{ $TEMPLATE_VARS->{$self} } = shift; | |||||||||||||||||||||||||||
| 1241 | } | ||||||||||||||||||||||||||||
| 1242 | 0 | return $TEMPLATE_VARS->{$self}; | |||||||||||||||||||||||||||
| 1243 | } | ||||||||||||||||||||||||||||
| 1244 | |||||||||||||||||||||||||||||
| 1245 | =head2 Templates registration and lookup | ||||||||||||||||||||||||||||
| 1246 | |||||||||||||||||||||||||||||
| 1247 | =head3 resolve_template TEMPLATE_PATH INCLUDE_PRIVATE_TEMPLATES | ||||||||||||||||||||||||||||
| 1248 | |||||||||||||||||||||||||||||
| 1249 | my $code = Template::Declare->resolve_template($template); | ||||||||||||||||||||||||||||
| 1250 | my $code = Template::Declare->has_template($template, 1); | ||||||||||||||||||||||||||||
| 1251 | |||||||||||||||||||||||||||||
| 1252 | Turns a template path (C<TEMPLATE_PATH>) into a C<CODEREF>. If the | ||||||||||||||||||||||||||||
| 1253 | boolean C<INCLUDE_PRIVATE_TEMPLATES> is true, resolves private templates | ||||||||||||||||||||||||||||
| 1254 | in addition to public ones. C<has_template()> is an alias for this method. | ||||||||||||||||||||||||||||
| 1255 | |||||||||||||||||||||||||||||
| 1256 | First it looks through all the valid Template::Declare classes defined via | ||||||||||||||||||||||||||||
| 1257 | C<dispatch_to>. For each class, it looks to see if it has a template called | ||||||||||||||||||||||||||||
| 1258 | $template_name directly (or via a mixin). | ||||||||||||||||||||||||||||
| 1259 | |||||||||||||||||||||||||||||
| 1260 | =cut | ||||||||||||||||||||||||||||
| 1261 | |||||||||||||||||||||||||||||
| 1262 | 286 | ▶ | sub resolve_template { | ||||||||||||||||||||||||||
|
Subroutine
resolve_template: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 1263 | 286 | my $self = shift; | |||||||||||||||||||||||||||
| 1264 | 286 | my $template_name = shift; | |||||||||||||||||||||||||||
| 1265 | 286 | ▶ | my $show_private = shift || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 1266 | |||||||||||||||||||||||||||||
| 1267 | 286 | my @search_packages; | |||||||||||||||||||||||||||
| 1268 | |||||||||||||||||||||||||||||
| 1269 | # If we're being called as a class method on T::D it means "search in any package" | ||||||||||||||||||||||||||||
| 1270 | # Otherwise, it means search only in this specific package" | ||||||||||||||||||||||||||||
| 1271 | 286 | ▶ | if ( $self eq __PACKAGE__ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1272 | 246 | @search_packages = @{ Template::Declare->dispatch_to }; | |||||||||||||||||||||||||||
| 1273 | } else { | ||||||||||||||||||||||||||||
| 1274 | 40 | @search_packages = ($self); | |||||||||||||||||||||||||||
| 1275 | } | ||||||||||||||||||||||||||||
| 1276 | |||||||||||||||||||||||||||||
| 1277 | 286 | foreach my $package (@search_packages) { | |||||||||||||||||||||||||||
| 1278 | 291 | ▶ | next unless ( $package and $package->isa(__PACKAGE__) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$package and $package->isa(__PACKAGE__)
MC/DC0%0/2
$package and $package->isa(__PACKAGE__)
$package
$package->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= $packageB= $package->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||
| 1279 | 291 | ▶ | if ( my $coderef = $package->_has_template( $template_name, $show_private ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1280 | 252 | return $coderef; | |||||||||||||||||||||||||||
| 1281 | } | ||||||||||||||||||||||||||||
| 1282 | } | ||||||||||||||||||||||||||||
| 1283 | } | ||||||||||||||||||||||||||||
| 1284 | |||||||||||||||||||||||||||||
| 1285 | =head3 has_template TEMPLATE_PATH INCLUDE_PRIVATE_TEMPLATES | ||||||||||||||||||||||||||||
| 1286 | |||||||||||||||||||||||||||||
| 1287 | An alias for C<resolve_template>. | ||||||||||||||||||||||||||||
| 1288 | |||||||||||||||||||||||||||||
| 1289 | =cut | ||||||||||||||||||||||||||||
| 1290 | |||||||||||||||||||||||||||||
| 1291 | 53 | ▶ | sub has_template { resolve_template(@_) } | ||||||||||||||||||||||||||
|
Subroutine
has_template: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1292 | |||||||||||||||||||||||||||||
| 1293 | =head3 register_template( TEMPLATE_NAME, CODEREF ) | ||||||||||||||||||||||||||||
| 1294 | |||||||||||||||||||||||||||||
| 1295 | MyApp::Templates->register_template( howdy => sub { ... } ); | ||||||||||||||||||||||||||||
| 1296 | |||||||||||||||||||||||||||||
| 1297 | This method registers a template called C<TEMPLATE_NAME> in the calling class. | ||||||||||||||||||||||||||||
| 1298 | As you might guess, C<CODEREF> defines the template's implementation. This | ||||||||||||||||||||||||||||
| 1299 | method is mainly intended to be used internally, as you use the C<template> | ||||||||||||||||||||||||||||
| 1300 | keyword to create templates, right? | ||||||||||||||||||||||||||||
| 1301 | |||||||||||||||||||||||||||||
| 1302 | =cut | ||||||||||||||||||||||||||||
| 1303 | |||||||||||||||||||||||||||||
| 1304 | 196 | ▶ | sub register_template { | ||||||||||||||||||||||||||
|
Subroutine
register_template: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1305 | 196 | my $class = shift; | |||||||||||||||||||||||||||
| 1306 | 196 | my $template_name = shift; | |||||||||||||||||||||||||||
| 1307 | 196 | my $code = shift; | |||||||||||||||||||||||||||
| 1308 | 196 | push @{ __PACKAGE__->templates()->{$class} }, $template_name; | |||||||||||||||||||||||||||
| 1309 | 196 | _register_template( $class, _template_name_to_sub($template_name), $code ) | |||||||||||||||||||||||||||
| 1310 | } | ||||||||||||||||||||||||||||
| 1311 | |||||||||||||||||||||||||||||
| 1312 | =head3 register_private_template( TEMPLATE_NAME, CODEREF ) | ||||||||||||||||||||||||||||
| 1313 | |||||||||||||||||||||||||||||
| 1314 | MyApp::Templates->register_private_template( howdy => sub { ... } ); | ||||||||||||||||||||||||||||
| 1315 | |||||||||||||||||||||||||||||
| 1316 | This method registers a private template called C<TEMPLATE_NAME> in the | ||||||||||||||||||||||||||||
| 1317 | calling class. As you might guess, C<CODEREF> defines the template's | ||||||||||||||||||||||||||||
| 1318 | implementation. | ||||||||||||||||||||||||||||
| 1319 | |||||||||||||||||||||||||||||
| 1320 | Private templates can't be called directly from user code but only from other | ||||||||||||||||||||||||||||
| 1321 | templates. | ||||||||||||||||||||||||||||
| 1322 | |||||||||||||||||||||||||||||
| 1323 | This method is mainly intended to be used internally, as you use the | ||||||||||||||||||||||||||||
| 1324 | C<private template> expression to create templates, right? | ||||||||||||||||||||||||||||
| 1325 | |||||||||||||||||||||||||||||
| 1326 | =cut | ||||||||||||||||||||||||||||
| 1327 | |||||||||||||||||||||||||||||
| 1328 | 21 | ▶ | sub register_private_template { | ||||||||||||||||||||||||||
|
Subroutine
register_private_template: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1329 | 21 | my $class = shift; | |||||||||||||||||||||||||||
| 1330 | 21 | my $template_name = shift; | |||||||||||||||||||||||||||
| 1331 | 21 | my $code = shift; | |||||||||||||||||||||||||||
| 1332 | 21 | push @{ __PACKAGE__->private_templates()->{$class} }, $template_name; | |||||||||||||||||||||||||||
| 1333 | 21 | _register_template( $class, _template_name_to_private_sub($template_name), $code ); | |||||||||||||||||||||||||||
| 1334 | |||||||||||||||||||||||||||||
| 1335 | } | ||||||||||||||||||||||||||||
| 1336 | |||||||||||||||||||||||||||||
| 1337 | =head3 buffer | ||||||||||||||||||||||||||||
| 1338 | |||||||||||||||||||||||||||||
| 1339 | Gets or sets the L<String::BufferStack> object; this is a class method. | ||||||||||||||||||||||||||||
| 1340 | |||||||||||||||||||||||||||||
| 1341 | You can use it to manipulate the output from tags as they are output. It's used | ||||||||||||||||||||||||||||
| 1342 | internally to make the tags nest correctly, and be output to the right place. | ||||||||||||||||||||||||||||
| 1343 | We're not sure if there's ever a need for you to frob it by hand, but it does | ||||||||||||||||||||||||||||
| 1344 | enable things like the following: | ||||||||||||||||||||||||||||
| 1345 | |||||||||||||||||||||||||||||
| 1346 | template simple => sub { | ||||||||||||||||||||||||||||
| 1347 | html { | ||||||||||||||||||||||||||||
| 1348 | head {} | ||||||||||||||||||||||||||||
| 1349 | body { | ||||||||||||||||||||||||||||
| 1350 | Template::Declare->buffer->set_filter( sub {uc shift} ); | ||||||||||||||||||||||||||||
| 1351 | p { 'Whee!' } | ||||||||||||||||||||||||||||
| 1352 | p { 'Hello, world wide web!' } | ||||||||||||||||||||||||||||
| 1353 | Template::Declare->buffer->clear_top if rand() < 0.5; | ||||||||||||||||||||||||||||
| 1354 | } | ||||||||||||||||||||||||||||
| 1355 | } | ||||||||||||||||||||||||||||
| 1356 | }; | ||||||||||||||||||||||||||||
| 1357 | |||||||||||||||||||||||||||||
| 1358 | ...which outputs, with equal regularity, either: | ||||||||||||||||||||||||||||
| 1359 | |||||||||||||||||||||||||||||
| 1360 | <html> | ||||||||||||||||||||||||||||
| 1361 | <head></head> | ||||||||||||||||||||||||||||
| 1362 | <body> | ||||||||||||||||||||||||||||
| 1363 | <P>WHEE!</P> | ||||||||||||||||||||||||||||
| 1364 | <P>HELLO, WORLD WIDE WEB!</P> | ||||||||||||||||||||||||||||
| 1365 | </body> | ||||||||||||||||||||||||||||
| 1366 | </html> | ||||||||||||||||||||||||||||
| 1367 | |||||||||||||||||||||||||||||
| 1368 | ...or: | ||||||||||||||||||||||||||||
| 1369 | |||||||||||||||||||||||||||||
| 1370 | <html> | ||||||||||||||||||||||||||||
| 1371 | <head></head> | ||||||||||||||||||||||||||||
| 1372 | <body></body> | ||||||||||||||||||||||||||||
| 1373 | </html> | ||||||||||||||||||||||||||||
| 1374 | |||||||||||||||||||||||||||||
| 1375 | We'll leave it to you to judge whether or not that's actually useful. | ||||||||||||||||||||||||||||
| 1376 | |||||||||||||||||||||||||||||
| 1377 | =head2 Helpers | ||||||||||||||||||||||||||||
| 1378 | |||||||||||||||||||||||||||||
| 1379 | You don't need to call any of this directly. | ||||||||||||||||||||||||||||
| 1380 | |||||||||||||||||||||||||||||
| 1381 | =head3 into | ||||||||||||||||||||||||||||
| 1382 | |||||||||||||||||||||||||||||
| 1383 | $class = into $class; | ||||||||||||||||||||||||||||
| 1384 | |||||||||||||||||||||||||||||
| 1385 | C<into> is a helper method providing semantic sugar for the L</mix> method. | ||||||||||||||||||||||||||||
| 1386 | All it does is return the name of the class on which it was called. | ||||||||||||||||||||||||||||
| 1387 | |||||||||||||||||||||||||||||
| 1388 | =cut | ||||||||||||||||||||||||||||
| 1389 | |||||||||||||||||||||||||||||
| 1390 | 4 | ▶ | sub into { shift } | ||||||||||||||||||||||||||
|
Subroutine
into: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1391 | |||||||||||||||||||||||||||||
| 1392 | =head2 Old, deprecated or just better to avoid | ||||||||||||||||||||||||||||
| 1393 | |||||||||||||||||||||||||||||
| 1394 | =head3 import_templates | ||||||||||||||||||||||||||||
| 1395 | |||||||||||||||||||||||||||||
| 1396 | import_templates MyApp::Templates under '/something'; | ||||||||||||||||||||||||||||
| 1397 | |||||||||||||||||||||||||||||
| 1398 | Like C<mix()>, but without support for the C<into> or C<setting> keywords. | ||||||||||||||||||||||||||||
| 1399 | That is, it mixes templates into the calling template class and does not | ||||||||||||||||||||||||||||
| 1400 | support package variables for those mixins. | ||||||||||||||||||||||||||||
| 1401 | |||||||||||||||||||||||||||||
| 1402 | B<Deprecated> in favor of L</mix>. Will be supported for a long time, but | ||||||||||||||||||||||||||||
| 1403 | new code should use C<mix()>. | ||||||||||||||||||||||||||||
| 1404 | |||||||||||||||||||||||||||||
| 1405 | =cut | ||||||||||||||||||||||||||||
| 1406 | |||||||||||||||||||||||||||||
| 1407 | 11 | ▶ | sub import_templates { | ||||||||||||||||||||||||||
|
Subroutine
import_templates: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1408 | 11 | my $caller = scalar caller(0); | |||||||||||||||||||||||||||
| 1409 | 11 | shift->_import($caller, $caller, @_); | |||||||||||||||||||||||||||
| 1410 | } | ||||||||||||||||||||||||||||
| 1411 | |||||||||||||||||||||||||||||
| 1412 | =head3 new_buffer_frame | ||||||||||||||||||||||||||||
| 1413 | |||||||||||||||||||||||||||||
| 1414 | $td->new_buffer_frame; | ||||||||||||||||||||||||||||
| 1415 | # same as | ||||||||||||||||||||||||||||
| 1416 | $td->buffer->push( private => 1 ); | ||||||||||||||||||||||||||||
| 1417 | |||||||||||||||||||||||||||||
| 1418 | Creates a new buffer frame, using L<String::BufferStack/push> with C<private>. | ||||||||||||||||||||||||||||
| 1419 | |||||||||||||||||||||||||||||
| 1420 | B<Deprecated> in favor of dealing with L</buffer> directly. | ||||||||||||||||||||||||||||
| 1421 | |||||||||||||||||||||||||||||
| 1422 | =cut | ||||||||||||||||||||||||||||
| 1423 | |||||||||||||||||||||||||||||
| 1424 | 0 | ▶ | sub new_buffer_frame { | ||||||||||||||||||||||||||
|
Subroutine
new_buffer_frame: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 1425 | 0 | __PACKAGE__->buffer->push( private => 1 ); | |||||||||||||||||||||||||||
| 1426 | } | ||||||||||||||||||||||||||||
| 1427 | |||||||||||||||||||||||||||||
| 1428 | =head3 end_buffer_frame | ||||||||||||||||||||||||||||
| 1429 | |||||||||||||||||||||||||||||
| 1430 | my $buf = $td->end_buffer_frame; | ||||||||||||||||||||||||||||
| 1431 | # same as | ||||||||||||||||||||||||||||
| 1432 | my $buf = $td->buffer->pop; | ||||||||||||||||||||||||||||
| 1433 | |||||||||||||||||||||||||||||
| 1434 | Deletes and returns the topmost buffer, using L<String::BufferStack/pop>. | ||||||||||||||||||||||||||||
| 1435 | |||||||||||||||||||||||||||||
| 1436 | B<Deprecated> in favor of dealing with L</buffer> directly. | ||||||||||||||||||||||||||||
| 1437 | |||||||||||||||||||||||||||||
| 1438 | =cut | ||||||||||||||||||||||||||||
| 1439 | |||||||||||||||||||||||||||||
| 1440 | 0 | ▶ | sub end_buffer_frame { | ||||||||||||||||||||||||||
|
Subroutine
end_buffer_frame: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 1441 | 0 | __PACKAGE__->buffer->pop; | |||||||||||||||||||||||||||
| 1442 | } | ||||||||||||||||||||||||||||
| 1443 | |||||||||||||||||||||||||||||
| 1444 | =head3 path_for $template | ||||||||||||||||||||||||||||
| 1445 | |||||||||||||||||||||||||||||
| 1446 | my $path = Template::Declare->path_for('index'); | ||||||||||||||||||||||||||||
| 1447 | |||||||||||||||||||||||||||||
| 1448 | Returns the path for the template name to be used for show, adjusted with | ||||||||||||||||||||||||||||
| 1449 | paths used in C<mix>. Note that this will only work for the last class into | ||||||||||||||||||||||||||||
| 1450 | which you imported the template. This method is, therefore, deprecated. | ||||||||||||||||||||||||||||
| 1451 | |||||||||||||||||||||||||||||
| 1452 | =cut | ||||||||||||||||||||||||||||
| 1453 | |||||||||||||||||||||||||||||
| 1454 | # Deprecated in favor of dispatch_to(). | ||||||||||||||||||||||||||||
| 1455 | 1 | ▶ | sub roots { | ||||||||||||||||||||||||||
|
Subroutine
roots: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1456 | # warn "roots() has been deprecated; use dispatch_to() instead\n"; | ||||||||||||||||||||||||||||
| 1457 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 1458 | 1 | ▶ | $class->dispatch_to( [ reverse @{ +shift } ] ) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1459 | 1 | return [ reverse @{ $class->dispatch_to } ]; | |||||||||||||||||||||||||||
| 1460 | } | ||||||||||||||||||||||||||||
| 1461 | |||||||||||||||||||||||||||||
| 1462 | # Removed methods that no longer work (and were never documented anyway). | ||||||||||||||||||||||||||||
| 1463 | # Remove these no-ops after a few releases (added for 0.41). | ||||||||||||||||||||||||||||
| 1464 | |||||||||||||||||||||||||||||
| 1465 | =begin comment | ||||||||||||||||||||||||||||
| 1466 | |||||||||||||||||||||||||||||
| 1467 | =head3 aliases | ||||||||||||||||||||||||||||
| 1468 | |||||||||||||||||||||||||||||
| 1469 | =head3 alias_metadata | ||||||||||||||||||||||||||||
| 1470 | |||||||||||||||||||||||||||||
| 1471 | =end comment | ||||||||||||||||||||||||||||
| 1472 | |||||||||||||||||||||||||||||
| 1473 | =cut | ||||||||||||||||||||||||||||
| 1474 | |||||||||||||||||||||||||||||
| 1475 | 0 | ▶ | sub aliases { | ||||||||||||||||||||||||||
|
Subroutine
aliases: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 1476 | 0 | require Carp; | |||||||||||||||||||||||||||
| 1477 | 0 | Carp::cluck( 'aliases() is a deprecated no-op' ); | |||||||||||||||||||||||||||
| 1478 | } | ||||||||||||||||||||||||||||
| 1479 | |||||||||||||||||||||||||||||
| 1480 | 0 | ▶ | sub alias_metadata { | ||||||||||||||||||||||||||
|
Subroutine
alias_metadata: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 1481 | 0 | require Carp; | |||||||||||||||||||||||||||
| 1482 | 0 | Carp::cluck( 'alias_metadata() is a deprecated no-op' ); | |||||||||||||||||||||||||||
| 1483 | } | ||||||||||||||||||||||||||||
| 1484 | |||||||||||||||||||||||||||||
| 1485 | 3 | ▶ | sub path_for { | ||||||||||||||||||||||||||
|
Subroutine
path_for: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 1486 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 1487 | 3 | my $template = shift; | |||||||||||||||||||||||||||
| 1488 | 3 | ▶ | return ($class->imported_into ||'') . '/' . $template; | ||||||||||||||||||||||||||
|
Condition100%2/2
$class->imported_into || ''
MC/DC100%1/1
$class->imported_into || ''
$class->imported_into
Truth table100%2/2
A
A= $class->imported_into
| |||||||||||||||||||||||||||||
| 1489 | } | ||||||||||||||||||||||||||||
| 1490 | |||||||||||||||||||||||||||||
| 1491 | 79 | ▶ | sub _templates_for { | ||||||||||||||||||||||||||
|
Subroutine
_templates_for: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 1492 | 79 | ▶ | my $tmpl = shift->templates->{+shift} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1493 | 37 | ▶ | return wantarray ? @{ $tmpl } : $tmpl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1494 | } | ||||||||||||||||||||||||||||
| 1495 | |||||||||||||||||||||||||||||
| 1496 | 79 | ▶ | sub _private_templates_for { | ||||||||||||||||||||||||||
|
Subroutine
_private_templates_for: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 1497 | 79 | ▶ | my $tmpl = shift->private_templates->{+shift} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1498 | 10 | ▶ | return wantarray ? @{ $tmpl } : $tmpl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1499 | } | ||||||||||||||||||||||||||||
| 1500 | |||||||||||||||||||||||||||||
| 1501 | 291 | ▶ | sub _has_template { | ||||||||||||||||||||||||||
|
Subroutine
_has_template: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 1502 | # Otherwise find only in specific package | ||||||||||||||||||||||||||||
| 1503 | 291 | my $pkg = shift; | |||||||||||||||||||||||||||
| 1504 | 291 | my $template_name = shift; | |||||||||||||||||||||||||||
| 1505 | 291 | my $show_private = 0 || shift; | |||||||||||||||||||||||||||
| 1506 | |||||||||||||||||||||||||||||
| 1507 | 291 | ▶ | if ( my $coderef = $pkg->_find_template_sub( _template_name_to_sub($template_name) ) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 1508 | 246 | return $coderef; | |||||||||||||||||||||||||||
| 1509 | ▶ | } elsif ( $show_private and $coderef = $pkg->_find_template_sub( _template_name_to_private_sub($template_name))) { | |||||||||||||||||||||||||||
|
Condition100%3/3
$show_private and $coderef = $pkg->_find_template_sub(_template_name_to_private_sub($template_name))
MC/DC100%2/2
$show_private and $coderef = $pkg->_find_template_sub(_template_name_to_private_sub($template_name))
$show_private
$coderef = $pkg->_find_template_sub(_template_name_to_private_sub($template_name))
Truth table100%3/3
A and B
A= $show_privateB= $coderef = $pkg->_find_template_sub(_template_name_to_private_sub($template_name))
| |||||||||||||||||||||||||||||
| 1510 | 6 | return $coderef; | |||||||||||||||||||||||||||
| 1511 | } | ||||||||||||||||||||||||||||
| 1512 | |||||||||||||||||||||||||||||
| 1513 | 39 | return undef; | |||||||||||||||||||||||||||
| 1514 | } | ||||||||||||||||||||||||||||
| 1515 | |||||||||||||||||||||||||||||
| 1516 | 214 | ▶ | sub _dispatch_template { | ||||||||||||||||||||||||||
|
Subroutine
_dispatch_template: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1517 | 214 | my $class = shift; | |||||||||||||||||||||||||||
| 1518 | 214 | my $code = shift; | |||||||||||||||||||||||||||
| 1519 | 214 | unshift @_, $class; | |||||||||||||||||||||||||||
| 1520 | 214 | goto $code; | |||||||||||||||||||||||||||
| 1521 | } | ||||||||||||||||||||||||||||
| 1522 | |||||||||||||||||||||||||||||
| 1523 | 355 | ▶ | sub _find_template_sub { | ||||||||||||||||||||||||||
|
Subroutine
_find_template_sub: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1524 | 355 | my $self = shift; | |||||||||||||||||||||||||||
| 1525 | 355 | my $subname = shift; | |||||||||||||||||||||||||||
| 1526 | 355 | return $self->can($subname); | |||||||||||||||||||||||||||
| 1527 | } | ||||||||||||||||||||||||||||
| 1528 | |||||||||||||||||||||||||||||
| 1529 | 534 | ▶ | sub _template_name_to_sub { | ||||||||||||||||||||||||||
|
Subroutine
_template_name_to_sub: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1530 | 534 | return _subname( "_jifty_template_", shift ); | |||||||||||||||||||||||||||
| 1531 | } | ||||||||||||||||||||||||||||
| 1532 | |||||||||||||||||||||||||||||
| 1533 | 38 | ▶ | sub _template_name_to_private_sub { | ||||||||||||||||||||||||||
|
Subroutine
_template_name_to_private_sub: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1534 | 38 | return _subname( "_jifty_private_template_", shift ); | |||||||||||||||||||||||||||
| 1535 | } | ||||||||||||||||||||||||||||
| 1536 | |||||||||||||||||||||||||||||
| 1537 | 572 | ▶ | sub _subname { | ||||||||||||||||||||||||||
|
Subroutine
_subname: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 1538 | 572 | my $prefix = shift; | |||||||||||||||||||||||||||
| 1539 | 572 | ▶ | my $template = shift || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 1540 | 572 | $template =~ s{/+}{/}g; | |||||||||||||||||||||||||||
| 1541 | 572 | $template =~ s{^/}{}; | |||||||||||||||||||||||||||
| 1542 | 572 | return join( '', $prefix, $template ); | |||||||||||||||||||||||||||
| 1543 | } | ||||||||||||||||||||||||||||
| 1544 | |||||||||||||||||||||||||||||
| 1545 | 217 | ▶ | sub _register_template { | ||||||||||||||||||||||||||
|
Subroutine
_register_template: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 1546 | 217 | my $self = shift; | |||||||||||||||||||||||||||
| 1547 | 217 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 1548 | 217 | my $subname = shift; | |||||||||||||||||||||||||||
| 1549 | 217 | my $coderef = shift; | |||||||||||||||||||||||||||
| 1550 | 48 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1551 | 48 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1552 | 217 | *{ $class . '::' . $subname } = $coderef; | |||||||||||||||||||||||||||
| 1553 | } | ||||||||||||||||||||||||||||
| 1554 | |||||||||||||||||||||||||||||
| 1555 | 26 | ▶ | sub _into { | ||||||||||||||||||||||||||
|
Subroutine
_into: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 1556 | 26 | my ($into, $under); | |||||||||||||||||||||||||||
| 1557 | 26 | ▶ | if ( eval { $_[0]->isa(__PACKAGE__) } ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 1558 | 2 | ($into, $under) = (shift, shift); | |||||||||||||||||||||||||||
| 1559 | 24 | } elsif ( eval { $_[1]->isa(__PACKAGE__) } ) { | |||||||||||||||||||||||||||
| 1560 | 2 | ($under, $into) = (shift, shift); | |||||||||||||||||||||||||||
| 1561 | } else { | ||||||||||||||||||||||||||||
| 1562 | 22 | $into = caller(1); | |||||||||||||||||||||||||||
| 1563 | 22 | $under = shift; | |||||||||||||||||||||||||||
| 1564 | } | ||||||||||||||||||||||||||||
| 1565 | 26 | return $into, $under, @_; | |||||||||||||||||||||||||||
| 1566 | } | ||||||||||||||||||||||||||||
| 1567 | |||||||||||||||||||||||||||||
| 1568 | 37 | ▶ | sub _import { | ||||||||||||||||||||||||||
|
Subroutine
_import: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||
| 1569 | 37 | ▶ | return undef if $_[0] eq __PACKAGE__; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1570 | 37 | my ($mixin, $into, $invocant, $prefix, $vars) = @_; | |||||||||||||||||||||||||||
| 1571 | |||||||||||||||||||||||||||||
| 1572 | |||||||||||||||||||||||||||||
| 1573 | 37 | $prefix =~ s|/+/|/|g; | |||||||||||||||||||||||||||
| 1574 | 37 | $prefix =~ s|/$||; | |||||||||||||||||||||||||||
| 1575 | 37 | $mixin->imported_into($prefix); | |||||||||||||||||||||||||||
| 1576 | |||||||||||||||||||||||||||||
| 1577 | 37 | my @packages = reverse grep { $_->isa(__PACKAGE__) } | |||||||||||||||||||||||||||
| 1578 | Class::ISA::self_and_super_path( $mixin ); | ||||||||||||||||||||||||||||
| 1579 | |||||||||||||||||||||||||||||
| 1580 | 37 | foreach my $from (@packages) { | |||||||||||||||||||||||||||
| 1581 | 79 | for my $tname ( __PACKAGE__->_templates_for($from) ) { | |||||||||||||||||||||||||||
| 1582 | 47 | my $sname = _template_name_to_sub($tname); | |||||||||||||||||||||||||||
| 1583 | 47 | ▶ | $into->register_template( | ||||||||||||||||||||||||||
|
Condition66%2/3
$invocant || $mixin
MC/DC0%0/2
$invocant || $mixin
$invocant
$mixin
Truth table66%2/3
A || B
A= $invocantB= $mixin
| |||||||||||||||||||||||||||||
| 1584 | "$prefix/$tname", | ||||||||||||||||||||||||||||
| 1585 | _import_code( $sname, $from, $invocant || $mixin, $vars ) | ||||||||||||||||||||||||||||
| 1586 | ); | ||||||||||||||||||||||||||||
| 1587 | } | ||||||||||||||||||||||||||||
| 1588 | 79 | for my $tname ( __PACKAGE__->_private_templates_for($from) ) { | |||||||||||||||||||||||||||
| 1589 | 10 | my $sname = _template_name_to_private_sub($tname); | |||||||||||||||||||||||||||
| 1590 | 10 | ▶ | $into->register_private_template( | ||||||||||||||||||||||||||
|
Condition66%2/3
$invocant || $mixin
MC/DC0%0/2
$invocant || $mixin
$invocant
$mixin
Truth table66%2/3
A || B
A= $invocantB= $mixin
| |||||||||||||||||||||||||||||
| 1591 | "$prefix/$tname", | ||||||||||||||||||||||||||||
| 1592 | _import_code( $sname, $from, $invocant || $mixin, $vars ) | ||||||||||||||||||||||||||||
| 1593 | ); | ||||||||||||||||||||||||||||
| 1594 | } | ||||||||||||||||||||||||||||
| 1595 | } | ||||||||||||||||||||||||||||
| 1596 | } | ||||||||||||||||||||||||||||
| 1597 | |||||||||||||||||||||||||||||
| 1598 | 57 | ▶ | sub _import_code { | ||||||||||||||||||||||||||
|
Subroutine
_import_code: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 1599 | 57 | my ($sname, $from, $mixin, $vars) = @_; | |||||||||||||||||||||||||||
| 1600 | 57 | my $code = $from->_find_template_sub( $sname ); | |||||||||||||||||||||||||||
| 1601 | 30 | ▶ | return $mixin eq $from ? $code : sub { shift; $code->($mixin, @_) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1602 | 57 | ▶ | unless $vars; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 1603 | 2 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1604 | 2 | shift @_; # Get rid of the passed-in "$self" class. | |||||||||||||||||||||||||||
| 1605 | 2 | local $TEMPLATE_VARS->{$mixin} = $vars; | |||||||||||||||||||||||||||
| 1606 | 2 | $code->($mixin, @_); | |||||||||||||||||||||||||||
| 1607 | 4 | }; | |||||||||||||||||||||||||||
| 1608 | } | ||||||||||||||||||||||||||||
| 1609 | |||||||||||||||||||||||||||||
| 1610 | =head1 PITFALLS | ||||||||||||||||||||||||||||
| 1611 | |||||||||||||||||||||||||||||
| 1612 | We're reusing the perl interpreter for our templating language, but Perl was | ||||||||||||||||||||||||||||
| 1613 | not designed specifically for our purpose here. Here are some known pitfalls | ||||||||||||||||||||||||||||
| 1614 | while you're scripting your templates with this module. | ||||||||||||||||||||||||||||
| 1615 | |||||||||||||||||||||||||||||
| 1616 | =over | ||||||||||||||||||||||||||||
| 1617 | |||||||||||||||||||||||||||||
| 1618 | =item * | ||||||||||||||||||||||||||||
| 1619 | |||||||||||||||||||||||||||||
| 1620 | It's quite common to see tag sub calling statements without trailing | ||||||||||||||||||||||||||||
| 1621 | semi-colons right after C<}>. For instance, | ||||||||||||||||||||||||||||
| 1622 | |||||||||||||||||||||||||||||
| 1623 | template foo => sub { | ||||||||||||||||||||||||||||
| 1624 | p { | ||||||||||||||||||||||||||||
| 1625 | a { attr { src => '1.png' } } | ||||||||||||||||||||||||||||
| 1626 | a { attr { src => '2.png' } } | ||||||||||||||||||||||||||||
| 1627 | a { attr { src => '3.png' } } | ||||||||||||||||||||||||||||
| 1628 | } | ||||||||||||||||||||||||||||
| 1629 | }; | ||||||||||||||||||||||||||||
| 1630 | |||||||||||||||||||||||||||||
| 1631 | is equivalent to | ||||||||||||||||||||||||||||
| 1632 | |||||||||||||||||||||||||||||
| 1633 | template foo => sub { | ||||||||||||||||||||||||||||
| 1634 | p { | ||||||||||||||||||||||||||||
| 1635 | a { attr { src => '1.png' } }; | ||||||||||||||||||||||||||||
| 1636 | a { attr { src => '2.png' } }; | ||||||||||||||||||||||||||||
| 1637 | a { attr { src => '3.png' } }; | ||||||||||||||||||||||||||||
| 1638 | }; | ||||||||||||||||||||||||||||
| 1639 | }; | ||||||||||||||||||||||||||||
| 1640 | |||||||||||||||||||||||||||||
| 1641 | But C<xml_decl> is a notable exception. Please always put a trailing semicolon | ||||||||||||||||||||||||||||
| 1642 | after C<xml_decl { ... }>, or you'll mess up the order of output. | ||||||||||||||||||||||||||||
| 1643 | |||||||||||||||||||||||||||||
| 1644 | =item * | ||||||||||||||||||||||||||||
| 1645 | |||||||||||||||||||||||||||||
| 1646 | Another place that requires trailing semicolon is the statements before a Perl | ||||||||||||||||||||||||||||
| 1647 | looping statement, an C<if> statement, or a C<show> call. For example: | ||||||||||||||||||||||||||||
| 1648 | |||||||||||||||||||||||||||||
| 1649 | p { "My links:" }; | ||||||||||||||||||||||||||||
| 1650 | for (@links) { | ||||||||||||||||||||||||||||
| 1651 | with ( src => $_ ), a {} | ||||||||||||||||||||||||||||
| 1652 | } | ||||||||||||||||||||||||||||
| 1653 | |||||||||||||||||||||||||||||
| 1654 | The C<;> after C< p { ... } > is required here, or Perl will complain about | ||||||||||||||||||||||||||||
| 1655 | syntax errors. | ||||||||||||||||||||||||||||
| 1656 | |||||||||||||||||||||||||||||
| 1657 | Another example is | ||||||||||||||||||||||||||||
| 1658 | |||||||||||||||||||||||||||||
| 1659 | h1 { 'heading' }; # this trailing semicolon is mandatory | ||||||||||||||||||||||||||||
| 1660 | show 'tag_tag' | ||||||||||||||||||||||||||||
| 1661 | |||||||||||||||||||||||||||||
| 1662 | =item * | ||||||||||||||||||||||||||||
| 1663 | |||||||||||||||||||||||||||||
| 1664 | The C<is> syntax for declaring tag attributes also requires a trailing | ||||||||||||||||||||||||||||
| 1665 | semicolon, unless it is the only statement in a block. For example, | ||||||||||||||||||||||||||||
| 1666 | |||||||||||||||||||||||||||||
| 1667 | p { class is 'item'; id is 'item1'; outs "This is an item" } | ||||||||||||||||||||||||||||
| 1668 | img { src is 'cat.gif' } | ||||||||||||||||||||||||||||
| 1669 | |||||||||||||||||||||||||||||
| 1670 | =item * | ||||||||||||||||||||||||||||
| 1671 | |||||||||||||||||||||||||||||
| 1672 | Literal strings that have tag siblings won't be captured. So the following template | ||||||||||||||||||||||||||||
| 1673 | |||||||||||||||||||||||||||||
| 1674 | p { 'hello'; em { 'world' } } | ||||||||||||||||||||||||||||
| 1675 | |||||||||||||||||||||||||||||
| 1676 | produces | ||||||||||||||||||||||||||||
| 1677 | |||||||||||||||||||||||||||||
| 1678 | <p> | ||||||||||||||||||||||||||||
| 1679 | <em>world</em> | ||||||||||||||||||||||||||||
| 1680 | </p> | ||||||||||||||||||||||||||||
| 1681 | |||||||||||||||||||||||||||||
| 1682 | instead of the desired output | ||||||||||||||||||||||||||||
| 1683 | |||||||||||||||||||||||||||||
| 1684 | <p> | ||||||||||||||||||||||||||||
| 1685 | hello | ||||||||||||||||||||||||||||
| 1686 | <em>world</em> | ||||||||||||||||||||||||||||
| 1687 | </p> | ||||||||||||||||||||||||||||
| 1688 | |||||||||||||||||||||||||||||
| 1689 | You can use C<outs> here to solve this problem: | ||||||||||||||||||||||||||||
| 1690 | |||||||||||||||||||||||||||||
| 1691 | p { outs 'hello'; em { 'world' } } | ||||||||||||||||||||||||||||
| 1692 | |||||||||||||||||||||||||||||
| 1693 | Note you can always get rid of C<outs> if the string literal is the only | ||||||||||||||||||||||||||||
| 1694 | element of the containing block: | ||||||||||||||||||||||||||||
| 1695 | |||||||||||||||||||||||||||||
| 1696 | p { 'hello, world!' } | ||||||||||||||||||||||||||||
| 1697 | |||||||||||||||||||||||||||||
| 1698 | =item * | ||||||||||||||||||||||||||||
| 1699 | |||||||||||||||||||||||||||||
| 1700 | Look out! If the if block is the last block/statement and the condition part | ||||||||||||||||||||||||||||
| 1701 | is evaluated to be 0: | ||||||||||||||||||||||||||||
| 1702 | |||||||||||||||||||||||||||||
| 1703 | p { if ( 0 ) { } } | ||||||||||||||||||||||||||||
| 1704 | |||||||||||||||||||||||||||||
| 1705 | produces | ||||||||||||||||||||||||||||
| 1706 | |||||||||||||||||||||||||||||
| 1707 | <p>0</p> | ||||||||||||||||||||||||||||
| 1708 | |||||||||||||||||||||||||||||
| 1709 | instead of the more intuitive output: | ||||||||||||||||||||||||||||
| 1710 | |||||||||||||||||||||||||||||
| 1711 | <p></p> | ||||||||||||||||||||||||||||
| 1712 | |||||||||||||||||||||||||||||
| 1713 | This is because C<if ( 0 )> is the last expression, so C<0> is returned as the | ||||||||||||||||||||||||||||
| 1714 | value of the whole block, which is used as the content of <p> tag. | ||||||||||||||||||||||||||||
| 1715 | |||||||||||||||||||||||||||||
| 1716 | To get rid of this, just put an empty string at the end so it returns empty | ||||||||||||||||||||||||||||
| 1717 | string as the content instead of 0: | ||||||||||||||||||||||||||||
| 1718 | |||||||||||||||||||||||||||||
| 1719 | p { if ( 0 ) { } '' } | ||||||||||||||||||||||||||||
| 1720 | |||||||||||||||||||||||||||||
| 1721 | =back | ||||||||||||||||||||||||||||
| 1722 | |||||||||||||||||||||||||||||
| 1723 | =head1 BUGS | ||||||||||||||||||||||||||||
| 1724 | |||||||||||||||||||||||||||||
| 1725 | Crawling all over, baby. Be very, very careful. This code is so cutting edge, | ||||||||||||||||||||||||||||
| 1726 | it can only be fashioned from carbon nanotubes. But we're already using this | ||||||||||||||||||||||||||||
| 1727 | thing in production :) Make sure you have read the L</PITFALLS> | ||||||||||||||||||||||||||||
| 1728 | section above :) | ||||||||||||||||||||||||||||
| 1729 | |||||||||||||||||||||||||||||
| 1730 | Some specific bugs and design flaws that we'd love to see fixed. | ||||||||||||||||||||||||||||
| 1731 | |||||||||||||||||||||||||||||
| 1732 | =over | ||||||||||||||||||||||||||||
| 1733 | |||||||||||||||||||||||||||||
| 1734 | =item Output isn't streamy. | ||||||||||||||||||||||||||||
| 1735 | |||||||||||||||||||||||||||||
| 1736 | =back | ||||||||||||||||||||||||||||
| 1737 | |||||||||||||||||||||||||||||
| 1738 | If you run into bugs or misfeatures, please report them to | ||||||||||||||||||||||||||||
| 1739 | C<bug-template-declare@rt.cpan.org>. | ||||||||||||||||||||||||||||
| 1740 | |||||||||||||||||||||||||||||
| 1741 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 1742 | |||||||||||||||||||||||||||||
| 1743 | =over | ||||||||||||||||||||||||||||
| 1744 | |||||||||||||||||||||||||||||
| 1745 | =item L<Template::Declare::Tags> | ||||||||||||||||||||||||||||
| 1746 | |||||||||||||||||||||||||||||
| 1747 | =item L<Template::Declare::TagSet> | ||||||||||||||||||||||||||||
| 1748 | |||||||||||||||||||||||||||||
| 1749 | =item L<Template::Declare::TagSet::HTML> | ||||||||||||||||||||||||||||
| 1750 | |||||||||||||||||||||||||||||
| 1751 | =item L<Template::Declare::TagSet::XUL> | ||||||||||||||||||||||||||||
| 1752 | |||||||||||||||||||||||||||||
| 1753 | =item L<Jifty> | ||||||||||||||||||||||||||||
| 1754 | |||||||||||||||||||||||||||||
| 1755 | =back | ||||||||||||||||||||||||||||
| 1756 | |||||||||||||||||||||||||||||
| 1757 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 1758 | |||||||||||||||||||||||||||||
| 1759 | Jesse Vincent <jesse@bestpractical.com> | ||||||||||||||||||||||||||||
| 1760 | |||||||||||||||||||||||||||||
| 1761 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 1762 | |||||||||||||||||||||||||||||
| 1763 | Template::Declare is Copyright 2006-2010 Best Practical Solutions, LLC. | ||||||||||||||||||||||||||||
| 1764 | |||||||||||||||||||||||||||||
| 1765 | Template::Declare is distributed under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 1766 | |||||||||||||||||||||||||||||
| 1767 | =cut | ||||||||||||||||||||||||||||
| 1768 | |||||||||||||||||||||||||||||
| 1769 | 48 | 1; | |||||||||||||||||||||||||||