Created
September 5, 2019 16:00
-
-
Save BlackScorp/a22c10325baf554181d30f012f09373f to your computer and use it in GitHub Desktop.
Code for the Youtube Tutorial https://youtu.be/g-rUVNo0FWc
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 | |
function render(string $path, array $data) { | |
if (defined('TEMPLATE_DIR')) { | |
$path = TEMPLATE_DIR . DIRECTORY_SEPARATOR . $path; | |
} | |
if (!is_file($path)) { | |
trigger_error('Datei "' . $path . '" wurde nicht gefunden'); | |
return null; | |
} | |
_templateData($data); | |
extract($data, EXTR_SKIP); | |
ob_start(); | |
require_once $path; | |
$content = ob_get_clean(); | |
return $content; | |
} | |
function escape(string $data) { | |
return htmlspecialchars($data, ENT_QUOTES, 'UTF-8'); | |
} | |
function _templateData(array $data = null) { | |
static $value = []; | |
if ($data) { | |
return $value = $data; | |
} | |
return $value; | |
} | |
function layout($path = null) { | |
static $layoutPath = ''; | |
if ($path && !(bool) $layoutPath) { | |
return $layoutPath = $path; | |
} | |
$data = _templateData(); | |
echo render($layoutPath, $data); | |
return null; | |
} | |
function section($name) { | |
static $sections = []; | |
if (!isset($sections[$name])) { | |
return $sections[$name] = ob_start(); | |
} | |
$content = trim(ob_get_clean()); | |
$data = _templateData(); | |
$data[$name] = $content; | |
_templateData($data); | |
unset($sections[$name]); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment