openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit a78336abeae25bb552be9dec57975b7212fc8d0e
parent 16afe8950382417dc82dcae4ccbcf09b59399968
Author: Jan Dankert <develop@jandankert.de>
Date:   Wed, 17 Nov 2021 23:22:19 +0100

Refactoring: Using a template context for templates instead of the HTTP-Request-Data

Diffstat:
Mmodules/cms/ui/UI.class.php | 2+-
Mmodules/template_engine/TemplateCompiler.php | 9+++++----
Mmodules/template_engine/components/html/Component.class.php | 5+++--
Mmodules/template_engine/components/html/component_form/FormComponent.class.php | 6+++---
Mmodules/template_engine/components/html/component_link/LinkComponent.class.php | 4++--
Amodules/template_engine/engine/TemplateContext.class.php | 28++++++++++++++++++++++++++++
Mmodules/template_engine/engine/TemplateEngine.class.php | 4++--
Mmodules/template_engine/engine/TemplateRunner.class.php | 5-----
8 files changed, 44 insertions(+), 19 deletions(-)

diff --git a/modules/cms/ui/UI.class.php b/modules/cms/ui/UI.class.php @@ -115,7 +115,7 @@ class UI header('X-OR-Template: '.$templateFile); $engine = new TemplateRunner(); - $engine->request = $request; + //$engine->request = $request; $engine->executeTemplate( $templateFile, $outputData ); } diff --git a/modules/template_engine/TemplateCompiler.php b/modules/template_engine/TemplateCompiler.php @@ -11,6 +11,7 @@ ini_set('display_startup_errors', 1); use cms\base\Startup; use template_engine\AppInfo; +use template_engine\engine\TemplateContext; use template_engine\engine\TemplateEngine; use util\FileUtils; @@ -48,10 +49,10 @@ foreach(FileUtils::readDir( $searchDir ) as $action ) // We are creating a fake request, because the template compiler needs to know // the action and subaction in which it will be executed. - $fakeRequest = new \cms\action\RequestParams(); - $fakeRequest->action = $action; - $fakeRequest->method = $method; - $engine->request = $fakeRequest; + $context = new TemplateContext(); + $context->action = $action; + $context->method = $method; + $engine->context = $context; echo "\t\tcompiling $templateFile\n\t\t to $outFile\n"; diff --git a/modules/template_engine/components/html/Component.class.php b/modules/template_engine/components/html/Component.class.php @@ -4,6 +4,7 @@ namespace template_engine\components\html; use cms\action\RequestParams; use template_engine\element\Element; +use template_engine\engine\TemplateContext; /** * Base class for all components. @@ -30,9 +31,9 @@ abstract class Component } /** - * @var RequestParams + * @var TemplateContext */ - public $request; + public $context; /** * @var Element diff --git a/modules/template_engine/components/html/component_form/FormComponent.class.php b/modules/template_engine/components/html/component_form/FormComponent.class.php @@ -56,13 +56,13 @@ class FormComponent extends Component public function createElement() { if ( ! $this->action ) - $this->action = $this->request->action; + $this->action = $this->context->action; if ( ! $this->subaction ) - $this->subaction = $this->request->method; + $this->subaction = $this->context->method; if ( ! $this->id ) - $this->id = $this->request->id; + $this->id = $this->context->id; $form = new CMSElement('form'); diff --git a/modules/template_engine/components/html/component_link/LinkComponent.class.php b/modules/template_engine/components/html/component_link/LinkComponent.class.php @@ -131,8 +131,8 @@ class LinkComponent extends Component // Aufpassen: Keine doppelten Hochkommas, keine Zeilenumbr├╝che. $data = array(); - $data['action' ] = (!empty($this->action ))?$this->action :$this->request->action; - $data['subaction'] = (!empty($this->subaction))?$this->subaction:$this->request->method; + $data['action' ] = (!empty($this->action ))?$this->action :$this->context->action; + $data['subaction'] = (!empty($this->subaction))?$this->subaction:$this->context->method; $data['id' ] = (!empty($this->id ))?$this->id :''; $data[RequestParams::PARAM_TOKEN] = '${_token}'; diff --git a/modules/template_engine/engine/TemplateContext.class.php b/modules/template_engine/engine/TemplateContext.class.php @@ -0,0 +1,27 @@ +<?php + + +namespace template_engine\engine; +use DomainException; +use DOMDocument; +use DOMElement; +use Exception; +use LogicException; +use template_engine\element\XMLFormatter; +use template_engine\element\PHPBlockElement; +use template_engine\components\html\Component; +use template_engine\components\html\NativeHtmlComponent; + +/** + * Template context + * + * @author Jan Dankert + */ +class TemplateContext +{ + public $action; + + public $subaction; + + public $id; +} +\ No newline at end of file diff --git a/modules/template_engine/engine/TemplateEngine.class.php b/modules/template_engine/engine/TemplateEngine.class.php @@ -34,7 +34,7 @@ class TemplateEngine public $renderType = 'html'; public $config = array(); - public $request; + public $context; private $srcFilename; @@ -156,7 +156,7 @@ class TemplateEngine /* @var $component Component */ $component = new $className(); $component->setDepth($depth+1); - $component->request = $this->request; + $component->context = $this->context; foreach ($attributes as $attribute) { diff --git a/modules/template_engine/engine/TemplateRunner.class.php b/modules/template_engine/engine/TemplateRunner.class.php @@ -20,11 +20,6 @@ use template_engine\components\html\NativeHtmlComponent; class TemplateRunner { /** - * @var \cms\action\RequestParams - */ - public $request; - - /** * Executes the required template and writes the output to standard-out.. * * @param $templateFile string filename of template