Skip to content

Instantly share code, notes, and snippets.

@dharmatech
Created June 23, 2012 22:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dharmatech/2980249 to your computer and use it in GitHub Desktop.
Save dharmatech/2980249 to your computer and use it in GitHub Desktop.

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'))))))));
?>
<?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()); }
?>
@madfriend
Copy link

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

@4d47
Copy link

4d47 commented Feb 16, 2013

Hey I wrote something similar to this https://github.com/4d47/php-tag-helper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment