1
1
<?php
2
2
3
-
4
3
namespace Markaby ;
5
4
6
5
class Builder{
7
6
var $ lines = array ();
8
7
var $ head = array ();
9
8
10
- function append_tag ($ tag , $ content , $ attr =array () ){
11
- $ html_attr = '' ;
12
-
13
- $ content = TagBuilder::ensure_call ($ content );
9
+ function append_tag ($ tag , $ content , array $ attr =array () ){
10
+ $ this ->lines [] = TagBuilder::tag ($ tag , $ content , $ attr );
11
+ }
14
12
15
- $ this ->lines []="< {$ tag }{$ html_attr }> $ content</ {$ tag }> " ;
13
+ public function text ($ text ) {
14
+ $ this ->lines [] = $ text ;
16
15
}
17
16
18
- public function __call ($ name , $ arguments )
19
- {
20
- $ this ->lines []= call_user_func_array ("\Markaby\TagBuilder::tag " , array_merge (array ($ name ),$ arguments ) );
17
+ public function __call ($ name , $ arguments ) {
18
+ $ this ->lines [] = call_user_func_array ("\Markaby\TagBuilder::tag " , array_merge (array ($ name ),$ arguments ) );
21
19
}
22
20
23
- public function head ($ content ){
24
- $ this ->head []= '<!-- todo head --> ' ;
21
+ public function head ($ content ) {
22
+ $ this ->head [] = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ' ;
23
+
24
+ if ( is_array ($ content )) {
25
+ foreach ($ content as $ k =>$ v ){
26
+ $ this ->head [] = TagBuilder::tag ($ k ,$ v );
27
+ }
28
+ } else {
29
+ $ this ->head [] = TagBuilder::ensure_call ($ content );
30
+ }
31
+
25
32
return $ this ;
26
33
}
27
34
28
- public function serialize_head (){
35
+ public function serialize_head () {
29
36
$ content = implode ("\n" , array_filter ($ this ->head , function ($ s ){ return !empty ($ s ); }) );
30
37
return "<head> $ content</head> \n" ;
31
38
}
32
39
33
- public function html5 ($ content ){
40
+ public function html5 ($ content ) {
34
41
$ content = TagBuilder::ensure_call ($ content );
35
42
36
43
$ head = $ this ->serialize_head ();
@@ -43,35 +50,47 @@ public function html5($content){
43
50
return $ this ;
44
51
}
45
52
53
+ public function xhtml_strict () {
54
+ }
55
+ public function xhtml_transitional () {
56
+ }
46
57
47
58
public function __toString (){
48
59
return implode ("\n" , array_filter ($ this ->lines , function ($ s ){ return !empty ($ s ); }) );
49
60
}
50
61
}
51
62
52
63
class TagBuilder {
53
-
54
-
55
- public static function ensure_call ($ content ){
56
- if (is_callable ($ content )){
64
+ /**
65
+ * accept as input strings and functions
66
+ */
67
+ public static function ensure_call ($ content ) {
68
+ if ( is_callable ($ content ) ){
57
69
$ content = $ content ();
70
+ } elseif ( is_array ($ content ) ) {
71
+ return $ content ;
58
72
}
59
73
return $ content ;
60
74
}
61
-
62
- public static function tag ($ tag , $ content , $ attr =array () ){
75
+ /**
76
+ * generates html tag
77
+ */
78
+ public static function tag ($ tag , $ content =null , array $ attr =array () ) {
63
79
$ html_attr = '' ;
64
80
$ content = self ::ensure_call ($ content );
65
81
66
- $ s = "< {$ tag }{$ html_attr }> \n$ content \n</ {$ tag }> \n" ;
82
+ // self-closing tag
83
+ if ( empty ($ content )) {
84
+ $ s = "< {$ tag }{$ html_attr } /> \n" ;
85
+ } else {
86
+ $ s = "< {$ tag }{$ html_attr }> \n$ content \n</ {$ tag }> \n" ;
87
+ }
67
88
return $ s ;
68
89
}
69
90
70
- public static function __callStatic ($ name , $ arguments )
71
- {
91
+ public static function __callStatic ($ name , $ arguments ) {
72
92
return call_user_func_array ("self::tag " , array_merge (array ($ name ),$ arguments ) );
73
93
}
74
-
75
94
}
76
95
77
96
0 commit comments