public
Last active

  • Download Gist
example.md
Markdown

Some HTML:

<html>
<head>
  <title>Book-O-Rama - New Book Entry</title>
</head>

<body>
  <h1>Book-O-Rama - New Book Entry</h1>

  <form action="insert_book.php" method="post">
    <table border="0">
      <tr>
        <td>ISBN</td>
         <td><input type="text" name="isbn" maxlength="13" size="13"></td>
      </tr>
      <tr>
        <td>Author</td>
        <td> <input type="text" name="author" maxlength="30" size="30"></td>
      </tr>
      <tr>
        <td>Title</td>
        <td> <input type="text" name="title" maxlength="60" size="30"></td>
      </tr>
      <tr>
        <td>Price $</td>
        <td><input type="text" name="price" maxlength="7" size="7"></td>
      </tr>
      <tr>
        <td colspan="2"><input type="submit" value="Register"></td>
      </tr>
    </table>
  </form>
</body>
</html>

Using html-tag-generators:

<?php

require('html-tag-generators-wrapper.inc');

$site_title = 'Book-O-Rama - New Book Entry';

function text_input($name, $maxlength, $size)
{
    return input(array('type' => 'text', 'name' => $name, 'maxlength' => $maxlength, 'size' => $size));
}

echo html(
    head(title($site_title)),
    body(
        h1($site_title),
        form(array('action' => 'insert_book.php', 'method' => 'post'),
            table(array('border' => 0),
                tr(td('ISBN'),   td(text_input('isbn', 13, 13))),
                tr(td('Author'), td(text_input('author', 13, 13))),
                tr(td('Title'),  td(text_input('title', 13, 13))),
                tr(td('Price'),  td(text_input('price', 13, 13))),
                tr(td(array('colspan' => 2), input(array('type' => 'submit', 'value' => 'Register'))))))));
?>
html-tag-generators.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
<?php
 
function attr($key, $val) { return "$key=\"$val\""; }
 
function html_tag($tag, $attributes, $content)
{
$str = "<$tag ";
foreach ($attributes as $key => $value)
$str .= attr($key, $value) . ' ';
 
$str .= '>'; $str .= "\n";
 
foreach ($content as $elt) $str .= $elt;
 
$str .= "</$tag>"; $str .= "\n";
 
return $str;
}
 
function html_single_tag($tag, $attributes)
{
$str = "<$tag ";
foreach ($attributes as $key => $value)
$str .= attr($key, $value) . ' ';
 
$str .= '/>'; $str .= "\n";
 
return $str;
}
 
function html_tag_wrapper($tag, $args)
{
$attributes = is_array($args[0]) ? array_shift($args) : array();
 
$content = $args;
 
return html_tag($tag, $attributes, $content);
}
 
function html_single_tag_wrapper($tag, $args)
{
$attributes = is_array($args[0]) ? array_shift($args) : array();
 
$content = $args;
 
return html_single_tag($tag, $attributes, $content);
}
 
function html() { return html_tag_wrapper('html', func_get_args()); }
function head() { return html_tag_wrapper('head', func_get_args()); }
function title() { return html_tag_wrapper('title', func_get_args()); }
function body() { return html_tag_wrapper('body', func_get_args()); }
function h1() { return html_tag_wrapper('h1', func_get_args()); }
function h2() { return html_tag_wrapper('h2', func_get_args()); }
function h3() { return html_tag_wrapper('h3', func_get_args()); }
function h4() { return html_tag_wrapper('h4', func_get_args()); }
function h5() { return html_tag_wrapper('h5', func_get_args()); }
function h6() { return html_tag_wrapper('h6', func_get_args()); }
function a() { return html_tag_wrapper('a', func_get_args()); }
function pre() { return html_tag_wrapper('pre', func_get_args()); }
function span() { return html_tag_wrapper('span', func_get_args()); }
function strong() { return html_tag_wrapper('strong', func_get_args()); }
function table() { return html_tag_wrapper('table', func_get_args()); }
function td() { return html_tag_wrapper('td', func_get_args()); }
function tr() { return html_tag_wrapper('tr', func_get_args()); }
function p() { return html_tag_wrapper('p', func_get_args()); }
function form() { return html_tag_wrapper('form', func_get_args()); }
 
function img() { return html_single_tag_wrapper('img', func_get_args()); }
function br() { return html_single_tag_wrapper('br', func_get_args()); }
function input() { return html_single_tag_wrapper('input', func_get_args()); }
 
?>

Have you thought about putting these functions in a namespace like html?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.