File modules/template_engine/element/Value.class.php

Last commit: Sun Nov 1 00:36:50 2020 +0100	Jan Dankert	Refactoring: Only using the configuration object.
1 <?php 2 3 namespace template_engine\element; 4 5 use template_engine\engine\TemplateEngine; 6 use util\text\variables\VariableResolver; 7 8 class Value 9 { 10 private $value; 11 private $expressions = []; 12 13 const CONTEXT_PHP = 0; 14 const CONTEXT_HTML = 1; 15 const CONTEXT_RAW = 2; 16 17 18 /** 19 * @param int $type a type from ValueExpression 20 * @param string $name 21 * @return string 22 */ 23 public static function createExpression($type, $name) 24 { 25 $expr = new ValueExpression($type,$name,0); 26 return $expr->getAsString(); 27 } 28 29 public function __construct($value) 30 { 31 if ( $value instanceof ValueExpression ) 32 { 33 $value = $value->getAsString(); 34 } 35 36 $this->value = $value; 37 } 38 39 40 public function render($context) 41 { 42 $res = new VariableResolver(); 43 $res->namespaceSeparator = ':'; 44 $res->defaultSeparator = '?'; 45 46 $res->addDefaultResolver( function($name) { 47 $parts = explode('.', $name); 48 49 return '\'.'.array_reduce($parts, function ($carry, $item) { 50 if (!$carry) 51 return '@$' . $item; 52 else 53 return $carry . '[\''.$item.'\']'; 54 }, '').'.\''; 55 }); 56 57 $res->addResolver( 'message',function($name) { 58 59 return '\'.@'.TemplateEngine::OUTPUT_ALIAS.'::lang(\'' . $name . '\').\''; 60 }); 61 62 $res->addResolver('config', function($name) { 63 $config_parts = explode('/', $name); 64 return '\'.'.TemplateEngine::OUTPUT_ALIAS.'::config([' . "'" . implode("'" . ',' . "'", $config_parts) . "'" . ']).\''; 65 }); 66 67 68 switch ($context) { 69 case Value::CONTEXT_PHP: 70 $escape = function ($expr) use ($context) { 71 return $expr; 72 }; 73 break; 74 75 case Value::CONTEXT_HTML: 76 $escape = function ($expr) use ($context) { 77 return TemplateEngine::OUTPUT_ALIAS.'::escapeHtml(' . $expr . ')'; 78 }; 79 break; 80 case Value::CONTEXT_RAW: 81 $escape = function ($expr) use ($context) { 82 return $expr; 83 }; 84 break; 85 } 86 87 if ( $context == self::CONTEXT_PHP ) { 88 return '\''.$res->resolveVariables( $this->value ).'\''; 89 90 } else { 91 92 $this->value = str_replace('\'','\\\'',$this->value); 93 return '<'.'?'.'php '.'echo '. $escape('\''.$res->resolveVariables( $this->value ).'\'').' ?'.'>'; 94 } 95 } 96 97 public function __xtoString() 98 { 99 return print_r($this,true); 100 } 101 }
Download modules/template_engine/element/Value.class.php
History Sun, 1 Nov 2020 00:36:50 +0100 Jan Dankert Refactoring: Only using the configuration object. Mon, 5 Oct 2020 23:32:06 +0200 Jan Dankert UI: Nicer buttons Sat, 3 Oct 2020 00:50:14 +0200 Jan Dankert Fix: Show the rights of node objects. Sun, 27 Sep 2020 04:09:05 +0200 Jan Dankert Refactoring: The tree functions should use normal templates as the other actions. Beware of the JS hell. Sat, 26 Sep 2020 23:15:19 +0200 Jan Dankert Fix: oops, a missing point. Sat, 26 Sep 2020 21:05:03 +0200 Jan Dankert Refactoring: An alias for the Output class, this results in a cleaner template. Sat, 26 Sep 2020 04:26:55 +0200 Jan Dankert Refactoring: read configuration values with a class. Sat, 26 Sep 2020 04:03:53 +0200 Jan Dankert Refactoring: read language keys with a class. Sat, 16 May 2020 01:08:40 +0200 Jan Dankert Refactoring: Switching the ValueExpressions in the templates to the new VariableResolver for supporting nested variables like ${message:prefix_${key}}. Thu, 27 Feb 2020 22:30:13 +0100 Jan Dankert New: ValueExpressions are able to contain other Value objects. Now data-driven messages are possible again :) Sat, 22 Feb 2020 22:23:01 +0100 Jan Dankert Refactoring: Enable Autoloading, Fix namespace structure.