Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (success)
83.33%
15 / 18
75.00% covered (success)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
File
83.33% covered (success)
83.33%
15 / 18
75.00% covered (success)
75.00%
3 / 4
9.37
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTemplate
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 render
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 renderTemplate
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
4.59
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\View\Template;
15
16/**
17 * View file template class
18 *
19 * @category   Pop
20 * @package    Pop\View
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    3.3.0
25 */
26class File extends AbstractTemplate
27{
28
29    /**
30     * Constructor
31     *
32     * Instantiate the view file template object
33     *
34     * @param  string $template
35     */
36    public function __construct($template)
37    {
38        $this->setTemplate($template);
39    }
40
41    /**
42     * Set view template
43     *
44     * @param  string $template
45     * @throws Exception
46     * @return File
47     */
48    public function setTemplate($template)
49    {
50        if (!file_exists($template)) {
51            throw new Exception("Error: The template file '" . $template . "' does not exist.");
52        }
53        $this->template = $template;
54
55        return $this;
56    }
57
58    /**
59     * Render the view and return the output
60     *
61     * @param  array $data
62     * @return string
63     */
64    public function render(array $data = null)
65    {
66        if (null !== $data) {
67            $this->data = $data;
68        }
69        $this->renderTemplate();
70        return $this->output;
71    }
72
73    /**
74     * Render view template file
75     *
76     * @throws \Exception
77     * @return void
78     */
79    protected function renderTemplate()
80    {
81        if (null !== $this->data) {
82            foreach ($this->data as $key => $value) {
83                ${$key} = $value;
84            }
85        }
86
87        try {
88            ob_start();
89            include $this->template;
90            $this->output = ob_get_clean();
91        } catch (\Exception $e) {
92            ob_clean();
93            throw $e;
94        }
95    }
96
97}