summaryrefslogtreecommitdiff
path: root/src/DTS/Template.php
blob: 89bd7f84f9fc3b9b070f9d56c003f60ae2bc5418 (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
<?php

declare(strict_types=1);

namespace DTS;

class Template
{
    private string $path;

    function __construct(string $path)
    {
        $this->path = $path;
    }

    public function render(string $template, array $data = []): string
    {
        $file = "{$this->path}$template.php";

        if (!is_file($file) || !is_readable($file)) {
            throw new \Exception("Unable to locate template $file");
        }

        extract($data);

        ob_start();

        require_once($file);

        return ob_get_clean();
    }
}