开发者

PHP - Looking for an HTML Generator/Manager [closed]

开发者 https://www.devze.com 2022-12-25 17:22 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 7 years ago.

Improve this question 开发者_高级运维

I'm trying to find a tool that I can use to generate markup programatically (non-irl word). Here is an example of what I'm trying to accomplish 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?


I'm working on a simple DSL that addresses this common task. It's called htmlgen, mirrored on packagist.

Here's an example -

use function htmlgen\html as h;

h('#wrapper',
  h('h1.title', 'Hello, World'),
  h('p',
    h('comment', 'link to something'),
    h('a', ['href'=>'https://duckduckgo.com'], 'search the internet')
  )
);

Here's the output (actual output does not have whitespace)

<div id="wrapper">
  <h1 class="title">Hello, World</h1>
  <p>
    <!-- link to something -->
    <a href="https://duckduckgo.com">search the internet</a>
  </p>
</div>

It's very new but at ~200 lines of source, it's already very powerful. Fork it and make adjustments or send me a note with suggestions.


There's one bundled as standard in PHP: http://php.net/manual/en/book.dom.php

C.


Writing your own simple framework is very simple and is a great learning experience. You can implement this in merely an hour or two, in fact I just did for a recent project only a couple weeks ago.

What I recommend is:

  1. Create a simple .HTML template with placeholder tags, I use the syntax %HTML_TITLE% and your setTitle method simple does a STR-REPLACE.
  2. The same holds true for JS, CSS
  3. Since you may not add certain pieces everytime it's important than it your generate() function you remove any remaining %% tags with a %\w+% regex.


i'm not aware of such a library, but it would be quite easy to write one, for example, based on XMLWriter.

That said, it's worth noting that php was especially designed to avoid things like this. I'd suggest a templating engine or even 'pure' php for html output.

0

精彩评论

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