summaryrefslogtreecommitdiff
path: root/src/Renderer/HtmlEngine.php
blob: 0ccffa65c8c8992cb186ff13a5f5d76f078b0728 (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
<?php

namespace Engelsystem\Renderer;

class HtmlEngine extends Engine
{
    /**
     * Render a template
     *
     * @param string  $path
     * @param mixed[] $data
     * @return string
     */
    public function get(string $path, array $data = []): string
    {
        $data = array_replace_recursive($this->sharedData, $data);
        $template = file_get_contents($path);

        if (is_array($data)) {
            foreach ($data as $name => $content) {
                $template = str_replace('%' . $name . '%', $content, $template);
            }
        }

        return $template;
    }

    /**
     * @param string $path
     * @return bool
     */
    public function canRender(string $path): bool
    {
        return mb_strpos($path, '.htm') !== false && file_exists($path);
    }
}