Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created June 6, 2012 20:34
Show Gist options
  • Select an option

  • Save nfreear/2884584 to your computer and use it in GitHub Desktop.

Select an option

Save nfreear/2884584 to your computer and use it in GitHub Desktop.
HTML5 templates and forms using PHP anonymous functions.
<?php
/*
PHP 5.3+ "A practical example of anonymous functions could be a.. template system."
Copyright N.D.Freear 7 June 2012.
..
lennymail at nospam dot gmail dot com 05-Oct-2011 10:40
http://php.net/manual/en/functions.anonymous.php#106046
*/
error_reporting(E_ALL); ini_set('display_errors', 1);
/* Create a generic HTML element.
*/
function html($elem = 'html', $attrs = array()) {
$attrs = _attributes($attrs);
#if ($id !== "") $id = " id=\"$id\"" ;
#$class = ($class !== "")? " class=\"$class\"":'';
$open='';
if ('html'==$elem) $open .= '<!doctype html>';
$open .= "<$elem$attrs";
if ('html'==$elem) $open .= '><meta charset=utf-8 /';
$close = "</$elem>";
if (preg_match('#^(link|img)$#', $elem)) $close = '';
return function ($inner = '', $attrs = array()) use ($open, $close) {
if (is_array($inner)) {
$attrs = $inner;
$inner = '';
}
$attrs = _attributes($attrs);
if (FALSE!==strpos($open, '<pre')) {
return "$open$attrs>" . htmlspecialchars($inner) . $close;
}
return "$open$attrs>$inner$close";
//return $open . htmlspecialchars($inner) . $close;
};
}
$elements = 'html,head,title,link,script,body,nav,h1,h2,p,div,pre,strong,em,ul,li';
foreach (explode(',', $elements) as $element) {
$$element = html($element);
}
/* CSS: <style> and <link rel=stylesheet />.
*/
function _css() {
return function($attrs) {
if (FALSE === strpos($attrs, '.css')) {
$open = '<style';
$close = '</style>';
$inner = $attrs;
$attrs = '';
} else {
$open = '<link';
$close = $inner = '';
$attrs = " rel='stylesheet' href='$attrs'";
}
return "$open$attrs>$inner$close";
};
}
$css = _css();
/* HTML form fields.
*/
function _input($type) {
return function($attrs = array()) use ($type) {
$attrs = _attributes($attrs);
return "<input type='$type'$attrs />";
};
}
$fields = array('email', 'url', 'text');
foreach ($fields as $name) {
$field = $name.'field';
$$field = _input($name);
}
function _attributes($attributes = array()) {
$attrs = '';
if (is_array($attributes)) {
foreach ($attributes as $at => $val) {
$attrs .= " $at='$val'";
}
}
elseif (is_string($attributes)) {
$attrs = ' '.$attributes;
}
return $attrs;
}
$layout = array('container','header','pmain','lsidebar','rsidebar','footer');
foreach ($layout as $element)
$$element = html("div", array('id' => $element));
$div = html("div", "test");
$bold = html('strong');
$italic = html('i');
$msg = $div($bold($italic("hello from the left sidebar")));
/*
echo $container(
$header(
"This is the header").$pmain(
$lsidebar(
$msg).$rsidebar(
"This is the right sidebar")).$footer(
));
*/
//try {
echo $html(
$title('*Testing')
. $css('xyz.css')
. $css('body{ font:1em sans-serif }')
. $body($h1('heading').$pmain('This is the body').$pre('<hello>')
. $emailfield()
)
);
/*
} catch ($e) {
echo 'Woops';
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment