-
Notifications
You must be signed in to change notification settings - Fork 1
/
html.php
47 lines (32 loc) · 1.06 KB
/
html.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
<?php /* Chronicle.md - Copyright (C) 2013 Bruce Alderson */
/* A simple HTML static class - generates HTML from simple code (not a replacement for templates, interpolation, etc.) */
class html {
/* Output *any* HTML tag with attributes
USAGE:
1. A simple node
<?= html::p('Some text'); ?>
2. A simple node with classes
<?= html::h1('Some text', array('a', 'b', 'c')); ?>
*/
public static function __callStatic($n, $p) {
return self::_node($n, array_shift($p), array_shift($p));
}
/* ======================== Private helpers ======================== */
// Return an HTML node
private static function _node($n, $v, $c) {
if (empty($n) || is_numeric($n)) throw new Exception('Invalid node type', 500);
$a = self::_attr($c);
return "<$n{$a}>$v</$n>";
}
//
private static function _attr($c) {
if (!$c) return '';
elseif (is_string($c)) $c = array($c);
if (is_array($c)) {
$c = implode(' ', $c);
$c = " class='$c'";
} else
$c = ''; // unknown type (should support non-class items in the
return $c;
}
}