Respect / Template

Experimental, HTML-only templating engine
Other
38 stars 6 forks source link

Use cases #1

Closed alganet closed 12 years ago

alganet commented 12 years ago

This issue is meant to discuss early use cases on the Respect\Template development. Feel free to contribute with any comments.

alganet commented 12 years ago

The simplest use case ever is simple tag injection. Given a typical pure HTML document:

<!doctype html>
<html>
  <head><title></title></head>
  <body><h1></h1></body>
</html>

And a pure PHP view-injector:

<?php
use Respect\Template\Html;

$template = new Html('index.html');
$template["title"] = "Hello!";
$template["body h1"] = "Welcome to Respect\Template";
echo $template;

The output is:

<!doctype html>
<html>
  <head><title>Hello!</title></head>
  <body><h1>Welcome to Respect\Template</h1></body>
</html>
alganet commented 12 years ago

A potential use case wildcard is the selector engine. In the sample above, the selector engine sample is simple. We need a selector engine as good as Sizzle.

Symfony has a selector engine we could reuse. I know about phpQuery as well, but the project is a little off the PHP community.

What do you think?

augustohp commented 12 years ago

IMHO the initial restriction to use only #ids is not a big disadvantage in the beginning of the project. My biggest concern is not how we query the elements but how this code is going to grow with time and how it is going to perform. In a template use-case scenario, performance is a greater limitation than what kind of selectors we support.

augustohp commented 12 years ago

Iterating over elements and rendering them as lists, tables, options, etc ....

<!doctype html>
<html>
  <body><ul id="names"></ul></body>
</html>
<?php
$template = new Html('index.html');
$template['#names'] = array("Chuck Norris", "Santa");
echo $template;
<!doctype html>
<html>
  <body><ul id="names"><li>Chuck Norris></li><li>Santa</li></ul></body>
</html>
alganet commented 12 years ago

To get the appropriate performance I believe we have two approaches:

Compiling is possible injecting PHP inside the HTML then evaluating using eval() or saving it to a temporary folder.

I'm not a fan of code generation. Adds too much layers and complexity. But unfortunately this could be the only way to get performance. Caching the DOM tree doen't spare the overhead caused by saveHTML().

Pure HTML:

<!doctype html>
<html>
  <body><ul id="names"></ul></body>
</html>

PHP View:

<?php
$template = new Html('index.html');
$template['#names'] = array("Chuck Norris", "Santa");
echo $template;

Intermediate compiled template:

<!doctype html>
<html>
  <body><ul id="names"><?php foreach($data["#names"] as $item): ?><li><?php echo $item;?></li><?php endforeach; ?></ul></body>
</html>

Final result:

<!doctype html>
<html>
  <body><ul id="names"><li>Chuck Norris></li><li>Santa</li></ul></body>
</html>
augustohp commented 12 years ago

I tried a lot of things with the DOM objects, and man ... they are slow by default. They do all kind of validation with the objects with the correct DTD, they actually always have a valid XML representation of the DOM (that is what you would expect from a DOM extension) and that takes time, if you try to validade a simple piece html it would take some seconds for it to run, that makes the server do a boo boo in no time.

I believe that caching should be done with the whole result, and not just the DOM tree, but i am not quite sure of that yet. The idea of compiling code is good and I agree with the complexity issue, and if i am not mistaken, Haanga does something like that.

alganet commented 12 years ago

Haang, Twig, queryTemplates (which are similar to ours) and Smarty all generates intermediate code.

I have faith in the DOMDocument. I've learned it from this post from Padraic Brady: http://blog.astrumfutura.com/2010/07/html-sanitisation-benchmarking-with-wibble-zf-proposal/ We just need to tune our code to benefit from it.

augustohp commented 12 years ago

Idea 1: What do you think of working with a base template (html) that has a lot of dummy data? The designer makes an HTML to be able to do all the CSS and Javascript with lots and lots of dummy data (lorem ipsum), we could load that and ignore all dummy data and populate the template on that.

Idea 2: Plugins could be used to a lot of things. I put very little effort thinking in this, but they are interesting and would be very handy later on. I just wanted to leave some space to have this someday ...

Ideia 3: Use as example the CSS Zengarden as template with out data.

augustohp commented 12 years ago

The given use cases should work with the current code, forgot to mention that ...