2023-09-04 23:02:53 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Mcp;
|
|
|
|
|
|
|
|
use Mcp\Util\TemplateVarArray;
|
|
|
|
|
|
|
|
class TemplateBuilder
|
|
|
|
{
|
|
|
|
|
|
|
|
private string $basedir;
|
|
|
|
private string $name;
|
|
|
|
private ?string $parent = null;
|
|
|
|
private array $vars = [];
|
|
|
|
|
|
|
|
public function __construct(string $basedir, string $name)
|
|
|
|
{
|
|
|
|
$this->basedir = $basedir;
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
|
2023-09-15 17:19:45 +00:00
|
|
|
/**
|
|
|
|
* Sets another template to be the "parent" of this one.
|
|
|
|
*
|
|
|
|
* The template specified in this TemplateBuilder's constructor will be included into the parent.
|
|
|
|
*/
|
2023-09-04 23:02:53 +00:00
|
|
|
public function parent(string $parent): TemplateBuilder
|
|
|
|
{
|
|
|
|
$this->parent = $parent;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-09-15 17:19:45 +00:00
|
|
|
/**
|
|
|
|
* Sets multiple variables after escaping them.
|
|
|
|
*/
|
2023-09-04 23:02:53 +00:00
|
|
|
public function vars(array $vars): TemplateBuilder
|
|
|
|
{
|
|
|
|
foreach ($vars as $key => $val) {
|
|
|
|
$this->vars[$key] = htmlspecialchars(strval($val));
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-09-15 17:19:45 +00:00
|
|
|
/**
|
|
|
|
* Sets the specified variable for this template, after escaping it.
|
|
|
|
*/
|
2023-09-04 23:02:53 +00:00
|
|
|
public function var(string $key, string $val): TemplateBuilder
|
|
|
|
{
|
|
|
|
$this->vars[$key] = htmlspecialchars($val);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-09-15 17:19:45 +00:00
|
|
|
/**
|
|
|
|
* Sets the specified variable for this template WITHOUT escaping it.
|
|
|
|
*
|
|
|
|
* User input included this way has to be manually sanitized before.
|
|
|
|
*/
|
2023-09-04 23:02:53 +00:00
|
|
|
public function unsafeVar(string $key, string $val): TemplateBuilder
|
|
|
|
{
|
|
|
|
$this->vars[$key] = $val;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-09-15 17:19:45 +00:00
|
|
|
/**
|
|
|
|
* Displays the template(s) with the current set of variables.
|
|
|
|
*/
|
2023-09-04 23:02:53 +00:00
|
|
|
public function render(): void
|
|
|
|
{
|
|
|
|
$v = new TemplateVarArray($this->vars);
|
|
|
|
$basepath = $this->basedir.DIRECTORY_SEPARATOR;
|
|
|
|
if ($this->parent == null) {
|
|
|
|
require $basepath.$this->name;
|
|
|
|
} else {
|
|
|
|
$v['child-template'] = $basepath.$this->name;
|
|
|
|
require $basepath.$this->parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|