Skip to content

Instantly share code, notes, and snippets.

@wondersloth
Created February 26, 2011 05:06
Show Gist options
  • Save wondersloth/844968 to your computer and use it in GitHub Desktop.
Save wondersloth/844968 to your computer and use it in GitHub Desktop.
Template rendering with anonymous functions.
<?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