Skip to content

Instantly share code, notes, and snippets.

@luckyshot
Last active May 8, 2017 19:54
Show Gist options
  • Save luckyshot/5395602 to your computer and use it in GitHub Desktop.
Save luckyshot/5395602 to your computer and use it in GitHub Desktop.
Simple Templating Engine
<?php
public function view($filename = 'home') {
// Default values for every View
$data = array(
"app_title" => $this->app_title,
"app_tagline" => $this->app_tagline,
"app_url" => $this->app_url,
"app_version" => $this->app_version,
"user_name" => $this->user_name,
);
// Read the file
if (file_exists('views/'.$filename.'.php')) {
$file = file_get_contents('views/'.$filename.'.php');
}else{
$file = file_get_contents('views/home.php');
}
// Custom stuff for each View
if ($filename == 'list') {
$list = $this->get_list();
$data = array_merge($data, array('list' => $list));
}
// Replace [custom values]
foreach ($data as $key => $value) {
$file = str_replace("[".$key."]", $value, $file);
}
return $file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment