Progress on Template Architecture
I've been working on Respect\Template this week and achieved a nice result. My current branch has some killing features:
<?php
use Respect\Template\Html;
$template = new Html('template.html');
$template['posts']->items('ul', 'li', Html::named(
'title' => Html::text('h3'),
'text' => Html::text('section'),
'date' => Html::text('footer time'),
'author' => Html::named(
'name' => Html::text('.fn')
)
));
$posts = array(
array(
'title' => 'Hello',
'title' => 'Hello World',
'date' => '2012-10-10',
'author' => array('name' => 'Gaigalas'),
),
array(
'title' => 'Lorem',
'title' => 'Lorem Ipsun',
'date' => '2013-10-10',
'author' => array('name' => 'Gaigalas'),
),
);
$template->render($posts);
Given this template:
<ul>
<li>
<article>
<h3>Some Blog Title!</h3>
<section>Some Blog text...</section>
<footer>
<time>2000-01-01</time>
<span class="vcard fn">John</span>
</footer>
</article>
</li>
</ul>
Would produce this:
<ul>
<li>
<article>
<h3>Hello</h3>
<section>Hello World</section>
<footer>
<time>2012-10-10</time>
<span class="vcard fn">Gaigalas</span>
</footer>
</article>
</li>
<li>
<article>
<h3>Lorem</h3>
<section>Lorem Ipsum</section>
<footer>
<time>2013-10-10</time>
<span class="vcard fn">Gaigalas</span>
</footer>
</article>
</li>
</ul>
There are some other tricks as well. One of them is selective compilation. All templates can be run in real time or compiled in any state. When I say any state, I mean this:
Compiling a template without value:
$template = new Html('template.html');
$template['title']->text('h3');
$template->compile();
Will generate something similar to <h3><?php echo $title;?></h3> in the final compilation. Now, in any moment, if you feed this template with data before compilation:
$template = new Html('template.html');
$template['title']->text('h3');
$template->compile(array('title' => 'Hello'));
Since the result is compiled selectively, now we have <h3>Hello</h3> in our compiled PHP template. This is a huge performance tool that can save a lot of template logic that doesn't change often like translations, page titles, some links =)
All of this is sort of working. Two of the main operators (the abstraction for a template operation) are not compiling their results yet (but they work in real time).
Most of this is tested (behavior style) and doc-commented. I broke BC with the old Template =(
I'm pushing this into the develop branch. Please review!
https://github.com/Respect/Template/blob/develop
@augustohp @henriquemoody @nickl- @iannsp @wesleyvicthor
Travis likes it! https://travis-ci.org/Respect/Template/jobs/4745448
Classes without coverage are those I've mentioned that are without compilation support.
Please refer to the HtmlTest for some samples of both real time and compilation:
https://github.com/Respect/Template/blob/develop/tests/library/Respect/Template/HtmlTest.php
And see how easy is to build a basic operator like text($selector):
https://github.com/Respect/Template/blob/develop/library/Respect/Template/Operators/Text.php
Wow! Impressive! Still reviewing things, but as far as I got:
:+1: on the compilation. IMO, you nailed it! :+1: on the bc break. :+1: on the operators.
One note on the abstract classes of operators though: What do you think of moving the abstract code to an interface and put the concrete code on the Operator class?
In original post, you have two "title" inside array of posts, I think you want to put "text" in second.
semantics good one.
Could I work on any type of template ?
something like:
<?php $template = new Xml('template.xml'); // new Yaml('template.yml'); new Rdf('template.rdf'); ?>
:panda_face:
Only XML-based templates I believe. The code relies a lot on DOM.
Nice! Will get back to this ....