PHP - HTML generator / manager search

I am trying to find a tool that I can use to create markup programmatically (non-irl word). Here is an example of what I'm trying to execute in pseudo code ...

$htmlDoc = new HTML_Document();

$htmlDoc->setTitle('Cool Title');

$htmlDoc->addJs('some_js_file.js');
$htmlDoc->addStylesheet('some_css_file.css');

$divElement = new HTML_Element_Div();
$htmlDoc->append('body', $divElement);

$output = $htmlDoc->generate();

What is the best tool for this?

+3
source share
4 answers

Where there is one standard in PHP: http://php.net/manual/en/book.dom.php

WITH.

+2
source

I am working on a simple DSL that solves this general problem. It was called htmlgen and is hosted on Github.

I just clicked 2.x with some significant improvements.

Here is an example

use function htmlgen\html as h;

h('#wrapper',
  h('h1.title', 'Hello, World'),
  h('p',
    h('comment', 'link to project'),
    h('a', ['href'=>'https://github.com/naomik/htmlgen'], 'See htmlgen on Github')
  )
);

Here's the conclusion (the actual output has no spaces)

<div id="wrapper">
  <h1 class="title">Hello, World</h1>
  <p>
    <!-- link to project -->
    <a href="https://github.com/naomik/htmlgen">See htmlgen on Github</a>
  </p>
</div>

, ~ 200 , . .

+5

. , .

:

  • .HTML -, % HTML_TITLE%, setTitle STR-REPLACE.
  • JS, CSS
  • , , generate(), %% %\w +% regex.
+2

, , , XMLWriter.

However, it is worth noting that php was specifically designed to avoid such situations. I would suggest a template engine or even a “clean” php for html output.

+1
source

Source: https://habr.com/ru/post/1739186/


All Articles