Compare commits
7 Commits
release-v1.0.5
...
v1.0.7
| Author | SHA1 | Date | |
|---|---|---|---|
| 16270fedf8 | |||
| 49bba32a34 | |||
| ce684e0ad1 | |||
| 9b3bc83d0d | |||
| 5dbca21828 | |||
| 78163a800e | |||
| bf7d1ec84f |
+35
-25
@@ -28,13 +28,6 @@ class TwigRender {
|
||||
*/
|
||||
private $templatesPath;
|
||||
|
||||
/**
|
||||
* Template variable name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $tname;
|
||||
|
||||
/**
|
||||
* Template data.
|
||||
*
|
||||
@@ -42,17 +35,39 @@ class TwigRender {
|
||||
*/
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* Template object.
|
||||
*
|
||||
* @var \Twig\Environment
|
||||
*/
|
||||
private $twig;
|
||||
|
||||
/**
|
||||
* Loader object.
|
||||
*
|
||||
* @var \Twig\Loader\FilesystemLoader
|
||||
*/
|
||||
private $loader;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct(string $template, string $templates_path, array $data = [], string $tname = 'data') {
|
||||
public function __construct(string $template, string $templates_path, array $data = []) {
|
||||
$this->template = $template;
|
||||
$this->templatesPath = $templates_path;
|
||||
$this->data = $data;
|
||||
if (!isset($data['host'])) {
|
||||
$this->data['host'] = $_SERVER['HTTP_HOST'];
|
||||
}
|
||||
$this->tname = $tname;
|
||||
$this->loader = new \Twig\Loader\FilesystemLoader([]);
|
||||
$this->twig = new Environment($this->loader, ['debug' => TRUE]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add global variable(s) to template.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function addGlobals($name, $value) : self {
|
||||
$this->twig->addGlobal($name, $value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,16 +75,14 @@ class TwigRender {
|
||||
*/
|
||||
public function render() : string {
|
||||
|
||||
$this->data['host'] = $_SERVER['HTTP_HOST'];
|
||||
$html = "";
|
||||
|
||||
try {
|
||||
$loader = new FilesystemLoader([]);
|
||||
$loader = $this->addPaths($loader);
|
||||
$twig = new Environment($loader, ['debug' => TRUE]);
|
||||
$twig->addExtension(new DebugExtension());
|
||||
$twig->addExtension(new MFDESTwigExtension());
|
||||
$twig->addExtension(new MDTwigExtension());
|
||||
$html = $twig->render($this->template, [$this->tname => $this->data]);
|
||||
$this->twig->addExtension(new DebugExtension());
|
||||
$this->twig->addExtension(new MFDESTwigExtension());
|
||||
$this->addPaths();
|
||||
$html = $this->twig->render($this->template, ['data' => $this->data]);
|
||||
}
|
||||
catch (LoaderError $e) {
|
||||
$html .= "<br/><pre>" . print_r($e->getMessage());
|
||||
@@ -90,11 +103,10 @@ class TwigRender {
|
||||
/**
|
||||
* Recursively add all directories under the templates base path.
|
||||
*/
|
||||
private function addPaths(FilesystemLoader $loader) : FilesystemLoader {
|
||||
private function addPaths() {
|
||||
|
||||
// Add base path.
|
||||
$loader->addPath($this->templatesPath);
|
||||
|
||||
$this->loader->addPath($this->templatesPath);
|
||||
// Create a recursive iterator to find all subdirectories.
|
||||
$iterator = new \RecursiveIteratorIterator(
|
||||
new \RecursiveDirectoryIterator($this->templatesPath, \FilesystemIterator::SKIP_DOTS),
|
||||
@@ -104,11 +116,9 @@ class TwigRender {
|
||||
// Add each sub-dir under the base path.
|
||||
foreach ($iterator as $info) {
|
||||
if ($info->isDir()) {
|
||||
$loader->addPath($info->getPathname());
|
||||
$this->loader->addPath($info->getPathname());
|
||||
}
|
||||
}
|
||||
|
||||
return $loader;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user