Last active
December 21, 2015 13:58
-
-
Save Zenger/6315922 to your computer and use it in GitHub Desktop.
The laziest possible template engine written in php
This file contains hidden or 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 require_once "template.php"; ?> | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Document</title> | |
</head> | |
<body> | |
<?php | |
ob_start(); | |
?> | |
<div id="container" class="{{class}}"> | |
{{@each articles}} | |
<article id="{{id}}"> | |
<h1>{{title}}</h1> | |
<p>{{content}}</p> | |
</article> | |
{{/@each}} | |
<footer id="footer">This is a test</footer> | |
<section id="{{offers_id}}"> | |
{{@each offers}} | |
<article class="offer"> | |
<a href="{{link}}"><h1>{{title}}</h1></a> | |
</article> | |
{{/@each}} | |
</section> | |
</div> | |
<?php | |
$html = ob_get_clean(); | |
$articles = array( | |
array( | |
'id' => 1, | |
'title' => "Here goes our first title", | |
'content' => 'This is our <strong>new html</strong>' | |
), | |
array( | |
'id' => 2, | |
'title' => 'Here i come ready or not', | |
'content' => 'This is <strong><em>simply the best</strong></em> and easiest template engine ever written' | |
) | |
); | |
$offers = array( | |
array( | |
'link' => 'http://someoffer.com/1', | |
'title' => "Offer number one", | |
), | |
array( | |
'link' => 'http://someoffer.com/2', | |
'title' => 'Offer number two' | |
) | |
); | |
$vars = array( | |
'articles' => $articles, | |
'class' => 'main-div', | |
'offers_id' => 'hello', | |
'offers' => $offers | |
); | |
echo Template::compile( $html , $vars ); | |
?> | |
</body> | |
</html> |
This file contains hidden or 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 { | |
static $escape = true; | |
static $template = ""; | |
static $vars = array(); | |
public static function sanitize($what) | |
{ | |
return filter_var($what, FILTER_SANITIZE_STRING); | |
} | |
public static function compile($template, $vars = array()) | |
{ | |
self::$template = $template; | |
self::$vars = $vars; | |
$template = self::parse_vars($template, $vars); | |
$template = self::parse_loops($template,$vars); | |
return $template; | |
} | |
public static function parse_vars($template, $vars) | |
{ | |
foreach($vars as $index => $var) | |
{ | |
if (!is_array($var)) // assuming loops | |
{ | |
if (self::$escape == true) | |
{ | |
$index = self::sanitize($index); | |
} | |
$template = str_replace("{{" .$index . "}}", $var, $template); | |
} | |
} | |
return $template; | |
} | |
public static function parse_loops($template, $vars) | |
{ | |
$loops = preg_match_all('/\{\{\@each\s(.*)\}\}(.*)\{\{\/\@each\}\}/Uis', $template, $matches, PREG_OFFSET_CAPTURE); // each loop | |
$html = ""; | |
if ($matches) | |
{ | |
$loops = count($matches[0]); | |
for($i=0;$i < (count($matches[1]));$i++) | |
{ | |
$index = $matches[1][$i][0] ; | |
$tt = $matches[2][$i][0]; | |
foreach( $vars[$index] as $var) | |
{ | |
$html .= self::parse_vars( $tt, $var ); | |
$template = str_replace($matches[0][$i][0], $html, $template); | |
} | |
} | |
} | |
return $template; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment