Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Progress on Template Architecture #30

Open
alganet opened this issue Feb 12, 2013 · 8 comments
Open

Progress on Template Architecture #30

alganet opened this issue Feb 12, 2013 · 8 comments

Comments

@alganet
Copy link
Member

alganet commented Feb 12, 2013

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

@alganet
Copy link
Member Author

alganet commented Feb 12, 2013

Travis likes it! https://travis-ci.org/Respect/Template/jobs/4745448

Classes without coverage are those I've mentioned that are without compilation support.

@alganet
Copy link
Member Author

alganet commented Feb 12, 2013

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

@augustohp
Copy link
Member

Wow! Impressive! Still reviewing things, but as far as I got:

👍 on the compilation. IMO, you nailed it!
👍 on the bc break.
👍 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?

@dgmike
Copy link

dgmike commented Feb 15, 2013

In original post, you have two "title" inside array of posts, I think you want to put "text" in second.

@wesleyvicthor
Copy link
Member

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'); ?>

@wesleyvicthor
Copy link
Member

🐼

@alganet
Copy link
Member Author

alganet commented Feb 16, 2013

Only XML-based templates I believe. The code relies a lot on DOM.

@nickl-
Copy link
Member

nickl- commented Feb 23, 2013

Nice! Will get back to this ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants