We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

PHP DOM Manipulation (offtop)

Hello.

I have an idea to use clean HTML-template in MVC in the following way:

  1. Designer generates HTML-template.
  2. On the server-side, Back-developer gets this HTML-template and fills it by data. The point: I don't want include ANY logic in the Hypertext Markup (so templates-engines is not variant). It's just like AJAX-development, but on the server-side.

I know that there is phpQuery and simpleHtmlDom, but it seems they have serious problems with speed.

Does somebody know any good instrument for PHP DOM manipulation?

No kidding?? PHP is good at printing out data to the client. DOM manipulation is always resource and time consuming. May I ask you what is the reason for it?



36.0k

I'm not kidding. I have team: designers, developers and so on. I want totally isolate they works.

We have fully workable AJAX-based web-system, that works by the earlier described principle: JS-script gets JSON-data and HTML-markup, then fills HTML-markup with the necessary data. But now we need to create JS-independent system, so I want that principle "get data and markup then combine them" worked on server-side.

When I searching for the existing examples, I found the article: https://www.workingsoftware.com.au/page/Your_templating_engine_sucks_and_everything_you_have_ever_written_is_spaghetti_code_yes_you So, I'm know that I'm not alone :)

Are you referring to your experience with Node.JS? If so, then PHP principles of work are slightly different. These two libraries you mentioned are the most popular. If you need speed you can either deal with plain DOM document or XHTML / XSLT. Alternative route is to make backend prepare and send data and then you use AngularJS or other JavaScript frontend library to render the page on the client.

OFFTOPIC

Several disadvantages I can list here are DOM manipulation in PHP is not perfect and building all the routines and behaviors can actually take more time and resources than simply printing HTML code with PHP. Support and further changes e.g., redesign may become harder. DOM manipulation uses more server resources and slower. Also it adds another layer between logic and presentation which is the "presentation logic". You would need to cache pages for speed and it will bring more problems to consider and overcome.

Seriously I don't see any advantage to prefer this:

<form>
    <dl class="error" id="whatevsError">
        <dt>You have to do something about this</dt>
        <dd>The reason we ask you do do this is so that we can laugh all the way to the bank</dd>
    </dl>
    <dl>
        <dt><label for="whatevs">Whatevs:</label</dt>
        <dd><input type="text" name="whatevs" id="whatevs" /></dd>
    </dl>
</form>

To render this using PHP you might do something this:

$dom = $this->loadDocument($my_template);

if(!checkFormError('whatevs'))
    $dom->getElementById('whatevsError')->parentNode->removeChild($dom->getElementById('whatevsError'));

over this:

<form>
    <?php if(!checkFormError('whatevs')) { ?>
    <dl class="error" id="whatevsError">
        <dt>You have to do something about this</dt>
        <dd>The reason we ask you do do this is so that we can laugh all the way to the bank</dd>
    </dl>
    <?php } ?>
    <dl>
        <dt><label for="whatevs">Whatevs:</label</dt>
        <dd><input type="text" name="whatevs" id="whatevs" /></dd>
    </dl>
</form>


36.0k

Thank you for the answer!

As I mentioned above, I just want use the same experience as in the "Ajax-compiling-pages", but in the "Server-side-compiling-pages". No, I don't use Node.JS in the front-page-rendering, I use it in the API.

Yes, I understand that work with DOM on the server-side will be slowly than work in ordinary page rendering style. But my best advantage is the reducing time and money on designers and developers interaction. Another advantage is more readable code and so on.

I found QueryPath library for my purposes, but it's adds from 50% till 100% time to the script execution. Now I'm really must take decision what to use: "spaghetti code" or "DOM manipulation"....

P.S. Your example is quite simple, IRL things may become much harder such as: case-trees, tonns of the loops and decision-making on every-attribute of the DOM-element.