aboutsummaryrefslogtreecommitdiff
path: root/lib/flexi/PhpTemplate.php
blob: 4eb0da131141e8a3ddb587d885183ab39c49e9d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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);
    }
}