PHP Class to Generate HTML?

I guess it could be done with http://www.php.net/manual/en/class.domdocument.php. But that isn't really a good way to do it.

I agree that your code code sample isn't very clear, you could consider something like:

<ul>
<?php foreach ($items as $item): ?>
    <li>
        <?=$item['something']?>
        <?php if ($item['foo'] == 'bar'): ?>
        <ul>
            <li>bar</li>
        </ul>
        <?php else: ?>
        <ul>
            <li>foo</li>
        </ul>
        <?php endif; ?>
    </li>
<?php endforeach; ?>
<ul>

That's a lot better imho, I use it like that in my views.

Btw, you should validate your html output. For example, a div-element isn't allowed in a li-element.

edit: Obviously, the following code:

<?php if ($item['foo'] == 'bar'): ?>
<ul>
    <li>bar</li>
</ul>
<?php else: ?>
<ul>
    <li>foo</li>
</ul>
<?php endif; ?>

Could be replaced by:

<ul>
    <li><?=($item['foo'] == 'bar' ? 'bar' : 'foo')?></li>
</ul>

I'm working on a simple DSL that addresses this common task. It's called htmlgen and it's being hosted on Github.

I just pushed 2.x with some major improvements.

Here's 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 output (actual output does not have whitespace)

<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>

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.


The main idea of generating HTML is to keep HTML as is.
Just divide your script into 2 parts: prepare data part and display data part. A latter one should contain mostly HTML with some PHP control structures. Of course, there should be as less PHP code as possible. Move all unnecessary PHP code into first part.

Well to sum up all of the above:
there should be nothing to unserialize in the $details array. Have your data prepared already

<? foreach ($details as $d): ?>              
<li class="classRow orphan">
  <div class="orphan" style="display:none"><? if ($d->get_info('orphan')): ?>orphan<? endif ?></div>
   <div class="classNumbers" id="<?= $d->get_info('class ID') ?>" style="display:none"></div> 
    <div class="rowBG" style="overflow:hidden;width:100%">
     <div class="startTime"></div>
      <div class="details">
       <span class="classes"><?= $d->get_info('class number') ?></span> -
        <input class="detailInput" type="text" value="<?= $d->get_info('class description') ?>"/>
         <div class="editButton">
          <a class="editExpand">options(+)</a>
         </div>
        </div>
        <div class="interval">
         <input class="intervalInput" type="text" value="<?= $d->get_info('interval') ?>" maxlength="5"/>
        </div>
        <div class="numRiders">
         <input class="numRidersInput" type="text" value="<?= $d->get_info('num riders') ?>"/>
        </div>
       </div>
       <div class="classOptions">
       <div class="selectRingMove">Move to Ring:
        <select id="ringSwap">
         <option>Select A Ring</option>
<?= foreach (get_ring_options() as $value => $option): ?>
     <option value="<?=$value?>"><?=$option?></option>
<? endforeach ?>
         </select>
       </div>
       <div class="signUpContainer">Sign-Up
        <input type="checkbox" class="signUp" <? if (!$d->get_info('online sign up'): ?>checked<? endif ?>/>
       </div>

and so on.

The main rule should be DRY: Do not Repeat Yourself. Do not repeat blocks of code if there is only one word difference.
But enclose that word into condition

Note that get_ring_options() was replaced with proper code.
Do not make functions that return HTML, but an array instead.

Tags:

Html

Php