Created
February 26, 2011 05:06
-
-
Save wondersloth/844968 to your computer and use it in GitHub Desktop.
Template rendering with anonymous functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Template | |
{ | |
function __construct($items, Closure $render) | |
{ | |
foreach ($items as $i) { | |
echo '<div style="border: 1px solid black; margin-bottom: 20px">'; | |
$render($i); | |
echo '</div>'; | |
} | |
} | |
public static function create($items, $render) | |
{ | |
$class = __CLASS__; | |
return new $class($items, $render); | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> | |
<head> | |
<title>untitled</title> | |
</head> | |
<body> | |
<h1>Welcome</h1> | |
<?php | |
$items = array( | |
'foo', | |
'bar', | |
'baz', | |
); | |
?> | |
<? | |
Template::create($items, function ($word) { | |
?> | |
<h2><?php echo $word ?></h2> | |
<p> | |
Lorem ipsum dolor sit amet. | |
</p> | |
<? | |
}); | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment