Here is a simplified version of how I make templates with my current project, if used:
class Template { var $pagename = 'index'; function __construct() { $this->pagename = basename($_SERVER['SCRIPT_NAME'], '.php'); register_shutdown_function(array($this, 'do_output')); } function do_output() { $this->header(); $this->display($this->pagename); $this->footer(); } function __call($template, array $params) { call_user_func(array($this, 'display'), $template, params); } function display($template, array $params = null) { include "templates/$template.php"; } }
The idea is that you can write "include" Template.inc ", a new template;" and it organizes do_output () to automatically run at the end of the script. There are a few things left, like the method used to pass variables to the template.
You mentioned that you are not using PHP, and there are several PHP-isms there: register_shutdown_function () ensures that the templates will be called before the object destructors, but after the main script and calls in $ this → header () / footer () - calls to the magic functions that simply display ("header") and display ("footer"), they are intended to be overridden.
Of course, there is nothing wrong with using a switch, as in the example you posted, but you don't need headers and footers inside each case statement. Something like this will do the same:
require "templates/header.php"; require "templates/navigation.php"; switch ($page) { case "home": require "views/home.php"; break; case "search": require "views/search.php"; break; } require "templates/footer.php";
... or you can replace the switch () with something based on the file name, as I used above, if it works to customize your pages. The switch is the safest way if you plan to do this through the URL parameters.
flussence
source share