The by far most powerful feature of templado is the application of view models into an existing HTML document. To turn a regular HTML document into a templado template for use with view models, RDFa Lite annotations need to get added - if they do not already exist.

RDFa Lite is a small subset of RDFa, the Resource Description Framework defined by the W3C, consisting of some few attributes that are used to express machine-readable data in Web documents like HTML, SVG, and XML. Templado, as of version 2.2.0, can make use the RDFa attributes 'property' and 'typeof'.

A very basic HTML document may look like this: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <title property="title">Sample Title</title> </head> <body> <h1 property="title">Sample Title</h1> <p property="text">Lorem ipsum dolor sit amet, aliquyam</p> </body> </html>

Every element within that HTML document containing a property attribute will be addressed by templado. A given view model must implement methods with names matching the values of the property attribute, so templado can call them - optionally prefixed by "get" - like in this example: <?php declare(strict_types = 1); class SampleView { public function getTitle(): string { return 'My First Templado View'; } public function getText(): string { return 'This is the text for the first paragraph'; } }

To apply the above view model onto the base HTML loaded earlier, we simply use the templado API: <?php declare(strict_types = 1); $html->applyViewModel(new SampleView()); echo $html->asString();

The echo will output the transformed HTML, which will look like this: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title property="title">My First Templado View</title> </head> <body> <h1 property="title">My First Templado View</h1> <p property="text">This is the text for the first paragraph</p> </body> </html>

While using such a basic view model might actually get you quite far already, the full potential of templado get into play with nested structures and lists. To achieve that, templado's behavior changes depending on the return type of the method called. As of version 2.2.0, the following return types are generally supported: array/iterator, boolean, null and object. In case other types are returned, the engine will throw an exception.

Let's have a look at the individual types in more detail.

Array & Iterator

The current node will be copied as many times as there are items in the returned array or iterator. As the template may contain redundant nodes of the same property, templado will clean up so that the template may illustrate the looks of a longer list than there actually are items in the array or iterator. <?php declare(strict_types = 1); class SampleView { // ... public function getText(): array { return [ 'This is the text for the first paragraph', 'This is the text for the second paragraph' ]; } } <!DOCTYPE html> <html lang="en"> <!-- .... --> <body> <!-- .... --> <p property="text">This is the text for the first paragraph</p> <p property="text">This is the text for the second paragraph</p> </body> </html>

Boolean

A boolean false can be used to remove a node or attribute. A boolean true is currently not processed and silently ignored. <?php declare(strict_types = 1); class SampleView { // ... public function getText(): bool { return false; } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title property="title">My First Templado View</title> </head> <body> <h1 property="title">My First Templado View</h1> </body> </html>

null

A null return value tells templado to simply do nothing with the current context.

object

An object does not necessarily provide any content of its own but first of all sets the context for nested childnodes of the current context node. Nested properties will be resolved against this object until another returned object is taking over or the current context processing is completed.

Additionally, for every attribute of the current node, templado will check whether or not a method with the name of the attribute is defined on this object - with or without the prefixed "get". If so, it will be called, passing the current value of the attribute along. Only string and false to remove the attribute are supported as a return type.

To set the node's text content - removing all potentially existing child nodes -, the object needs to provide the method `asString`. Only string and null are supported as return type.

In case the current context node has the RDFa attribute `typeof` set, this object must provide the accompanying method `typeOf`. Templado will call this method to determine the matching set of nodes.
Non-Matching nodes will be removed. If no matching node can be found, an exception is thrown.

<?php declare(strict_types = 1); class SampleView { public function getChild(): ChildView { return new ChildView(); } } class ChildView { public function asString(string $text): string { return 'new text'; } public function getClass(string $text): string { return $text . ' current'; } public function typeOf(): string { return 'type-A'; } }