Modified two aspects of `FuzeWorks\Layout`:

- It is now possible to load different engines by passing the bool $ignoreCurrentEngine to Layout::get(). This allows the developer to use different engines in succession without having to Layout::reset() the system.
- The PHPEngine now passes assigned variables with their original name. The use of $vars in templates is no longer required, but still possible.
This commit is contained in:
Abel Hoogeveen 2019-07-22 11:45:56 +02:00
parent e5268efcdf
commit f37d4a38d8
No known key found for this signature in database
GPG Key ID: 96C2234920BF4292
3 changed files with 25 additions and 14 deletions

View File

@ -14,12 +14,12 @@
],
"require": {
"php": ">=7.1.0",
"fuzeworks/core": "1.2.0-RC3"
"fuzeworks/core": "1.2.0-RC4"
},
"require-dev": {
"ext-json": "*",
"smarty/smarty": "~3.1",
"latte/latte": "~2.4",
"smarty/smarty": "3.1.*",
"latte/latte": "2.5.*",
"phpunit/phpunit": "^7",
"mikey179/vfsStream": "1.6.5"
},

View File

@ -149,12 +149,12 @@ class Layout
* Remember that doing so will result in a LayoutException when multiple compatible files are found.
*
* @param string $file File to load
* @param array $directories Directory to load it from
*
* @param array $directories Directory to load it from
* @param bool $ignoreCurrentEngine
* @return string The output of the template
* @throws LayoutException On error
*/
public function get(string $file, array $directories = []): string
public function get(string $file, array $directories = [], bool $ignoreCurrentEngine = false): string
{
Logger::newLevel("Loading template file '".$file."'");
@ -165,26 +165,33 @@ class Layout
$this->loadTemplateEngines();
// First retrieve the filePath
if (is_null($this->current_engine)) {
if (is_null($this->current_engine))
$this->setFileFromString($file, $directories, array_keys($this->file_extensions));
} else {
elseif ($ignoreCurrentEngine)
$this->setFileFromString($file, $directories, array_keys($this->file_extensions));
else
$this->setFileFromString($file, $directories, $this->current_engine->getFileExtensions());
}
// Then assign some basic variables for the template
// @TODO: Implement csrfTokenName and csrfHash from security under layoutLoadEvent
// Select an engine if one is not already selected
if (is_null($this->current_engine)) {
$this->current_engine = $this->getEngineFromExtension($this->getExtensionFromFile($this->file));
}
$this->current_engine->setDirectory($this->directory);
// Select an engine if one is not already selected
if (!$ignoreCurrentEngine)
$engine = $this->current_engine;
else
$engine = $this->getEngineFromExtension($this->getExtensionFromFile($this->file));
// Set directory of the engine
$engine->setDirectory($this->directory);
// And run an Event to see what other parts have to say about it
try {
/** @var LayoutLoadEvent $event */
$event = Events::fireEvent('layoutLoadEvent', $this->file, $this->directory, $this->current_engine, $this->assigned_variables);
$event = Events::fireEvent('layoutLoadEvent', $this->file, $this->directory, $engine, $this->assigned_variables);
// @codeCoverageIgnoreStart
} catch (EventException $e) {
throw new LayoutException("layoutEvent threw exception: '".$e->getMessage()."''", 1);
@ -196,14 +203,14 @@ class Layout
return 'cancelled';
// And re-fetch the data from the event
$this->current_engine = $event->engine;
$engine = $event->engine;
$this->assigned_variables = $event->assigned_variables;
Logger::stopLevel();
// And finally run it
if (file_exists($event->file)) {
return $this->current_engine->get($event->file, $this->assigned_variables);
return $engine->get($event->file, $this->assigned_variables);
}
throw new LayoutException('The requested file was not found', 1);

View File

@ -70,6 +70,10 @@ class PHPEngine implements TemplateEngine
$vars = $this->assigned_variables;
$directory = $this->directory;
// Preset assigned variables
foreach ($vars as $key => $val)
$$key = $val;
// Then run the file
if (!is_null($file)) {
ob_start();