4

I'm a PHP newb. Sorry if this is an FAQ...

Let's say I have this HTML table:

<table width="100%" cellpadding="12" cellspacing="0" border="0">
  <tr bgcolor="black">
    <td align="left">
      <img src="logo.gif" />
    </td>
    <td>
      <h1>Hello</h1>
    </td>
    <td align="right">
      <img src="logo.gif" />
    </td>
  </tr>
</table>

Instead of escaping to HTML (?> ... <?php) or using echo with manual string construction, I'd like to use PHP functions to generate the code. I cooked up a library so that the above example can be generated with this:

echo table(
  array('width' => '100%', 'cellpadding' => '12', 'cellspacing' => '0', 'border' => '0'),
    tr(
      array('bgcolor' => 'black'),
      td(
        array('align' => 'left'),
        img(array('src' => 'logo.gif'))),
      td(array(), h1(array(), 'Hello')),
      td(array('align' => 'right'), img(array('src' => 'logo.gif')))));

My question is, is there already a popular or commonly used library that does this?

dharmatech
  • 8,979
  • 8
  • 42
  • 88
  • 1
    If it ain't broke. Don't fix it. You wrote it and it works. That great! Why look for a library? – Cole Tobin Jun 23 '12 at 22:11
  • 1
    Interesting library. I like the idea of nesting function calls matching the intended output structure. I'm curious to know how this would perform for heavily nested html. – Jonathan Beebe Jun 23 '12 at 22:24
  • 1
    what's wrong with plain HTML for 99% of your HTML? your PHP version of it has about 80 more characters in it, creates more overhead of parsing, and is really, but really, UGLY. (and hard to read) – Dvir Jun 23 '12 at 22:27
  • one problem with your idea is what happens if an HTML element name is the same as a PHP build-in function name? the only example I can think of right now is `header`, but you need to be careful of namespace clashes like this when you're writing a general-purpose library. – Spudley Jun 24 '12 at 14:27
  • @somethingkindawierd Thanks! I've updated the library. Passing the attributes array is now optional, so many instances are now more concise. – dharmatech Jun 28 '12 at 22:20
  • Why bother if all your code does is return the same string? An OO approach with instances that allow you to add and remove attributes and access child nodes would be much better. At the very least you could use functions that generate render arrays or stdClass instances that would still allow you to manipulate DOM nodes and then have a generic render() function for outputting them. – tvanc Jan 06 '14 at 21:21

5 Answers5

3

You can use Codeigniter html tables and form helper but that's about it. The rest is up to you.

Samson
  • 2,801
  • 7
  • 37
  • 55
3

See non OO-Answer of PHP HTML Creation Library:

example.php:

<?php
require ("html.php");
  // crete a header
  $head=head(title("This is an example"));
  // and a body
  $body=body(h(1,"This is a header 1").pre("With some preformatted text").hr());
  // wrap it in html
  echo html($head.$body);
?>

html.php:

<?php
/**
 * HTML Abstraction
 */

   // anchor   
   function a($href,$la,$indent=-1) {
     return attrtag("a",attr("href",$href),$la,$indent,$indent);
   }

   // break
   function br($indent=-1) {
     return tag("br","",$indent,$indent);
   }

   // header
   function h($h,$lh,$indent=-1) {
     if ($indent<0) 
       $indent=$h+1;
     return tag("h".$h,$lh,$indent,-1);
   }

   // horizontal ruler
   function hr($indent=-1) {
     return tag("hr","",$indent,$indent);
   }

   // image
   function img($src,$alt,$width,$height,$indent=-1) {
     return attrtag("img",attr("src",$src).attr("alt",$alt).attr("width",$width).attr("height",$height),"",$indent,$indent);
   }

   // pre formatted content
   function pre($content,$indent=-1) {
     return tag("pre",$content,$indent);
   }

   // table 
   function td($ltd,$indent=5) {
     return tag("td",$ltd,$indent,$indent);
   }

   // table header
   function th($lth,$indent=5) {
     return tag("th",$lth,$indent,$indent);
   }

   // table row
   function tr($ltr,$indent=4) {
     return tag("tr",$ltr,$indent,$indent);
   }


   // table
   function table($lt,$indent=3) {
     return tag("table",$lt,$indent,$indent);
   }

   // title
   function title($title,$indent=2) {
     return tag("title",$title,$indent,-1);
   }

   // head
   function head($head,$indent=1) {
     return tag("head",$head,$indent,$indent);
   }

   // body
   function body($body,$indent=1) {
     return tag("body",$body,$indent,$indent);
   }

   // html
   function html($html) {
     return tag("html",$html,-1,0);
   } 
   // indentation by the given count
   function indentation($count) {
     return str_repeat("  ",$count);
   }

   // adds a newline    
   function line($line) {
     return $line."\n";
   }

   // an attribute with a given value
   // or empty if value is not set
   function attr($attr,$value) {
     if (empty($value))
       return "";
     else
       return " ".$attr."='".$value."'";
   }

   // attributed tag, possibly indented
   function attrtag($tag,$attr,$ltagcontent,$openindent=-1,$closeindent=-1) {
    $html="<".$tag.$attr;
    if ($openindent>=0)
      $html="\n".indentation($openindent).$html;
    if (empty($ltagcontent)) {
      $html.="/>";
        if ($closeindent>=0)
          $html.="\n".indentation($closeindent);
    } else {
        $html.=">".$ltagcontent;
        if ($closeindent>=0) {
          $html.="\n".indentation($closeindent);
        }
        $html.="</".$tag.">";
    }
    return $html;
   }

   // tag with possible indentation
   function tag($tag,$ltagcontent,$openindent=-1,$closeindent=-1) {
      return attrtag($tag,"",$ltagcontent,$openindent,$closeindent);
   }

    // indent the given lines
   function indent($html,$indent) {
     $result="";
     $lines=explode("\n",$html);
     foreach($lines as $line) {
       $result.=indentation($indent).$line."\n"; 
     }
     return $result;
   }

?>
Community
  • 1
  • 1
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
2

codeigniter does this kind of stuff. i’d suggest you look into that.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
2

While not an exact match, the Yii framework uses the CHtml class to assist in generating html.

Jonathan Beebe
  • 5,241
  • 3
  • 36
  • 42
2

I didn't find a library so I've continued to update my own HTML tab library.

dharmatech
  • 8,979
  • 8
  • 42
  • 88
  • 1
    You might want to consider having it automatically HTML-encode your attribute values and tag contents. You could have an optional argument to skip the encoding. And you'd want to automatically skip it when you're including other tags inside a tag, or when you're using a – JW. Jun 28 '12 at 22:38
  • it would be cool to see a port of mithril.js to php. It would be a truly dynamic way of creating html. – Jon49 Oct 18 '14 at 17:41