| 1 | package Template::Lace; | ||
| 2 | |||
| 3 | our $VERSION = '0.016'; | ||
| 4 | |||
| 5 | 1; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Template::Lace - Logic-less, strongly typed, and componentized HTML templates. | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | A Template Model: | ||
| 14 | |||
| 15 | package MyApp::Template::User; | ||
| 16 | |||
| 17 | use Moo; | ||
| 18 | |||
| 19 | has [qw/age name motto/] => (is=>'ro', required=>1); | ||
| 20 | |||
| 21 | sub template { | ||
| 22 | return q[ | ||
| 23 | <html> | ||
| 24 | <head> | ||
| 25 | <title>User Info</title> | ||
| 26 | </head> | ||
| 27 | <body> | ||
| 28 | <dl id='user'> | ||
| 29 | <dt>Name</dt> | ||
| 30 | <dd id='name'> -NAME- </dd> | ||
| 31 | <dt>Age</dt> | ||
| 32 | <dd id='age'> -AGE- </dd> | ||
| 33 | <dt>Motto</dt> | ||
| 34 | <dd id='motto'> -MOTTO- </dd> | ||
| 35 | </dl> | ||
| 36 | </body> | ||
| 37 | </html> | ||
| 38 | ] | ||
| 39 | } | ||
| 40 | |||
| 41 | sub process_dom { | ||
| 42 | my ($self, $dom) = @_; | ||
| 43 | $dom->dl('#user', +{ | ||
| 44 | age=>$self->age, | ||
| 45 | name=>$self->name, | ||
| 46 | motto=>$self->motto}); | ||
| 47 | } | ||
| 48 | |||
| 49 | 1; | ||
| 50 | |||
| 51 | A Factory and Renderer | ||
| 52 | |||
| 53 | my $factory = Template::Lace::Factory->new( | ||
| 54 | model_class=>'MyApp::Template::User'); | ||
| 55 | |||
| 56 | # Once the $factory is created you can reuse it to build new | ||
| 57 | # template renderer instances. | ||
| 58 | |||
| 59 | my $renderer = $factory->create( | ||
| 60 | age=>42, | ||
| 61 | name=>'John', | ||
| 62 | motto=>'Life in the Fast Lane!'); | ||
| 63 | |||
| 64 | print $renderer->render; | ||
| 65 | |||
| 66 | Outputs: | ||
| 67 | |||
| 68 | <html> | ||
| 69 | <head> | ||
| 70 | <title> | ||
| 71 | User Info | ||
| 72 | </title> | ||
| 73 | </head> | ||
| 74 | <body id="body"> | ||
| 75 | <dl id="user"> | ||
| 76 | <dt> | ||
| 77 | Name | ||
| 78 | </dt> | ||
| 79 | <dd id="name"> | ||
| 80 | John | ||
| 81 | </dd> | ||
| 82 | <dt> | ||
| 83 | Age | ||
| 84 | </dt> | ||
| 85 | <dd id="age"> | ||
| 86 | 42 | ||
| 87 | </dd> | ||
| 88 | <dt> | ||
| 89 | Motto | ||
| 90 | </dt> | ||
| 91 | <dd id="motto"> | ||
| 92 | Why Not? | ||
| 93 | </dd> | ||
| 94 | </dl> | ||
| 95 | </body> | ||
| 96 | </html> | ||
| 97 | |||
| 98 | =head1 DISCLAIMER | ||
| 99 | |||
| 100 | L<Template::Lace> is a toolkit for building HTML pages using logic-less and componentized | ||
| 101 | templates. As such this distribution is currently not aimed at standalone use but rather | ||
| 102 | exists as all the reusable bits that fell out when I refactored L<Catalyst::View::Template::Lace>. | ||
| 103 | So currently this toolkit then exists to support the L<Catalyst> View and as a result documentation | ||
| 104 | here is high level and API level. If you want to integrate L<Template::Lace> into other | ||
| 105 | web frameworks you might wish to review L<Catalyst::View::Template::Lace> for a possible | ||
| 106 | approach. Ideas about how to make this distribution more usefully stand alone are quite welcomed! | ||
| 107 | Examples given here are probably more verbose than it would be if using this under a web | ||
| 108 | framework like L<Catalyst> (see L<Catalyst::View::Template::Lace>). | ||
| 109 | |||
| 110 | You may wish to review the files under the C</examples> directory of this distribution to | ||
| 111 | see a L<Web::Simple> proof of concept. | ||
| 112 | |||
| 113 | B<NOTE> Since this is still under heavy development and review I reserve the right to make | ||
| 114 | breaking changes, or to conclude the approach is fundementally flawed and exit the project. Do | ||
| 115 | not use this code in production aimed systems unless you are skilled enough to take on that | ||
| 116 | risk and responsibility. | ||
| 117 | |||
| 118 | =head1 DESCRIPTION | ||
| 119 | |||
| 120 | L<Template::Lace> is a toolkit that makes it possible to bind HTML templates to plain old Perl | ||
| 121 | classes as long as they provide a defined interface. These | ||
| 122 | templates are fully HTML markup only; they contain no display logic, only valid HTML and component | ||
| 123 | declarations. We use L<Template::Lace::DOM> (which is a subclass of L<Mojo::DOM58>) to alter the | ||
| 124 | template for presentation at request time. L<Template::Lace::DOM> provides an API to transform | ||
| 125 | the template into HTML using instance data and methods provided by the class. See the class | ||
| 126 | L<Template::Lace::DOM> and L<Mojo::DOM58> for more about how these classes allow you to inspect | ||
| 127 | and modify a DOM. | ||
| 128 | |||
| 129 | When you have a Perl class that conform to these requirements we call that a 'Model' class | ||
| 130 | Here's an example of a very simple Model class: | ||
| 131 | |||
| 132 | package MyApp::Template::User; | ||
| 133 | |||
| 134 | use Moo; | ||
| 135 | |||
| 136 | has [qw/age name motto/] => (is=>'ro', required=>1); | ||
| 137 | |||
| 138 | sub template { q[ | ||
| 139 | <html> | ||
| 140 | <head> | ||
| 141 | <title>User Info</title> | ||
| 142 | </head> | ||
| 143 | <body> | ||
| 144 | <dl id='user'> | ||
| 145 | <dt>Name</dt> | ||
| 146 | <dd id='name'> -NAME- </dd> | ||
| 147 | <dt>Age</dt> | ||
| 148 | <dd id='age'> -AGE- </dd> | ||
| 149 | <dt>Motto</dt> | ||
| 150 | <dd id='motto'> -MOTTO- </dd> | ||
| 151 | </dl> | ||
| 152 | </body> | ||
| 153 | </html> | ||
| 154 | ]} | ||
| 155 | |||
| 156 | sub process_dom { | ||
| 157 | my ($self, $dom) = @_; | ||
| 158 | $dom->dl('#user', +{ | ||
| 159 | age=>$self->age, | ||
| 160 | name=>$self->name, | ||
| 161 | motto=>$self->motto}); | ||
| 162 | } | ||
| 163 | |||
| 164 | 1; | ||
| 165 | |||
| 166 | In this example the Model class defines two methods, C<process_dom> and C<template>. Any Perl class | ||
| 167 | can be used as Model class as long as it provides these methods (as well as a stub for C<prepare_dom> | ||
| 168 | which will we discuss later). The C<template> method | ||
| 169 | just needs to return a string. It should contain your desired HTML markup. The C<process_dom> method | ||
| 170 | is an instance method on your class, and it gets both C<$self> and C<$dom>, where C<$dom> is a | ||
| 171 | DOM representation of your template (via L<Template::Lace::DOM>). Anything you want to change about | ||
| 172 | the template should be done via the L<Template::Lace::DOM> API. This is a subclass of L<Mojo::DOM58> | ||
| 173 | a Jquery like API for transforming HTML. Our custom subclass contains some additional helper methods | ||
| 174 | to make common types of transforms easier. For example here we use the custom helper C<dl> to find | ||
| 175 | a C<dl> tag by its id and then populate its data by matching hash keys to tag ids. | ||
| 176 | |||
| 177 | So how do you get a rendered template out of a Model class? That's the job of two additional | ||
| 178 | classes, L<Template::Lace::Factory> and L<Template::Lace::Renderer> (with a tag team by | ||
| 179 | L<Template::Lace::Components> should your template contain components; to be discussed later). | ||
| 180 | |||
| 181 | L<Template::Lace::Factory> wraps your model class and inspects it to create an initial DOM representation | ||
| 182 | of the template (as well as prepare a component hierarchy, should you have components). Most | ||
| 183 | simply it looks like this: | ||
| 184 | |||
| 185 | my $factory = Template::Lace::Factory->new( | ||
| 186 | model_class=>'MyApp::Template::User'); | ||
| 187 | |||
| 188 | Next you call the C<create> method on the C<$factory> instance with the initial arguments you want to | ||
| 189 | pass to the Model. Create doesn't return the Model directly, but instead returns an instance of | ||
| 190 | L<Template::Lace::Renderer> which is wrapping the model: | ||
| 191 | |||
| 192 | my $renderer = $factory->create( | ||
| 193 | age=>42, | ||
| 194 | name=>'John', | ||
| 195 | motto=>'Life in the Fast Lane!'); | ||
| 196 | |||
| 197 | $renderer->model; # this is the actual instance of MyApp::Template::User via | ||
| 198 | # the provided args to ->create. | ||
| 199 | |||
| 200 | Those initial arguments are passed to the model and used to create an instance of the model. But the | ||
| 201 | wrapping C<$renderer> exposes methods that are used to do the actual transformation. For example | ||
| 202 | |||
| 203 | print $renderer->render; | ||
| 204 | |||
| 205 | Would return: | ||
| 206 | |||
| 207 | <html> | ||
| 208 | <head> | ||
| 209 | <title> | ||
| 210 | User Info | ||
| 211 | </title> | ||
| 212 | </head> | ||
| 213 | <body id="body"> | ||
| 214 | <dl id="user"> | ||
| 215 | <dt> | ||
| 216 | Name | ||
| 217 | </dt> | ||
| 218 | <dd id="name"> | ||
| 219 | John | ||
| 220 | </dd> | ||
| 221 | <dt> | ||
| 222 | Age | ||
| 223 | </dt> | ||
| 224 | <dd id="age"> | ||
| 225 | 42 | ||
| 226 | </dd> | ||
| 227 | <dt> | ||
| 228 | Motto | ||
| 229 | </dt> | ||
| 230 | <dd id="motto"> | ||
| 231 | Why Not? | ||
| 232 | </dd> | ||
| 233 | </dl> | ||
| 234 | </body> | ||
| 235 | </html> | ||
| 236 | |||
| 237 | And that is the basics of it! Once you have a C<$factory> you can call C<create> on it as many times | ||
| 238 | as you like to make different versions of the rendered page. | ||
| 239 | |||
| 240 | =head1 PREPARING THE DOM | ||
| 241 | |||
| 242 | Sometimes a Model may wish to make some modifications to its DOM once at setup time. For | ||
| 243 | example you might add some debugging information to the header. Although you can do this | ||
| 244 | in C<process_dom> it might seem wasteful if the change isn't dynamic, or bound to something | ||
| 245 | that can change for each request. In this case your Model may add a class method C<prepare_dom> | ||
| 246 | which gets access to the DOM of the template during its initial setup. Any changes you make | ||
| 247 | at this point will become cloned for all subsequent requests. For example: | ||
| 248 | |||
| 249 | package MyApp::Template::List; | ||
| 250 | |||
| 251 | use Moo; | ||
| 252 | with 'Template::Lace::Model::AutoTemplate'; | ||
| 253 | |||
| 254 | has [qw/form items copywrite/] => (is=>'ro', required=>1); | ||
| 255 | |||
| 256 | sub time { return scalar localtime } | ||
| 257 | |||
| 258 | sub prepare_dom { | ||
| 259 | my ($class, $dom, $merged_args) = @_; | ||
| 260 | |||
| 261 | # Add some meta-data | ||
| 262 | $dom->at('head') | ||
| 263 | ->prepend_content("<meta startup='${\$self->time}'>"); | ||
| 264 | } | ||
| 265 | |||
| 266 | sub process_dom { | ||
| 267 | my ($self, $dom) = @_; | ||
| 268 | $dom->ol('#todos', $self->items); | ||
| 269 | } | ||
| 270 | |||
| 271 | In this case we'd add a startup timestamp to the header area of the template, which might be | ||
| 272 | useful for debugging for example. | ||
| 273 | |||
| 274 | This example also used the role L<Template::Lace::Model::AutoTemplate> which allows you to | ||
| 275 | pull your template from a standalone file using a simple naming convention. When your | ||
| 276 | templates are larger and more complex, or when you have HTML designer that prefers standalone | ||
| 277 | templates instead of ones mixed into Perl code, you can use this role to achieve that. See | ||
| 278 | the docs in L<Template::Lace::Model::AutoTemplate> for more. | ||
| 279 | |||
| 280 | =head1 ADVANCED TEMPLATE MANIPULATION | ||
| 281 | |||
| 282 | So far we've seen how to use C<process_dom> (and the related C<prepare_dom>) to transform your | ||
| 283 | template. Strickly speaking however you do not actually need to declare these methods in your | ||
| 284 | view model. You may instead choose to allow the renderer to 'call into' the model. This approach | ||
| 285 | can give the end user a bit more control over how the template is actually rendered. Example: | ||
| 286 | |||
| 287 | package Local::Template::NoProcessDom; | ||
| 288 | |||
| 289 | use Moo; | ||
| 290 | |||
| 291 | sub template { qq{ | ||
| 292 | <section> | ||
| 293 | Hello <span id="name">NAME</span>, you are <span id="age"></span> years old! | ||
| 294 | </section> | ||
| 295 | }} | ||
| 296 | |||
| 297 | sub fill_name { | ||
| 298 | my ($self, $dom, $name) = @_; | ||
| 299 | $dom->do('#name', $name); | ||
| 300 | } | ||
| 301 | |||
| 302 | sub fill_age { | ||
| 303 | my ($self, $dom, $age) = @_; | ||
| 304 | $dom->do('#age', $age); | ||
| 305 | } | ||
| 306 | |||
| 307 | my $factory = Template::Lace::Factory->new( | ||
| 308 | model_class=>'Local::Template::NoProcessDom'); | ||
| 309 | |||
| 310 | my $renderer = $factory->create(); | ||
| 311 | |||
| 312 | $renderer->call('fill_name', 'John'); | ||
| 313 | $renderer->call('fill_age', '42'); | ||
| 314 | |||
| 315 | my $html = $renderer->render; | ||
| 316 | print $html; | ||
| 317 | |||
| 318 | Returns: | ||
| 319 | |||
| 320 | <section> | ||
| 321 | Hello <span id="name">John</span>, you are <span id="age">42</span> years old! | ||
| 322 | </section> | ||
| 323 | |||
| 324 | You might find this approach useful when you have a template where you process it differently | ||
| 325 | depending on logic under the control of the code that gets the renderer. For example you might | ||
| 326 | have a form that may or may not have errors. | ||
| 327 | |||
| 328 | B<NOTE> You can combine both approachs, have a C<process_dom> and yet also call into the view | ||
| 329 | model to handle special display conditions. | ||
| 330 | |||
| 331 | =head1 COMPONENTS | ||
| 332 | |||
| 333 | Most template systems have a mechanism to make it possible to divide your template into | ||
| 334 | discrete, re-usable chunks. L<Template::Lace> provides this via Components. Components are | ||
| 335 | custom tags embedded into your template, usually with some markup which allows you to control | ||
| 336 | the passing of information into the component from the Model class. In your template you | ||
| 337 | would declare a component like this: | ||
| 338 | |||
| 339 | <prefix-name | ||
| 340 | attr1='literal value' | ||
| 341 | attr2='$.foo' | ||
| 342 | attr3=\'title:content'> | ||
| 343 | [some additional content such as HTML markup and text] | ||
| 344 | </prefix-name> | ||
| 345 | |||
| 346 | A component doesn't have to contain anything (like the <br/> or <hr/> tag) in which case | ||
| 347 | its just: | ||
| 348 | |||
| 349 | <prefix-name | ||
| 350 | attr1='literal value' | ||
| 351 | attr2='$.foo' | ||
| 352 | attr3=\'title:content' /> | ||
| 353 | |||
| 354 | Here's an example: | ||
| 355 | |||
| 356 | <view-footer copydate='$.copywrite' /> | ||
| 357 | |||
| 358 | And another example: | ||
| 359 | |||
| 360 | <lace-form | ||
| 361 | method="post" | ||
| 362 | action="$.post_url"> | ||
| 363 | <input type='text' name='user' /> | ||
| 364 | <input type='text' name='age' /> | ||
| 365 | <input type='text' name='motto' /> | ||
| 366 | <input type='submit' /> | ||
| 367 | </lace-form> | ||
| 368 | |||
| 369 | Canonically a component is a tag in the form of '$prefix-$name' (like <prefix-name ...>) | ||
| 370 | and typically will contain HTML attributes (like 'attr1="Literal"') and it may also | ||
| 371 | have content, as in "<lace-form><input id='content' type="submit"/></lace-form>". When you render | ||
| 372 | a template containing components, any HTML attributes will be converted to a real value | ||
| 373 | and passed to the component as initialization arguments. There are three different | ||
| 374 | ways your attributes will be processed: | ||
| 375 | |||
| 376 | =over4 | ||
| 377 | |||
| 378 | =item literal values | ||
| 379 | |||
| 380 | Example: <prefix-name attr='1984'/> | ||
| 381 | |||
| 382 | When a value is a simple literal that value is passed as is. | ||
| 383 | |||
| 384 | =item a path from the model instance | ||
| 385 | |||
| 386 | Example: <prefix-name attr='$.foo'/> | ||
| 387 | |||
| 388 | This returns the value of "$self->foo", where $self is the model instance that the factory | ||
| 389 | created. You can follow a data path similarly to Template Toolkit, for example: | ||
| 390 | |||
| 391 | <prefix-name attr="$.foo.bar.baz" /> | ||
| 392 | |||
| 393 | Would be the value of "$self->foo->bar->baz" (or possibly $self->foo->{bar}{baz} since we | ||
| 394 | follow either a method name or the key of a hash). Currently we do not follow arrayrefs. | ||
| 395 | |||
| 396 | You'll probably use this quite often to pass instance information to your component. If | ||
| 397 | the path does not exist that will return a run time error. | ||
| 398 | |||
| 399 | =item a CSS match | ||
| 400 | |||
| 401 | Examples: <prefix-name title=\'title:content' css=\'@link' /> | ||
| 402 | |||
| 403 | When the value of the attribute begins with a '\' that means we want to get the value of | ||
| 404 | a CSS match to the current DOM. In Perl when a variable starts with a '\' the means its | ||
| 405 | a reference; so you can think of this as a reference to a point in the current DOM. | ||
| 406 | |||
| 407 | In general the value here is just a normal CSS match specification (see L<Mojo::DOM58> for | ||
| 408 | details on the match specifications supported). However we have added a few minor bits to | ||
| 409 | how L<Mojo::DOM58> works to make some types matching easier. First, if a match specification | ||
| 410 | ends in ':content' that means 'match the content, not the full node'. In the example case | ||
| 411 | "title=\'title:content'" that would get the text value of the title tag. This could also be | ||
| 412 | used to get a stringified form of a matches content when the content is complex such as a match | ||
| 413 | like "body=\'body:content'. If you want the contents of a match in the form of a collection of | ||
| 414 | nodes instead using "body=\'body:nodes'. You might wish to do this if the contents are large | ||
| 415 | and you want to avoid unnessary stringification and parsing. | ||
| 416 | |||
| 417 | Lastly, in the case where you want the match to return a collection of nodes all matching the | ||
| 418 | specification, you would prepend a '@' to the front of it (think in Perl @variable means an | ||
| 419 | array variable). In the given example "css=\'@link'" we want the attribute 'css' to be a | ||
| 420 | collection of all the linked stylesheets in the current DOM. | ||
| 421 | |||
| 422 | You will use this type of value when you are making components that do complex layout | ||
| 423 | and overlays of the current DOM (such as when you are creating a master layout page for | ||
| 424 | your website). | ||
| 425 | |||
| 426 | =back | ||
| 427 | |||
| 428 | In addition to any attributes you pass to a component via a declaration as described above | ||
| 429 | all components could get some of the following automatic atttributes: | ||
| 430 | |||
| 431 | =over 4 | ||
| 432 | |||
| 433 | =item content | ||
| 434 | |||
| 435 | If the component has content as in the following example | ||
| 436 | |||
| 437 | <prefix-name | ||
| 438 | attr1='literal value' | ||
| 439 | attr2='$.foo' | ||
| 440 | attr3=\'title:content'> | ||
| 441 | [some addtional content such as HTML markup and text] | ||
| 442 | </prefix-name> | ||
| 443 | |||
| 444 | That content will be sent to the component under the 'content' attribute | ||
| 445 | |||
| 446 | =item container | ||
| 447 | |||
| 448 | If the component is a subcomponent it will receive the instance model of its | ||
| 449 | parent as the 'container' attribute. | ||
| 450 | |||
| 451 | =item model | ||
| 452 | |||
| 453 | All components get the 'model' attribute, which is the model instance that contains | ||
| 454 | the template in which they appear. I would use this carefully since I think that | ||
| 455 | you would prefer to pass information from the model to the component via attributes. | ||
| 456 | |||
| 457 | =back | ||
| 458 | |||
| 459 | If you need to pass complex or structured data to your arguments you may do so using | ||
| 460 | JSON: | ||
| 461 | |||
| 462 | <prefix-name | ||
| 463 | hash={"q":"The query string"} | ||
| 464 | array=["1","2","3"] | ||
| 465 | [some addtional content such as HTML markup and text] | ||
| 466 | </prefix-name> | ||
| 467 | |||
| 468 | Components can be an instance of any class that does C<create> and C<process_dom> | ||
| 469 | but generally you will make your components out of other L<Template::Lace> models | ||
| 470 | since that provides the most features and template reusability. Components are added to | ||
| 471 | the Factory at the time you construct it: | ||
| 472 | |||
| 473 | my $factory = Template::Lace::Factory->new( | ||
| 474 | model_class=>'Local::Template::List', | ||
| 475 | component_handlers=>+{ | ||
| 476 | layout => sub { | ||
| 477 | my ($name, $args, %attrs) = @_; | ||
| 478 | $name = ucfirst $name; | ||
| 479 | return Template::Lace::Factory->new(model_class=>"Local::Template::$name"); | ||
| 480 | }, | ||
| 481 | lace => { | ||
| 482 | form => Template::Lace::Factory->new(model_class=>'Local::Template::Form'), | ||
| 483 | input => Template::Lace::Factory->new(model_class=>'Local::Template::Input'), | ||
| 484 | }, | ||
| 485 | }, | ||
| 486 | ); | ||
| 487 | |||
| 488 | Components are added as a hashref of data associated with the 'component_handlers' | ||
| 489 | initialization argument for the class L<Template::Lace::Factory>. You can either | ||
| 490 | attach a component to a full 'prefix-name' pair, as in the examples for 'form' and | ||
| 491 | 'input', or you can create a component 'generator' for an entire prefix by associating | ||
| 492 | the prefix with a coderef which is responsible for returning a component based on the | ||
| 493 | actual name. | ||
| 494 | |||
| 495 | If your components are trivial and/or you don't want to make a full model and Factory for | ||
| 496 | one, you can use the L<Template::Lace::Utils> subroutine C<mk_component> to assist. This | ||
| 497 | creates an instance of L<Template::Lace::ComponentCallback> which is a very simple component | ||
| 498 | defined by a code reference. These type of components are easy to make and can run faster | ||
| 499 | when rendering your templates but have the downside of being defined into your Factory and | ||
| 500 | this reduces reuse. Example: | ||
| 501 | |||
| 502 | use Template::Lace::Utils 'mk_component'; | ||
| 503 | my $factory = Template::Lace::Factory->new( | ||
| 504 | model_class=>'Local::Template::List', | ||
| 505 | component_handlers=>+{ | ||
| 506 | tags => { | ||
| 507 | anchor => mk_component { | ||
| 508 | my ($self, %attrs) = @_; | ||
| 509 | return "<a href='$_{href}' target='$_{target}'>$_{content}</a>"; | ||
| 510 | } | ||
| 511 | }, | ||
| 512 | }, | ||
| 513 | ); | ||
| 514 | |||
| 515 | When using this approach to creating a component, you define a subroutine that will get C<$self> | ||
| 516 | and C<%attrs> (where C<%attrs> is the processed attributes from your component declaration in the | ||
| 517 | template) and you must return | ||
| 518 | either a string or an instance of L<Template::Lace::DOM>. To make things easier for creating | ||
| 519 | simple components we localize '$_' to C<$self> and '%_' to C<%attrs>. For the most part these | ||
| 520 | types of components are the same as those defined via a factory but with fewer overall features | ||
| 521 | (for example they can't currently support C<on_component_add>). The choice for either style | ||
| 522 | will depend on your need for reuse and the overall complexity of the component. | ||
| 523 | |||
| 524 | When the factory is created, it delegates the job of walking the DOM we made from the Model's | ||
| 525 | template and creating a hierarchy of components actually found. All found components need to | ||
| 526 | match something in 'component_handlers' or they will be silently ignored (this is a feature | ||
| 527 | since if you are using client side web components you would want those to be passed on | ||
| 528 | without note). When we call C<create> on the factory we initialize the component | ||
| 529 | hierarchy by calling C<create> on each factory component present and then when we render | ||
| 530 | the template we render each component and replace the full component node with th results | ||
| 531 | of the rendering. | ||
| 532 | |||
| 533 | Here's a full example of a template that is a TODO list which contains a list of | ||
| 534 | items 'TODO' as well as a form to add new items (with the ability to give error feedback to | ||
| 535 | the user if they submit bad items). | ||
| 536 | |||
| 537 | package MyApp::Template::List | ||
| 538 | |||
| 539 | use Moo; | ||
| 540 | |||
| 541 | has [qw/form items copywrite/] => (is=>'ro', required=>1); | ||
| 542 | |||
| 543 | sub template {q[ | ||
| 544 | <view-master title=\'title:content' | ||
| 545 | css=\'@link' | ||
| 546 | meta=\'@meta' | ||
| 547 | body=\'body:content'> | ||
| 548 | <html> | ||
| 549 | <head> | ||
| 550 | <title>Things To Do</title> | ||
| 551 | <link href="/static/summary.css" rel="stylesheet"/> | ||
| 552 | <link href="/static/core.css" rel="stylesheet"/> | ||
| 553 | </head> | ||
| 554 | <body> | ||
| 555 | <view-form id="newtodo" fif='$.form.fif' errors='$.form.errors'> | ||
| 556 | <view-input id='item' | ||
| 557 | label="Todo" | ||
| 558 | name="item" | ||
| 559 | type="text" /> | ||
| 560 | </view-form> | ||
| 561 | <ol id='todos'> | ||
| 562 | <li> -What To Do?- </li> | ||
| 563 | </ol> | ||
| 564 | <view-footer copydate='$.copywrite' /> | ||
| 565 | </body> | ||
| 566 | </html> | ||
| 567 | </view-master> | ||
| 568 | ]} | ||
| 569 | |||
| 570 | sub process_dom { | ||
| 571 | my ($self, $dom) = @_; | ||
| 572 | $dom->ol('#todos', $self->items); | ||
| 573 | } | ||
| 574 | |||
| 575 | 1; | ||
| 576 | |||
| 577 | So this Model declares a template with four components: C<view-master>, C<view-form>, C<view-input>, | ||
| 578 | and C<view-footer>. It also declares a C<process_dom> method to populate the C<ol> at id 'todos'. | ||
| 579 | The C<process_dom> should be straight forward, the C<ol> helper is smart enough to populate the | ||
| 580 | list for you if you pass it an array reference. Component wise we have a hierarchy that looks like | ||
| 581 | this: | ||
| 582 | |||
| 583 | view-master | ||
| 584 | view-form | ||
| 585 | view-input | ||
| 586 | view-footer | ||
| 587 | |||
| 588 | And we could create a C<$factory> for this model with a setup like: | ||
| 589 | |||
| 590 | my $factory = Template::Lace::Factory->new( | ||
| 591 | model_class=>'MyApp::Template::List', | ||
| 592 | component_handlers=>+{ | ||
| 593 | view => { | ||
| 594 | master => Template::Lace::Factory->new(model_class=>'MyApp::Template::Form'), | ||
| 595 | form => Template::Lace::Factory->new(model_class=>'MyApp::Template::Form'), | ||
| 596 | input => Template::Lace::Factory->new(model_class=>'MyApp::Template::Input'), | ||
| 597 | footer => Template::Lace::Factory->new(model_class=>'MyApp::Template::Form'), | ||
| 598 | }, | ||
| 599 | }, | ||
| 600 | ); | ||
| 601 | |||
| 602 | B<NOTE> If you have a strong convention between your component name and the Model class | ||
| 603 | it would be easy to take advantage of the prefix handler code reference option to automatically | ||
| 604 | generate your components as needed. Example given is for ease of understanding! | ||
| 605 | |||
| 606 | The 'view-footer' component is the most simple so lets look at that first: | ||
| 607 | |||
| 608 | package MyApp::View::Footer; | ||
| 609 | |||
| 610 | use Moo; | ||
| 611 | |||
| 612 | has copydate => (is=>'ro', required=>1); | ||
| 613 | |||
| 614 | sub process_dom { | ||
| 615 | my ($self, $dom) = @_; | ||
| 616 | $dom->at('#copy') | ||
| 617 | ->append_content($self->copydate); | ||
| 618 | } | ||
| 619 | |||
| 620 | sub template { | ||
| 621 | my $class = shift; | ||
| 622 | return q[ | ||
| 623 | <section id='footer'> | ||
| 624 | <hr/> | ||
| 625 | <p id='copy'>copyright </p> | ||
| 626 | </section> | ||
| 627 | ]; | ||
| 628 | } | ||
| 629 | |||
| 630 | 1; | ||
| 631 | |||
| 632 | When we call: | ||
| 633 | |||
| 634 | my $renderer = $factory->create(copywrite=>2017, ...); | ||
| 635 | |||
| 636 | That creates an instance of C<MyApp::Template::List> with the attribute 'copywrite' set to '2017'. | ||
| 637 | During this setup, we walk the component hierarchy and when we get to the 'view-footer' component | ||
| 638 | we call "->create(copydate=>$renderer->model->copywrite)" on it to create an instance of that | ||
| 639 | component. Then when we call: | ||
| 640 | |||
| 641 | my $html_string = $renderer->render; | ||
| 642 | |||
| 643 | We call "->render" on that component instance and replace the component node with the new | ||
| 644 | string. In this case that string would look like: | ||
| 645 | |||
| 646 | <section id='footer'> | ||
| 647 | <hr/> | ||
| 648 | <p id='copy'>copyright 2017</p> | ||
| 649 | </section> | ||
| 650 | |||
| 651 | and that would replace the entire node "<view-footer copydate='$.copywrite' />". | ||
| 652 | |||
| 653 | This 'view-footer' example is probably the closest thing to a traditional 'include' or | ||
| 654 | 'partial' template as you might have used in other template systems. For an include this | ||
| 655 | simple it probably seems like a lot more work and code. However as you will see the | ||
| 656 | component system is significantly more powerful than this, and even with an example this | ||
| 657 | simple you get some powerful benefits including a strong separate between HTML markup | ||
| 658 | and display logic, the ability to take full advantage of the power of Perl and a strongly | ||
| 659 | typed interface between your component at the world. These are all things that I believe | ||
| 660 | make your code more maintainable and les buggy. Lets look at a more complex component, | ||
| 661 | the 'view-form' component: | ||
| 662 | |||
| 663 | package MyApp::View::Form; | ||
| 664 | |||
| 665 | use Moo; | ||
| 666 | |||
| 667 | has [qw/id fif errors content/] => (is=>'ro', required=>0); | ||
| 668 | |||
| 669 | sub create_child { | ||
| 670 | my ($self, $child_factory, %init_args) = @_; | ||
| 671 | my $value = $self->fif->{$init_args{name}}; | ||
| 672 | my @errors = @{$self->errors->{$init_args{name}} ||[]}; | ||
| 673 | my $child_component = $child_factory->create( | ||
| 674 | %init_args, | ||
| 675 | value => $value, | ||
| 676 | errors => \@errors); | ||
| 677 | return $child_component; | ||
| 678 | } | ||
| 679 | |||
| 680 | sub process_dom { | ||
| 681 | my ($self, $dom) = @_; | ||
| 682 | $dom->at('form') | ||
| 683 | ->attr(id=>$self->id) | ||
| 684 | ->content($self->content); | ||
| 685 | } | ||
| 686 | |||
| 687 | sub template { | ||
| 688 | my $class = shift; | ||
| 689 | return q[<form></form>]; | ||
| 690 | } | ||
| 691 | |||
| 692 | So I deliberately made a more complex example here so you could see one of the ways | ||
| 693 | that parent / child interaction can occur when there is a component hierarchy. Here | ||
| 694 | we are letting the parent 'view-form' component be responsible for creating its own | ||
| 695 | child components. We are doing this so that the 'view-form' component can give information | ||
| 696 | about state and any errors to any input type child components. We could also have | ||
| 697 | had the children reach into the 'view-form' component to get that information (since | ||
| 698 | each child does have access to the parent via the 'container' attribute, or we could have | ||
| 699 | simple pulled it directly from the Model class via an html attribute, for example: | ||
| 700 | |||
| 701 | <view-input id='item' | ||
| 702 | label="Todo" | ||
| 703 | name="item" | ||
| 704 | type="text" | ||
| 705 | fif="$.form.fif.item" | ||
| 706 | errors="$.form.errors.item"/> | ||
| 707 | |||
| 708 | This part of the system is under active development and consideration, its likely | ||
| 709 | we haven't worked out all the best ways this needs to work, or built in all the API | ||
| 710 | necessary. Discussion welcomed! | ||
| 711 | |||
| 712 | This component also demonstrates how a component that wraps content might work, since it | ||
| 713 | collects that content via the 'content' attribute and inserts into into its own | ||
| 714 | local DOM (via ->content($self->content) using L<Template::Lace::DOM>. Lets see the | ||
| 715 | how the 'view-input' component works: | ||
| 716 | |||
| 717 | package MyApp::View::Input; | ||
| 718 | |||
| 719 | use Moo; | ||
| 720 | |||
| 721 | has [qw/id label name type value errors/] => (is=>'ro'); | ||
| 722 | |||
| 723 | sub process_dom { | ||
| 724 | my ($self, $dom) = @_; | ||
| 725 | |||
| 726 | # Set Label content | ||
| 727 | $dom->at('label') | ||
| 728 | ->content($self->label) | ||
| 729 | ->attr(for=>$self->name); | ||
| 730 | |||
| 731 | # Set Input attributes | ||
| 732 | $dom->at('input')->attr( | ||
| 733 | type=>$self->type, | ||
| 734 | value=>$self->value, | ||
| 735 | id=>$self->id, | ||
| 736 | name=>$self->name); | ||
| 737 | |||
| 738 | # Set Errors or remove error block | ||
| 739 | if($self->errors) { | ||
| 740 | $dom->ol('.errors', $self->errors); | ||
| 741 | } else { | ||
| 742 | $dom->at("div.error")->remove; | ||
| 743 | } | ||
| 744 | } | ||
| 745 | |||
| 746 | sub template { | ||
| 747 | my $class = shift; | ||
| 748 | return q[ | ||
| 749 | <link href="css/main.css" /> | ||
| 750 | <style id="min"> | ||
| 751 | div { border: 1px } | ||
| 752 | </style> | ||
| 753 | <div class="field"> | ||
| 754 | <label>LABEL</label> | ||
| 755 | <input /> | ||
| 756 | </div> | ||
| 757 | <div class="ui error message"> | ||
| 758 | <ol class='errors'> | ||
| 759 | <li>ERROR</li> | ||
| 760 | </ol> | ||
| 761 | </div> | ||
| 762 | ]; | ||
| 763 | } | ||
| 764 | |||
| 765 | 1; | ||
| 766 | |||
| 767 | Here I think you start to get the picture of how components can organize complex | ||
| 768 | display logic without making a messy template. This component easily encapsulates | ||
| 769 | many of the parts of an input form, including label setup, value, type, etc. It | ||
| 770 | also displays any form errors (for example generated by L<HTML::Formhandler> during | ||
| 771 | a POST) or removes the HTML markup around errors. A simple include in TT would | ||
| 772 | have to contain loops and conditionals, and leave you with a template that was | ||
| 773 | not tidy at all. Additionally it would be trivial to make a subclass of this | ||
| 774 | template that changes the markup, adds new features or behaviors. For example | ||
| 775 | you could have a subclass that added CSS markup from one of the popular CSS | ||
| 776 | frameworks. Or you could add javascript to make the form AJAXy. Example: | ||
| 777 | |||
| 778 | package MyApp::Role::View::StyledInput; | ||
| 779 | |||
| 780 | use Moo; | ||
| 781 | |||
| 782 | around 'prepare_dom', sub { | ||
| 783 | my ($orig, $self, $dom, @args) = @_; | ||
| 784 | $dom->at('input') | ||
| 785 | ->attr(class=>'large-input'); | ||
| 786 | return $self->$orig($dom, @args); | ||
| 787 | }; | ||
| 788 | |||
| 789 | You could that add this role to any input type component. Basically the full power | ||
| 790 | of Perl is available! | ||
| 791 | |||
| 792 | Finally, lets look at the 'view-master' component, which is a type of layout component | ||
| 793 | to add common header / footer information to your page (a standard enough thing to do | ||
| 794 | when building a website: | ||
| 795 | |||
| 796 | |||
| 797 | package MyApp::View::Master; | ||
| 798 | |||
| 799 | use Moo; | ||
| 800 | |||
| 801 | has title => (is=>'ro', required=>1); | ||
| 802 | has css => (is=>'ro', required=>1); | ||
| 803 | has meta => (is=>'ro', required=>1); | ||
| 804 | has body => (is=>'ro', required=>1); | ||
| 805 | |||
| 806 | sub on_component_add { | ||
| 807 | my ($self, $dom) = @_; | ||
| 808 | $dom->title($self->title) | ||
| 809 | ->head(sub { $_->append_content($self->css->join) }) | ||
| 810 | ->head(sub { $_->prepend_content($self->meta->join) }) | ||
| 811 | ->body(sub { $_->at('h1')->append($self->body) }) | ||
| 812 | ->at('#header') | ||
| 813 | ->content($self->title); | ||
| 814 | } | ||
| 815 | |||
| 816 | sub template { | ||
| 817 | my $class = shift; | ||
| 818 | return q[ | ||
| 819 | <!DOCTYPE html> | ||
| 820 | <html> | ||
| 821 | <head> | ||
| 822 | <meta charset="utf-8" /> | ||
| 823 | <meta content="width=device-width, initial-scale=1" name="viewport" /> | ||
| 824 | <title>Page Title</title> | ||
| 825 | <link href="/static/base.css" rel="stylesheet" /> | ||
| 826 | <link href="/static/index.css" rel="stylesheet"/ > | ||
| 827 | </head> | ||
| 828 | <body id="body"> | ||
| 829 | <h1 id="header">Intro</h1> | ||
| 830 | </body> | ||
| 831 | </html> | ||
| 832 | ]; | ||
| 833 | } | ||
| 834 | |||
| 835 | 1; | ||
| 836 | |||
| 837 | This is a type of component intended to perform layout work for you. In this case we are creating a common | ||
| 838 | header and footer and some internal market. The values for it attributes come not from the model class | ||
| 839 | but from the contained DOM itself. Lets look again at the top of the component declaration: | ||
| 840 | |||
| 841 | <view-master title=\'title:content' | ||
| 842 | css=\'@link' | ||
| 843 | meta=\'@meta' | ||
| 844 | body=\'body:content'> | ||
| 845 | |||
| 846 | So four attributes, all coming from the DOM associated with the 'content' area of this component. We | ||
| 847 | grab the content of the title tag and the content of the HTML body tag, as well as the collection (if | ||
| 848 | any) of the link takes (for css style sheets) and any template specific meta tags. | ||
| 849 | |||
| 850 | If you are looking carefully you have noticed instead of a 'process_dom' method we have a 'on_component_add' method. We could do this with 'process_dom' but that method runs for every request and since this overlay contains no dynamic request bound information its more efficient to run it once ('on_component_add' runs once at setup time; the change it makes becomes part of the base DOM which is cloned for every following request). So 'on_component_add' is like 'prepare_dom' except it allows a component to modify the DOM of the view that is calling it instead of its own. | ||
| 851 | |||
| 852 | Here's a sample of the actual result, rendering all the components (you can peek at the repository which has all the code for these examples to see how it all works) | ||
| 853 | |||
| 854 | <html> | ||
| 855 | <head> | ||
| 856 | <meta startup="Fri Mar 31 08:43:24 2017"> | ||
| 857 | <meta charset="utf-8"> | ||
| 858 | <meta content="width=device-width, initial-scale=1" name="viewport"> | ||
| 859 | <title> | ||
| 860 | Things To Do | ||
| 861 | </title> | ||
| 862 | <link href="/static/base.css" rel="stylesheet" type="text/css"> | ||
| 863 | <link href="/css/input.min.css" rel="stylesheet" type="text/css"> | ||
| 864 | <script src="/js/input.min.js" type="text/javascript"></script> | ||
| 865 | </head> | ||
| 866 | <body id="body"> | ||
| 867 | <h1> | ||
| 868 | Things To Do | ||
| 869 | </h1> | ||
| 870 | <form id="newtodo"> | ||
| 871 | <div class="field"> | ||
| 872 | <label for="item">Todo</label> | ||
| 873 | <input id="item" name="item" type="text" value="milk"> | ||
| 874 | </div> | ||
| 875 | <div class="ui error message"> | ||
| 876 | <ol class="errors"> | ||
| 877 | <li>too short | ||
| 878 | </li> | ||
| 879 | <li>too similar it existing item | ||
| 880 | </li> | ||
| 881 | </ol> | ||
| 882 | </div> | ||
| 883 | </form> | ||
| 884 | <ol id="todos"> | ||
| 885 | <li>Buy Milk | ||
| 886 | </li> | ||
| 887 | <li>Walk Dog | ||
| 888 | </li> | ||
| 889 | </ol> | ||
| 890 | <section id="footer"> | ||
| 891 | <hr> | ||
| 892 | <p id="copy"> | ||
| 893 | copyright 2017 | ||
| 894 | </p> | ||
| 895 | </section> | ||
| 896 | </body> | ||
| 897 | </html> | ||
| 898 | |||
| 899 | So even though we have a page with a lot happening, we can write a model class that focuses | ||
| 900 | just on the primary task (display the list of Todos) and let components handle the other work. | ||
| 901 | A complex template can be logically divided into clear chunks, each dedicated to one function | ||
| 902 | and each with a clearly defined, strongly typed interface. I believe this leads to well | ||
| 903 | organized and concise templates that are maintainable over the long term. | ||
| 904 | |||
| 905 | You can review the documentation for each of the main classes in this distribution, and/or | ||
| 906 | review the test cases for more examples. Or if you want to use this for building web sites | ||
| 907 | immediately, see L<Catalyst::View::Template::Lace> as you quickest path. | ||
| 908 | |||
| 909 | =head1 IMPORTANT NOTE REGARDING VALID HTML | ||
| 910 | |||
| 911 | Please note that L<Mojo::DOM58> tends to enforce rule regarding valid HTML5. For example, you | ||
| 912 | cannot nest a block level element inside a 'P' element. This might at times lead to some | ||
| 913 | surprising results in your output. | ||
| 914 | |||
| 915 | =head1 AUTHOR | ||
| 916 | |||
| 917 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||
| 918 | |||
| 919 | =head1 SEE ALSO | ||
| 920 | |||
| 921 | L<Template::Lace::Factory>, L<Template::Lace::Component>, L<Template::Lace::Renderer>, | ||
| 922 | L<Template::Lace::DOM>, L<Template::Lace::Model::AutoTemplate>, | ||
| 923 | and L<Template::Lace::Factory::InferInitArgsRole> | ||
| 924 | |||
| 925 | Other classes defined in this distribution | ||
| 926 | |||
| 927 | L<Mojo::DOM58>, L<HTML::Zoom>. Both of these are approaches to programmatically examining and | ||
| 928 | altering a DOM. | ||
| 929 | |||
| 930 | L<Template::Semantic> is a similar system that uses XPATH instead of a CSS inspired matching | ||
| 931 | specification. It has more dependencies (including L<XML::LibXML> and doesn't separate the actual | ||
| 932 | template data from the directives. You might find this more simple approach appealing, | ||
| 933 | so its worth alook. | ||
| 934 | |||
| 935 | L<HTML::Seamstress> Seems to also be prior art along these lines but I have trouble following | ||
| 936 | the code and it seems not active. Might be worth looking at at least for ideas! | ||
| 937 | |||
| 938 | L<Template::Lace> A previous work of my along the same lines but based on pure.js | ||
| 939 | L<http://beebole.com/pure/>. | ||
| 940 | |||
| 941 | L<PLift>, uses L<XML::LibXML>. | ||
| 942 | |||
| 943 | L<Catalyst::View::Template::Lace> Catalyst adaptor for this with some L<Catalyst> specific | ||
| 944 | enhancements. | ||
| 945 | |||
| 946 | =head1 COPYRIGHT & LICENSE | ||
| 947 | |||
| 948 | Copyright 2017, John Napiorkowski L<email:jjnapiork@cpan.org> | ||
| 949 | |||
| 950 | This library is free software; you can redistribute it and/or modify it under | ||
| 951 | the same terms as Perl itself. | ||
| 952 | |||
| 953 | =cut |