Skip to content

Commit

Permalink
add support for self-closing tags and automatically indented output
Browse files Browse the repository at this point in the history
  • Loading branch information
naomi committed Feb 28, 2011
1 parent a1eda9f commit f9c4938
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 28 deletions.
61 changes: 60 additions & 1 deletion class.html.php
Expand Up @@ -25,6 +25,13 @@ static public function capture(Closure $call){

class Html {

static public $self_closing_tags = array(
"base", "basefont", "br", "col", "frame", "hr", "input", "link", "meta", "param"
);

static private $indent_level = -1;
static private $indent_pattern = "\t";

# the magic
static public function __callStatic($tag, $args){
$params = array(null, array(), null);
Expand All @@ -39,7 +46,30 @@ static public function __callStatic($tag, $args){

# tag generator
static private function content_for($tag, $text=null, $html_attributes=array(), $callback=null){
echo "<{$tag}", self::attributes($html_attributes), ">", $callback instanceof Closure ? "\n".Utility::capture($callback) : $text, "</{$tag}>\n";

# indent
echo self::indent();

# self-closing tag
if(in_array($tag, self::$self_closing_tags)){
echo "<{$tag}", self::attributes($html_attributes), " />\n";
}

# standard tag
else {
echo "<{$tag}", self::attributes($html_attributes), ">";

# block
if($callback instanceof Closure) echo "\n", Utility::capture($callback), self::indent(false);

# single line
else echo $text;

echo "</{$tag}>\n";
}

# outdent
self::outdent();
}

# html attribute generator
Expand All @@ -58,6 +88,35 @@ static public function a($text, $href, $html_attributes=array()){
self::__callStatic("a", array($text, $html_attributes));
}

# comment tag
static public function comment($comment){
echo "\n", self::indent(), "<!-- {$comment} -->\n";
self::outdent();
}

# set indent level
static public function set_indent_level($level){
if(is_numeric($level)) self::$indent_level = $level-1;
}

# set indent pattern
static public function set_indent_pattern($pattern){
self::$indent_pattern = $pattern;
}

# increase indent level
static private function indent($increment=true){
if($increment) self::$indent_level++;
$tabs = "";
for($i=0; $i<self::$indent_level; $i++) $tabs .= self::$indent_pattern;
return $tabs;
}

# decrease indent level
static private function outdent(){
self::$indent_level--;
}

}

?>
28 changes: 22 additions & 6 deletions example.php
Expand Up @@ -2,13 +2,29 @@

require "class.html.php";

Html::div(array("id" => "wrapper"), function(){
Html::h1("Hello, World", array("class" => "title"));
Html::ul(array("class" => "links"), function(){
foreach(array(1,2,3) as $x)
Html::li(function() use($x) {
Html::a("Link {$x}", "#{$x}");
Html::set_indent_pattern(" ");

Html::html(function(){
Html::head(function(){
Html::meta(array("http-equiv"=>"Content-Type", "content"=>"text/html; charset=UTF-8"));
Html::link(array("rel"=>"stylesheet", "type"=>"text/css", "href"=>"global.css"));
});
Html::body(function(){
Html::div(array("id"=>"wrapper"), function(){
Html::h1("Hello, World", array("class"=>"title"));

Html::comment("navigation");
Html::ul(array("class"=>"links"), function(){
foreach(array(1,2,3) as $x)
Html::li(function() use($x) {
Html::a("Link {$x}", "#{$x}");
});
});

Html::comment("let's see some text");
Html::p("Lorem ipsum dolor sit amet, consectetur adipisicing elit...");

});
});
});

Expand Down
77 changes: 56 additions & 21 deletions readme.md
Expand Up @@ -12,31 +12,66 @@ PHP >= 5.3

require "class.html.php";

Html::div(array("id" => "wrapper"), function(){
Html::h1("Hello, World", array("class" => "title"));
Html::ul(array("class" => "links", function(){
foreach(array(1,2,3) as $x)
Html::li(function() use($x) {
Html::a("Link {$x}", "#{$x}");
Html::set_indent_pattern(" ");

Html::html(function(){
Html::head(function(){
Html::meta(array("http-equiv"=>"Content-Type", "content"=>"text/html; charset=UTF-8"));
Html::link(array("rel"=>"stylesheet", "type"=>"text/css", "href"=>"global.css"));
});
Html::body(function(){
Html::div(array("id"=>"wrapper"), function(){
Html::h1("Hello, World", array("class"=>"title"));

Html::comment("navigation");
Html::ul(array("class"=>"links"), function(){
foreach(array(1,2,3) as $x)
Html::li(function() use($x) {
Html::a("Link {$x}", "#{$x}");
});
});

Html::comment("let's see some text");
Html::p("Lorem ipsum dolor sit amet, consectetur adipisicing elit...");

});
});
});

?>

## Output

<div id="wrapper">
<h1 class="title">Hello, World</h1>
<ul class="links">
<li>
<a href="#1">Link 1</a>
</li>
<li>
<a href="#2">Link 2</a>
</li>
<li>
<a href="#3">Link 3</a>
</li>
</ul>
</div>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="global.css" />
</head>
<body>
<div id="wrapper">
<h1 class="title">Hello, World</h1>

<!-- navigation -->
<ul class="links">
<li>
<a href="#1">Link 1</a>
</li>
<li>
<a href="#2">Link 2</a>
</li>
<li>
<a href="#3">Link 3</a>
</li>
</ul>

<!-- let's see some text -->
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
</div>
</body>
</html>


## Try it and see!

$ cd htmlgen
$ php example.php

0 comments on commit f9c4938

Please sign in to comment.