2014-07-20 12:26:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Core;
|
|
|
|
|
2014-10-22 19:59:09 +02:00
|
|
|
use LogicException;
|
|
|
|
|
2014-07-20 12:26:15 +02:00
|
|
|
/**
|
|
|
|
* Template class
|
|
|
|
*
|
|
|
|
* @package core
|
|
|
|
* @author Frederic Guillot
|
|
|
|
*/
|
|
|
|
class Template
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Template path
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const PATH = 'app/Templates/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a template
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* $template->load('template_name', ['bla' => 'value']);
|
|
|
|
*
|
|
|
|
* @access public
|
2014-10-22 19:59:09 +02:00
|
|
|
* @params string $__template_name Template name
|
|
|
|
* @params array $__template_args Key/Value map of template variables
|
2014-07-20 12:26:15 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-10-22 19:59:09 +02:00
|
|
|
public function load($__template_name, array $__template_args = array())
|
2014-07-20 12:26:15 +02:00
|
|
|
{
|
2014-10-22 19:59:09 +02:00
|
|
|
$__template_file = self::PATH.$__template_name.'.php';
|
2014-07-20 12:26:15 +02:00
|
|
|
|
2014-10-22 19:59:09 +02:00
|
|
|
if (! file_exists($__template_file)) {
|
|
|
|
throw new LogicException('Unable to load the template: "'.$__template_name.'"');
|
2014-07-20 12:26:15 +02:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:59:09 +02:00
|
|
|
extract($__template_args);
|
2014-07-20 12:26:15 +02:00
|
|
|
|
|
|
|
ob_start();
|
2014-10-22 19:59:09 +02:00
|
|
|
include $__template_file;
|
2014-07-20 12:26:15 +02:00
|
|
|
return ob_get_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render a page layout
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $template_name Template name
|
|
|
|
* @param array $template_args Key/value map
|
|
|
|
* @param string $layout_name Layout name
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function layout($template_name, array $template_args = array(), $layout_name = 'layout')
|
|
|
|
{
|
|
|
|
return $this->load(
|
|
|
|
$layout_name,
|
|
|
|
$template_args + array('content_for_layout' => $this->load($template_name, $template_args))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|