6
votes

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?

5
I've been philosophing about this too. In theory you could collect all html output from your application, pipe it into phpQuery, and then inject the actual content like PURE does it. In theory. - mario
The Hello World Example of PURE can easily be reimplemented with DOM in a dozen lines. It would use XPath for the DOM queries though. If you dont mind learning another markup language, have a look at XSLT. - Gordon
I think the same as Gordon, if you go to the route of implementing something like PURE in PHP(which shouldn't be that difficult), and don't have a DOM engine available, XPATH is the way to go. PURE came from my XSLT past meeting with JSON. But here I disagree with Gordon, I wouldn't go back to XSLT for nothing. - Mic

5 Answers

4
votes

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.

2
votes

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.

1
votes

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>
1
votes

Interesting starts. But I'm impressed why we can't have diferent template with specimen text to change the view and the different state of elements - and extract the code to create exiting template systems or just fill it up with content. Find the workflow feels so broken an complicate - don't like mixing html with php / js nor writing abstract template systems. Need to much brain - to much commication without easy visualisations.

0
votes

I think Psttt! templating engine for php is exactly what are you looking for, it keeps your html template intact allowing you to better reuse your htmls.

full source code here http://github.com/givanz/psttt