Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 75e1f7979c | |||
| c8d150d752 | |||
| b7ee83e82a | |||
| d923c788f9 | |||
| 000ed4c093 | |||
| 2dc9c8df1a |
@@ -7,8 +7,6 @@ namespace Mdesouza\Utils;
|
||||
* A database class around the PDO object.
|
||||
*/
|
||||
|
||||
include_once 'db.inc.php';
|
||||
|
||||
/**
|
||||
* A mysql class around the PDO object.
|
||||
*/
|
||||
|
||||
+41
-5
@@ -24,10 +24,17 @@ class TwigRender {
|
||||
/**
|
||||
* Templates paths.
|
||||
*
|
||||
* @var array
|
||||
* @var string
|
||||
*/
|
||||
private $templatesPath;
|
||||
|
||||
/**
|
||||
* Template variable name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $tname;
|
||||
|
||||
/**
|
||||
* Template data.
|
||||
*
|
||||
@@ -38,10 +45,14 @@ class TwigRender {
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct(string $template, array $templates_path, array $data = []) {
|
||||
public function __construct(string $template, string $templates_path, array $data = [], string $tname = 'data') {
|
||||
$this->template = $template;
|
||||
$this->templatesPath = $templates_path;
|
||||
$this->data = $data;
|
||||
if (!isset($data['host'])) {
|
||||
$this->data['host'] = $_SERVER['HTTP_HOST'];
|
||||
}
|
||||
$this->tname = $tname;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,14 +60,15 @@ class TwigRender {
|
||||
*/
|
||||
public function render() : string {
|
||||
|
||||
$this->data['host'] = $_SERVER['HTTP_HOST'];
|
||||
$html = "";
|
||||
|
||||
try {
|
||||
$loader = new FilesystemLoader($this->templatesPath);
|
||||
$loader = new FilesystemLoader([]);
|
||||
$loader = $this->addPaths($loader);
|
||||
$twig = new Environment($loader, ['debug' => TRUE]);
|
||||
$twig->addExtension(new DebugExtension());
|
||||
$twig->addExtension(new MFDESTwigExtension());
|
||||
$html = $twig->render($this->template, ['data' => $this->data]);
|
||||
$html = $twig->render($this->template, [$this->tname => $this->data]);
|
||||
}
|
||||
catch (LoaderError $e) {
|
||||
$html .= "<br/><pre>" . print_r($e->getMessage());
|
||||
@@ -74,4 +86,28 @@ class TwigRender {
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively add all directories under the templates base path.
|
||||
*/
|
||||
private function addPaths(FilesystemLoader $loader) : FilesystemLoader {
|
||||
|
||||
// Add base path.
|
||||
$loader->addPath($this->templatesPath);
|
||||
|
||||
// Create a recursive iterator to find all subdirectories.
|
||||
$iterator = new \RecursiveIteratorIterator(
|
||||
new \RecursiveDirectoryIterator($this->templatesPath, \FilesystemIterator::SKIP_DOTS),
|
||||
\RecursiveIteratorIterator::SELF_FIRST
|
||||
);
|
||||
|
||||
// Add each sub-dir under the base path.
|
||||
foreach ($iterator as $info) {
|
||||
if ($info->isDir()) {
|
||||
$loader->addPath($info->getPathname());
|
||||
}
|
||||
}
|
||||
|
||||
return $loader;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user