Skip to content

Instantly share code, notes, and snippets.

@martindilling
Created August 25, 2016 11:08
Show Gist options
  • Save martindilling/690d0506bcf51c2794212c86ce0ad6e2 to your computer and use it in GitHub Desktop.
Save martindilling/690d0506bcf51c2794212c86ce0ad6e2 to your computer and use it in GitHub Desktop.
Very very simple template implementation
<?php
/**
* Load a template passing the given data into it.
*
* Note: Assumes templates are loaded from a 'templates' folder.
* Use dot-notation for defining the template name.
* Don't include the file extension in the template name.
* eg. 'pages.home' > 'templates/pages/home.php'
* 'partials.elements.contact-button' > 'templates/partials/elements/contact-button.php'
*
* @param string $name
* @param array $data
* @return string
* @throws Exception
*/
function load_template ($name, $data = [])
{
// Figure out the file we should load.
// Expands dots to directory seperators.
$templateFile = 'templates' . DIRECTORY_SEPARATOR . str_replace('.', DIRECTORY_SEPARATOR, $name) . '.php';
// Fail if the template doesn't exist
if (!is_file($templateFile)) {
throw new \Exception("Could not load template [{$name}] [{$templateFile}]");
}
// Start output buffering
ob_start();
// Extract the data array to variables to make them available in the template we include
extract($data);
// Include the template file
include $templateFile;
// Delete the buffer and return the buffer content
return ob_get_clean();
}
/**
* Helper to load a partial template.
* Can be used in templates.
*
* @param string $partial
* @param array $data
* @return string
*/
function partial ($partial, $data = [])
{
return load_template("partials.{$partial}", $data);
}
/**
* Load a page template in the given layout.
*
* Note: Assumes layout templates are loaded from a 'layouts' folder in the templates folder.
* Assumes page templates are loaded from a 'pages' folder in the templates folder.
* The layout must echo the $content variable,
* that also means you must not pass in a variable
* named 'content' in the data.
*
* @param string $page
* @param string $layout
* @param array $data
* @return string
*/
function page ($page, $layout, $data = [])
{
$data['content'] = load_template("pages.{$page}", $data);
return load_template("layouts.{$layout}", $data);
}
/**
* Print the home page.
*
* @param mysqli $db
* @return bool
*/
function home($db)
{
$sql = "select * from cms_aboutheader order by page_id ASC";
$result = $db->query($sql);
$rowCount = $result->num_rows;
if ($result === false) {
return false;
}
$slides = [];
while ($row = $result->fetch_object()) {
$slides[] = [
'image' => $row->page_image,
'title' => $row->page_slidertitle,
'text' => $row->page_sliderbuttom,
];
}
echo page('home', 'default', [
'pageTitle' => ' | Home',
'slides' => $slides,
]);
}
<!-- Default Layout Template -->
<!-- With the current implementation a layout template the page template will be inserted into the $content variable -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>My Website<?= $pageTitle ?? '' ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<?= $content ?>
</body>
</html>
<!-- Home Page Template -->
<main class='main-wrapper'>
<h1>Home Page</h1>
<p>
This is the home page.
</p>
<?= partial('carousel', ['slides' => $slides]) ?>
</main>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment