Skip to content

Progress on Template Architecture #30

@alganet

Description

@alganet

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions