Skip to content

Commit a5d1415

Browse files
committed
refined head generation
1 parent 61fbe0c commit a5d1415

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

Builder.php

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
<?php
22

3-
43
namespace Markaby;
54

65
class Builder{
76
var $lines = array();
87
var $head = array();
98

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+
}
1412

15-
$this->lines[]="<{$tag}{$html_attr}>$content</{$tag}>";
13+
public function text($text) {
14+
$this->lines[] = $text;
1615
}
1716

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 ) );
2119
}
2220

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+
2532
return $this;
2633
}
2734

28-
public function serialize_head(){
35+
public function serialize_head() {
2936
$content = implode("\n", array_filter($this->head, function ($s){ return !empty($s); }) );
3037
return "<head>$content</head>\n";
3138
}
3239

33-
public function html5($content){
40+
public function html5($content) {
3441
$content = TagBuilder::ensure_call($content);
3542

3643
$head = $this->serialize_head();
@@ -43,35 +50,47 @@ public function html5($content){
4350
return $this;
4451
}
4552

53+
public function xhtml_strict() {
54+
}
55+
public function xhtml_transitional() {
56+
}
4657

4758
public function __toString(){
4859
return implode("\n", array_filter($this->lines, function ($s){ return !empty($s); }) );
4960
}
5061
}
5162

5263
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) ){
5769
$content = $content();
70+
} elseif( is_array($content) ) {
71+
return $content;
5872
}
5973
return $content;
6074
}
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() ) {
6379
$html_attr = '';
6480
$content = self::ensure_call($content);
6581

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+
}
6788
return $s;
6889
}
6990

70-
public static function __callStatic($name, $arguments)
71-
{
91+
public static function __callStatic($name, $arguments) {
7292
return call_user_func_array("self::tag", array_merge(array($name),$arguments ) );
7393
}
74-
7594
}
7695

7796

0 commit comments

Comments
 (0)