/
Template.php
43 lines (32 loc) · 1.2 KB
/
Template.php
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
<?php
namespace LaravelLang\Lang\Concerns;
use Helldar\PrettyArray\Services\File;
use Helldar\Support\Facades\Helpers\Str;
trait Template
{
protected static array $templates = [];
protected function template(string $filename, array $values = [], bool $trim = false, bool $return_empty = false): string
{
$template = $this->getTemplate($filename, $trim);
$values = array_merge($this->extend ?? [], $values);
return $this->replace($template, $values, $return_empty);
}
protected function replace(string $template, array $values, bool $return_empty = false): string
{
return $return_empty ? '' : Str::replace($template, $values, '{{%s}}');
}
protected function getTemplate(string $filename, bool $trim = false): string
{
if (static::$templates[$filename] ?? false) {
return static::$templates[$filename];
}
$template = File::make()->loadRaw(
$this->templatePath($filename)
);
return static::$templates[$filename] = $trim ? trim($template) : $template;
}
protected function templatePath(string $filename): string
{
return $this->app->resourcePath($filename);
}
}