Created
August 25, 2016 11:08
-
-
Save martindilling/690d0506bcf51c2794212c86ce0ad6e2 to your computer and use it in GitHub Desktop.
Very very simple template implementation
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 | |
/** | |
* 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, | |
]); | |
} |
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
<!-- 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> |
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
<!-- Home Page Template --> | |
<main class='main-wrapper'> | |
<h1>Home Page</h1> | |
<p> | |
This is the home page. | |
</p> | |
<?= partial('carousel', ['slides' => $slides]) ?> | |
</main> |
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
<!-- Carousel Partial Template --> | |
<!-- I simplyfied the markup just to get a better overview --> | |
<section class="carousel"> | |
<!-- Indicators --> | |
<div class="carousel-indicators"> | |
<?php foreach ($slides as $slide): ?> | |
<button><?= $slide['title'] ?></button> | |
<?php endforeach; ?> | |
</div> | |
<hr> | |
<div class="carousel-images"> | |
<?php foreach ($slides as $slide): ?> | |
<div class="slide" style="border: 1px solid gray; padding: 8px; margin: 8px;"> | |
<img src="<?= $slide['image'] ?>" alt="<?= $slide['title'] ?>"> | |
<div class="carousel-caption"> | |
<h1><?= $slide['title'] ?></h1> | |
<p><?= $slide['text'] ?></p> | |
</div> | |
</div> | |
<?php endforeach; ?> | |
</div> | |
</section> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment