Is there a html-only templates system for php?

元气小坏坏 提交于 2019-12-22 05:18:21

问题


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?


回答1:


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:


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.




回答3:


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>



回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/3248235/is-there-a-html-only-templates-system-for-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!