Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- *
- * @author Sebastian Büttner <sebastian.buettner@powerserverplus.net>
- * @package void
- * @version 1.0.0
- * @copyright (c) 2013, powerserverplus.net
- */
- class FileNotFoundException extends Exception {}
- class Template
- {
- /**
- * @var String Path to tempalte dir
- */
- const template_dir = 'templates';
- /**
- * @var String (sub)template filename
- */
- protected $template = '';
- /**
- * @var Array data to be used within the template
- */
- protected $data;
- /**
- * Template constructor
- *
- * @param String $templ name of the template
- */
- public function __construct($templ, $layout = 'index')
- {
- $this->load($layout);
- $this->data = array();
- $this->assign('subtemplate', $templ);
- }
- /**
- * Save a passed variable for later use in template
- *
- * @param String $name name of the varaible used to identify
- * @param Mixed $value value to be assigned to the variable
- */
- public function assign($name, $value)
- {
- /* Separate name by dot */
- $path = explode('.', $name);
- /* Take the last element of the array, as this is our final identifier */
- $lastKey = array_pop($path);
- /* Create / fill array structure, start with the template`s data base array */
- $parent = &$this->data;
- /* Iterate through the given name structure... */
- foreach ($path as $key)
- {
- /* ...And create substructres where needed */
- if (!isset($parent[$key]))
- $parent[$key] = array();
- /* Move the parent `pointer` deeper within the structure */
- $parent = &$parent[$key];
- }
- /* Finally assign the value to the deepest element in structure */
- $parent[$lastKey] = $value;
- }
- /**
- * Prepare and display the tempalte
- *
- */
- public function display()
- {
- /* We start with no code */
- $template_code = '';
- /* Merge template data with session-flash data */
- $data = $this->data;
- if (isset($_SESSION['appends']) && is_array($_SESSION['appends']) && !empty($_SESSION['appends']))
- $data = array_merge($data, $_SESSION['appends']);
- $_SESSION['appends'] = array();
- /* And iterate then over all passed variables, making them executable code again */
- foreach ($data as $key => $value)
- {
- /* Add a new line to the template code */
- $template_code .=
- /* Write varaible name */
- '$' . $key . ' = ' .
- /* Let php unserialize the variables content (which has been serialized above) at runtime of evaluation */
- 'unserialize(\'' .
- /* Serialize the variables content, what is needed for arrays */
- serialize($value) .
- /* Close unserialize function and do a newline for more proper readable code */
- '\');' . "\n";
- }
- /* Put a php close tag, as we are continuing with out html-code-template */
- $template_code .= '?>';
- /* Append the template */
- $template_code .= file_get_contents($this->template);
- /* Execute the build string as PHP-Code */
- eval($template_code);
- }
- /**
- * Includes a given template by name
- *
- * @param String $templ the name of the tempalte file to be included
- */
- static function getInclude($templ)
- {
- /* Build the full path of the file to load */
- $filename = Template::template_dir . '/' . $templ . '.template.php';
- /* Check whether the requested file does exist, if not throw exception */
- if ( ! file_exists($filename) )
- throw new FileNotFoundException('Template file ' . $filename . ' not found.');
- /* return the filename to include */
- return $filename;
- }
- /**
- * Helper function, gives $default back when $var is unset
- *
- * @param Mixed $var
- * @param Mixed $default
- *
- */
- static function withDefault(&$var, $default)
- {
- if (!isset($var))
- return $default;
- return $var;
- }
- /**
- * Try to load template by name, can throw FileNotFoundException
- *
- * @param String $templ template name
- */
- private function load($templ)
- {
- /* Build the full path of the file to load */
- $filename = Template::template_dir . '/' . $templ . '.template.php';
- /* Check whether the requested file does exist, if not throw exception */
- if ( ! file_exists($filename) )
- throw new FileNotFoundException('Template file ' . $filename . ' not found.');
- /* Set the filename of the template */
- $this->template = $filename;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement