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
- {
- /**
- * Whether to use debugging or not
- */
- const DEBUG = true;
- /**
- * @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;
- }
- /*
- *
- */
- public function phpize_var($var, $intend = false)
- {
- $php = '';
- switch ( gettype($var) )
- {
- case 'boolean':
- $php .= $var === true ? 'true' : 'false';
- break;
- case 'integer':
- case 'double':
- $php .= $var;
- break;
- case 'string':
- $php .= "'" .
- str_replace("'", "\\'", $var) .
- "'";
- break;
- case 'object':
- $php .= '(object)';
- case 'array':
- $php .= 'array(' . ($intend ? "\n" : '') ;
- foreach ($var as $key => $value)
- {
- $php .= $this->phpize_var($key, $intend) .
- ' => ' .
- $this->phpize_var($value, $intend) .
- ', ';
- if ($intend)
- $php .= "\n";
- }
- $php .= ')';
- break;
- case 'NULL':
- $php .= 'NULL';
- break;
- case 'resource':
- default:
- }
- if ($intend)
- $php = str_replace("\n", "\n ", $php);
- return $php;
- }
- /**
- * 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)
- {
- /* Secure that variables are valid for php */
- if (! preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/ims', $key) )
- continue;
- /* Add a new line to the template code */
- $template_code .=
- /* Write varaible name */
- '$' . $key . ' = ' . $this->phpize_var($value, true) . ';' . "\n";
- }
- $template_vars = $template_code;
- /* 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);
- /* Debug */
- if (self::DEBUG)
- {
- echo "<div style='display: none; width: 80%; position: absolute; top: 50px; left: 50%; margin-left: -45%; background: url(/images/bg.png); z-index: 1001; border-radius: 8px; border: 4px solid #E6572C; padding: 30px; color: #555; text-shadow: 0 1px 1px #fff ' id='debug'>\n";
- echo "<h3 style='font-style: normal; color: #E6572C; border-bottom: 2px solid #bbb; margin-bottom: 20px;'>Template vars:</h3><pre style='background: #ffffff; border-radius: 2px; border: 1px solid #aaa; padding: 5px; margin: 10px 0;'>" . ($template_vars) . "</pre>";
- echo "</div>";
- }
- echo "<script type='text/javascript'>$(document).ready( function(){ $(document).keypress( function(e){ if (e.keyCode == 100) $('#debug').slideToggle() } ) } );</script>\n";
- }
- /**
- * 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