summaryrefslogtreecommitdiff
path: root/app/Core/Template.php
blob: f21e8a6d2cfa628887fa500b1e6451ca05ccf0a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php

namespace Core;

use LogicException;

/**
 * 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
     * @params string   $__template_name   Template name
     * @params array    $__template_args   Key/Value map of template variables
     * @return string
     */
    public function load($__template_name, array $__template_args = array())
    {
        $__template_file = self::PATH.$__template_name.'.php';

        if (! file_exists($__template_file)) {
            throw new LogicException('Unable to load the template: "'.$__template_name.'"');
        }

        extract($__template_args);

        ob_start();
        include $__template_file;
        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))
        );
    }
}