Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
TemplateGenerator.php
Go to the documentation of this file.
1<?php
2
4
6
17abstract class TemplateGenerator {
18
19 protected string $openVarTag;
20 protected string $closeVarTag;
21 protected string $openExpressionTag;
22 protected string $closeExpressionTag;
23 protected string $safeFilter;
24
25 abstract public function openBlock(string $name): string;
26
27 abstract public function closeBlock(): string;
28
29 abstract public function asArray(array $array): string;
30
31 abstract public function insertVariable(string $name, bool $safe = false): string;
32
33 abstract public function includeFile(string $filename, bool $asVariable = false): string;
34
35 abstract public function extendsTemplate(string $templateName, bool $asVariable = false): string;
36
37 abstract public function foreach(string $arrayName, string $value, ?string $key = null): string;
38
39 abstract public function endForeach(): string;
40
41 abstract public function condition(string $condition): string;
42
43 abstract public function endCondition(): string;
44
45 abstract public function getNonce(): string;
46
47 abstract public function getNonceArray(): string;
48
49 abstract public function getSelf(): string;
50
51 abstract public function asVariable(string $var): string;
52
56 public function getOpenVarTag(): string {
57 return $this->openVarTag;
58 }
59
63 public function getCloseVarTag(): string {
64 return $this->closeVarTag;
65 }
66
70 public function getOpenExpressionTag(): string {
71 return $this->openExpressionTag;
72 }
73
77 public function getCloseExpressionTag(): string {
78 return $this->closeExpressionTag;
79 }
80
81 private function parseValue($v) {
82 if (\is_numeric($v) && gettype($v) !== 'string') {
83 $result = $v;
84 } elseif ($v !== '' && UString::isBooleanStr($v)) {
85 $result = UString::getBooleanStr($v);
86 } elseif (\is_array($v)) {
87 $result = $this->asArray($v);
88 } elseif (\is_string($v) && UString::isExpression($v)) {
89 $result = \trim($v);
90 } else {
91 $result = "\"" . \str_replace(['$', '"'], ['\$', '\"'], $v) . "\"";
92 }
93 return $result;
94 }
95
96 protected function implodeParameters(array $parameters): string {
97 $result = [];
98 foreach ($parameters as $param) {
99 $result[] = $this->parseValue($param);
100 }
101 return \implode(',', $result);
102 }
103
104 protected function insertAssets(string $funcName, string $url) {
105 return $this->openExpressionTag . "$funcName('" . $url . "'," . $this->getNonceArray() . ")" . $this->closeExpressionTag;
106 }
107
108 public function insertJS(string $url): string {
109 return $this->insertAssets('js', $url);
110 }
111
112 public function insertCSS(string $url): string {
113 return $this->insertAssets('css', $url);
114 }
115
116 public function callFunction(string $funcName, ...$parameters): string {
117 return $this->openExpressionTag . "$funcName(" . $this->implodeParameters($parameters) . ')' . $this->closeExpressionTag;
118 }
119
120}
String utilities.
Definition UString.php:15
Ubiquity abstract TemplateGenerator class.
insertVariable(string $name, bool $safe=false)
extendsTemplate(string $templateName, bool $asVariable=false)
callFunction(string $funcName,... $parameters)
includeFile(string $filename, bool $asVariable=false)
insertAssets(string $funcName, string $url)