commit f3ce21708a13723f965a7d288c350087c075bc36
parent 47b9c3f926a21b0cb7f3b7c7aabdca67ddd67a27
Author: Jan Dankert <develop@jandankert.de>
Date: Wed, 29 Jan 2020 23:04:28 +0100
New: Support for simple element-creation.
Diffstat:
4 files changed, 76 insertions(+), 8 deletions(-)
diff --git a/modules/template-engine/Element.class.php b/modules/template-engine/Element.class.php
@@ -0,0 +1,47 @@
+<?php
+
+
+namespace modules\template_engine;
+
+
+class Element
+{
+ private $name;
+ private $attributes = [];
+ private $content = '';
+ private $selfClosing = true;
+
+ public function __construct( $name )
+ {
+ $this->name = $name;
+ }
+
+ public function content( $content )
+ {
+ $this->content = $content;
+ return $this;
+ }
+
+ public function getBegin() {
+
+ return '<'.$this->name.array_reduce( array_keys($this->attributes),function($carry,$key){return $carry.' '.$key.'="'.htmlspecialchars($this->attributes[$key]).'"';},'').(($this->selfClosing && !$this->content)?' /':'').'>'.$this->content;
+ }
+
+
+ public function getEnd() {
+ if ( $this->selfClosing && !$this->content)
+ return '';
+ else
+ return '</'.$this->name.'>';
+ }
+
+ public function attr($key,$value) {
+ $this->attributes[$key] = $value;
+ return $this;
+ }
+
+ public function selfClosing($selfClosing) {
+ $this->selfClosing = boolval($selfClosing);
+ return $this;
+ }
+}+
\ No newline at end of file
diff --git a/modules/template-engine/components/html/Component.class.php b/modules/template-engine/components/html/Component.class.php
@@ -3,6 +3,7 @@
namespace template_engine\components;
use cms\action\RequestParams;
+use modules\template_engine\Element;
abstract class Component
{
@@ -14,7 +15,17 @@ abstract class Component
*/
public $request;
- public function getDepth()
+ /**
+ * @var Element
+ */
+ private $element;
+
+ public function __construct()
+ {
+ $this->element = $this->createElement();
+ }
+
+ public function getDepth()
{
return $this->depth;
}
@@ -24,12 +35,20 @@ abstract class Component
$this->depth = $depth;
}
+ public function createElement() {
+ return null;
+ }
+
+
/**
* Gets the beginning of this component.
* @return string
*/
public function getBegin()
{
+ if ( $this->element )
+ return $this->element->getBegin();
+
ob_start();
$this->begin();
$src = ob_get_contents();
@@ -39,6 +58,9 @@ abstract class Component
public function getEnd()
{
+ if ( $this->element )
+ return $this->element->getEnd();
+
ob_start();
$this->end();
$src = ob_get_contents();
diff --git a/modules/template-engine/components/html/newline/Newline.class.php b/modules/template-engine/components/html/newline/Newline.class.php
@@ -2,13 +2,14 @@
namespace template_engine\components;
+use modules\template_engine\Element;
+
class NewlineComponent extends Component
{
- public function begin()
+ public function createElement()
{
- echo '<br/>';
+ return (new Element('br'));
}
}
-?>-
\ No newline at end of file
diff --git a/modules/template-engine/require.php b/modules/template-engine/require.php
@@ -2,5 +2,4 @@
include( dirname(__FILE__) . '/TemplateEngineInfo.class.php');
include( dirname(__FILE__) . '/engine/TemplateEngine.class.php');
-
-?>-
\ No newline at end of file
+include( dirname(__FILE__) . '/Element.class.php');