BlueGit

timfa 66wiki

Power template: optional Usage section

- templateRenderer: add conditional blocks [[?path]] ... [[/path]]; content is
  kept only if the value at path is non-empty (not missing/null, not a blank
  string, not an empty array). Templates without the syntax render unchanged.
- power.template: Usage section between Requirements and Description, wrapped
  in [[?usage]] so it is omitted when usage is absent, null or []
- power.template.css: style .powerUsage/.usageList like requirements

timfa Tim Falken · pushed by timfa

Authored 2026-09-28 · 7 weeks ago

Hash
e3e1de1ea5bc5422ee364f054cb36f351f8e7d50
Parent
688b0163
3 files changed +46 −1
web/templateRenderer.php +34 −0
@@ -76,6 +76,12 @@ final class TemplateRenderer
76 76 */
77 77 private function renderTemplateString(string $template, array $data): string
78 78 {
79 // 0) Conditional blocks: [[?path]] ... [[/path]]
80 // The block content is kept only if the value at `path` is non-empty
81 // (not missing/null, not an empty or whitespace-only string, not an empty array).
82 // Templates without this syntax are unaffected.
83 $template = $this->resolveConditionalBlocks($template, $data);
84
79 85 // 1) Child-template tags: [[moves:proficiency.move]]
80 86 $template = preg_replace_callback(
81 87 '/\[\[\s*([a-zA-Z0-9_.-]+)\s*:\s*([a-zA-Z0-9_.-]+)\s*\]\]/',
@@ -237,6 +243,34 @@ final class TemplateRenderer
237 243 return $template ?? '';
238 244 }
239 245
246 private function resolveConditionalBlocks(string $template, array $data): string
247 {
248 $pattern = '/\[\[\?\s*([a-zA-Z0-9_.-]+)\s*\]\](.*?)\[\[\/\s*\1\s*\]\]/s';
249
250 // Loop so that nested blocks (with different paths) are resolved too.
251 do {
252 $previous = $template;
253 $template = preg_replace_callback(
254 $pattern,
255 fn($m) => $this->isEmptyValue($this->getByPath($data, $m[1])) ? '' : $m[2],
256 $template
257 ) ?? $previous;
258 } while ($template !== $previous);
259
260 return $template;
261 }
262
263 private function isEmptyValue(mixed $value): bool
264 {
265 if ($value === null)
266 return true;
267 if (is_string($value))
268 return trim($value) === '';
269 if (is_array($value))
270 return count($value) === 0;
271 return false;
272 }
273
240 274 private function getByPath(mixed $data, string $path): mixed
241 275 {
242 276 $parts = explode('.', $path);
@@ -76,6 +76,12 @@ final class TemplateRenderer
76 */
76 */
77 private function renderTemplateString(string $template, array $data): string
77 private function renderTemplateString(string $template, array $data): string
78 {
78 {
79 // 0) Conditional blocks: [[?path]] ... [[/path]]
80 // The block content is kept only if the value at `path` is non-empty
81 // (not missing/null, not an empty or whitespace-only string, not an empty array).
82 // Templates without this syntax are unaffected.
83 $template = $this->resolveConditionalBlocks($template, $data);
84
79 // 1) Child-template tags: [[moves:proficiency.move]]
85 // 1) Child-template tags: [[moves:proficiency.move]]
80 $template = preg_replace_callback(
86 $template = preg_replace_callback(
81 '/\[\[\s*([a-zA-Z0-9_.-]+)\s*:\s*([a-zA-Z0-9_.-]+)\s*\]\]/',
87 '/\[\[\s*([a-zA-Z0-9_.-]+)\s*:\s*([a-zA-Z0-9_.-]+)\s*\]\]/',
@@ -237,6 +243,34 @@ final class TemplateRenderer
237 return $template ?? '';
243 return $template ?? '';
238 }
244 }
239
245
246 private function resolveConditionalBlocks(string $template, array $data): string
247 {
248 $pattern = '/\[\[\?\s*([a-zA-Z0-9_.-]+)\s*\]\](.*?)\[\[\/\s*\1\s*\]\]/s';
249
250 // Loop so that nested blocks (with different paths) are resolved too.
251 do {
252 $previous = $template;
253 $template = preg_replace_callback(
254 $pattern,
255 fn($m) => $this->isEmptyValue($this->getByPath($data, $m[1])) ? '' : $m[2],
256 $template
257 ) ?? $previous;
258 } while ($template !== $previous);
259
260 return $template;
261 }
262
263 private function isEmptyValue(mixed $value): bool
264 {
265 if ($value === null)
266 return true;
267 if (is_string($value))
268 return trim($value) === '';
269 if (is_array($value))
270 return count($value) === 0;
271 return false;
272 }
273
240 private function getByPath(mixed $data, string $path): mixed
274 private function getByPath(mixed $data, string $path): mixed
241 {
275 {
242 $parts = explode('.', $path);
276 $parts = explode('.', $path);
web/templates/power.template +9 −0
@@ -10,6 +10,15 @@
10 10 </ul>
11 11 </section>
12 12
13 [[?usage]]
14 <section class="powerUsage">
15 <h3>Usage</h3>
16 <ul class="usageList">
17 [[usage:listStringItem]]
18 </ul>
19 </section>
20 [[/usage]]
21
13 22 <section class="powerDescription">
14 23 <p>{{description}}</p>
15 24 </section>
@@ -10,6 +10,15 @@
10 </ul>
10 </ul>
11 </section>
11 </section>
12
12
13 [[?usage]]
14 <section class="powerUsage">
15 <h3>Usage</h3>
16 <ul class="usageList">
17 [[usage:listStringItem]]
18 </ul>
19 </section>
20 [[/usage]]
21
13 <section class="powerDescription">
22 <section class="powerDescription">
14 <p>{{description}}</p>
23 <p>{{description}}</p>
15 </section>
24 </section>
web/templates/power.template.css +3 −1
@@ -27,8 +27,10 @@
27 27
28 28 .powerRisks,
29 29 .powerRequirements,
30 .powerUsage,
30 31 .riskList,
31 .requirementList {
32 .requirementList,
33 .usageList {
32 34 margin: 0;
33 35 padding-left: 1.25rem;
34 36 }
@@ -27,8 +27,10 @@
27
27
28.powerRisks,
28.powerRisks,
29.powerRequirements,
29.powerRequirements,
30.powerUsage,
30.riskList,
31.riskList,
31.requirementList {
32.requirementList,
33.usageList {
32 margin: 0;
34 margin: 0;
33 padding-left: 1.25rem;
35 padding-left: 1.25rem;
34}
36}