Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a html-only templates system for php?

I have started coding in clojure, and I'm really impressed by Enlive. One thing that I really like about it is that Enlive uses html-only templates. So a template is a file with html in it, ending in .html, simple as that. It gets parsed into a dom tree and then that dom tree is manipulated by clojure/enlive, combined, made dynamic, etc. No syntax in the html template files, a beautifully clean separation.

Another example of a similar system, done via javascript, is PURE.

Is there anything like that in php? Or, in general, any ways of doing html-only templating?

like image 913
Kzqai Avatar asked Jul 14 '10 16:07

Kzqai


People also ask

What is template system PHP?

A templating system provides a way of separating the code in a web page from the layout of that page. In larger projects, templates can be used to allow designers to deal exclusively with designing web pages and programmers to deal (more or less) exclusively with programming.

What is templating HTML?

Definition and Usage. The <template> tag is used as a container to hold some HTML content hidden from the user when the page loads. The content inside <template> can be rendered later with a JavaScript.

Which of the following is a PHP based template?

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.


3 Answers

Fascinated to hear of Enlive. I've been thinking about this concept for a few years and have hacked together something in PHP that shares some principles: templates are pure HTML, and you "fill" them by addressing content to a specific node in the document using CSS or XPath.

$t = new Template('yourfile.html');
$t->fill('#nav',$someMarkup);
$t->fill('#header',$otherMarkup);

I also experimented with throwing together some provisions for separating the content out into a "stylesheet" of sorts... well, "stylesheet" is the wrong word. I'm calling them content-addressing sheets (the project is called CAST, for content-addressed-style-templating). A CAS looks like you might expect:

.col #foot {
    content: 'foot';
}

#content {
    content: file_get_contents('pangolin.txt');
}

The values of content are assumed to be PHP expressions. There's some provision for setting up PHP that applies across selectors, too.

Take a run of the cssfill.php script that's in the tarball (best invocation is probably ./cssfill.php pangolin.cas pangolin.html, you might have to change the path to your php interpreter inside cssfill.php), take a look at the output, compare with the input files.

If this post generates any enthusiasm for the idea in you, feel free to let me know. I've been wondering whether this was a crazy idea or whether it has a place, if it does, I'd be happy to turn it into a thoughtfully released open source project as opposed to a tarball randomly tossed onto the internet.

like image 75
Weston C Avatar answered Oct 23 '22 23:10

Weston C


You can check phptal.org. It's a template engine for PHP that uses HTML tags. But it's not that fast.

You can also check other projects like twig-project.org witch is waaay faster.

like image 36
esgy Avatar answered Oct 23 '22 23:10

esgy


Checkout PHPTAL. PHPTAL is a XML/XHTML template library for PHP5 which provides compiled templates and fine-grained caching. Templates are defined in pure XML/HTML markup.

<div class="item" tal:repeat="item itemsArray">
    <span tal:condition="item/hasDate" tal:replace="item/getDate"/>
    <a href="${item/getUrl}" tal:content="item/getTitle"/>
  <p tal:content="value/getContent"/>
</div>
like image 1
Benjamin Cremer Avatar answered Oct 23 '22 22:10

Benjamin Cremer