diff options
| author | Philipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de> | 2024-09-24 10:53:31 +0200 |
|---|---|---|
| committer | Philipp Schüttlöffel <schuettloeffel@zqs.uni-hannover.de> | 2024-09-24 10:53:31 +0200 |
| commit | 4459dd7917f4d1c34f40bb68f0e991e9c3d53e4c (patch) | |
| tree | 5c07151ae61276d334e88f6309c30d439a85c12e /lib/flexi/PhpTemplate.php | |
| parent | da0022e5c1abbf9825ae76debaabdff7e8623bb4 (diff) | |
| parent | 97a188592c679890a25c37ab78463add76a52ff7 (diff) | |
Merge branch 'main' into issue-3911issue-3911
Diffstat (limited to 'lib/flexi/PhpTemplate.php')
| -rw-r--r-- | lib/flexi/PhpTemplate.php | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/lib/flexi/PhpTemplate.php b/lib/flexi/PhpTemplate.php new file mode 100644 index 0000000..4eb0da1 --- /dev/null +++ b/lib/flexi/PhpTemplate.php @@ -0,0 +1,106 @@ +<?php +/** + * A template engine that uses PHP to render templates. + * + * @copyright 2008 Marcus Lunzenauer <mlunzena@uos.de> + * @author Marcus Lunzenauer <mlunzena@uos.de> + * @license MIT + */ + +namespace Flexi; + +class PhpTemplate extends Template +{ + /** + * Parse, render and return the presentation. + * + * @return string A string representing the rendered presentation. + * @throws TemplateNotFoundException + */ + protected function _render(): string + { + extract($this->get_attributes()); + + # include template, parse it and get output + ob_start(); + require $this->template; + $content_for_layout = ob_get_contents(); + ob_end_clean(); + + # include layout, parse it and get output + if (isset($this->layout)) { + $defined = get_defined_vars(); + unset($defined['this']); + $content_for_layout = $this->layout->render($defined); + } + + return $content_for_layout; + } + + /** + * Parse, render and return the presentation of a partial template. + * + * @param Template|string $partial A partial name or template + * @param array $attributes An optional associative array of attributes + * and their associated values. + * @return string A string representing the rendered presentation. + * @throws TemplateNotFoundException + */ + public function render_partial(Template|string $partial, array $attributes = []): string + { + return $this->factory->render($partial, $attributes + $this->attributes); + } + + /** + * Renders a partial template with every member of a collection. This member + * can be accessed by a template variable with the same name as the name of + * the partial template. + * + * Example: + * + * # template entry.php contains: + * <li><?= $entry ?></li> + * + * + * $entries = ['lorem', 'ipsum']; + * $template->render_partial_collection('entry', $entries); + * + * # results in: + * <li>lorem</li> + * <li>ipsum</li> + * + * If you want to use specific content between the rendered partials, you + * may define a spacer partial that will be used for that. The spacer will + * be rendered with the given attributes. + * + * @param string $partial A name of a partial template. + * @param array $collection The collection to be rendered. + * @param Template|string|null $spacer Optional a name of a partial template + * used as spacer. + * @param array $attributes An optional associative array of attributes + * and their associated values. + * + * @return string A string representing the rendered presentation. + * @throws TemplateNotFoundException + */ + public function render_partial_collection( + string $partial, + array $collection, + Template|string|null $spacer = null, + array $attributes = [] + ): string { + $template = $this->factory->open($partial); + $template->set_attributes($this->attributes); + $template->set_attributes($attributes); + + $collected = []; + $iterator_name = pathinfo($partial, PATHINFO_FILENAME); + foreach ($collection as $element) { + $collected[] = $template->render([$iterator_name => $element]); + } + + $spacer = isset($spacer) ? $this->render_partial($spacer, $attributes) : ''; + + return implode($spacer, $collected); + } +} |
