开发者

PHP5.3 Oriented Object Modeling for HTML generation, a possibility or performance bottleneck?

The main motivation in this is that I was very tired or using php tags to write php views that were very ugly. Consider this approach:

<?php
<ul>
    <?php echo foreach($products as $product): ?>
    <li><?php echo $product->getName()?><li>
</ul>

This is the basic approach of trivial MVC with views, but I was tired of that, so I use phpQuery instead:

<?php
$ul = pq('<ul />');

foreach($products as $product) {
    $ul->append(pq('<li />')
        ->html($product->getName())
    );
}

echo $ul;

This is much cleaner. Unfortunately, phpQuery does not seem active on development. The thing is, phpQuery is programmed in a procedural way and is really hard to understand and maintain and contains some bugs that make it rather useless in most DOM manipulation cases. I was thinking of writing an OO approach to DOM writing based on jQuery using Zend Framework's component Zend_Dom_Query, with the same approach of phpQuery: same interface as jQuery.

After some search on the net on PHP HTML Generators, I cannot seem to find much that has been done on that part, and my question is, is using OO with PHP to generate HTML a performance bottleneck, since all static HTML would be server side generated?

I know there is the HAML approach, but I like the idea of DOM manipulation, so that it is possible to inject html at any time anywhere during the script execution. DOM manipulation on the server side leads to view script inheritence like this:

//a parent index.phtml
$container = pq('<div />')
    ->addClass('.container')
    ->html($this->_("Hello开发者_Go百科 World!"));

$this->setRenderReturn($container);

//a child index.phtml
$container = $this->renderParent(__FILE__); //retrieves the parent just like the conventional parent:: in php scripts
$container->addClass('anotherClass');

$this->setRenderReturn($container);

Also, in case I missed a PHP library that does just that, I would gladly appreciate if someone could point me to it.


In direct answer to your question...

is using OO with PHP to generate HTML a performance bottleneck, since all static HTML would be server side generated?

Yes.

You should have a look at Twig and particularly the extensions added in Symfony 2. It supports template inheritance as well as named sections for overwriting / inserting markup.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜