• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CPS-IT / handlebars / 19730280759

27 Nov 2025 08:43AM UTC coverage: 91.362% (-0.2%) from 91.549%
19730280759

push

github

web-flow
Merge pull request #498 from CPS-IT/feature/unflatten-variables

2 of 2 new or added lines in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

973 of 1065 relevant lines covered (91.36%)

5.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.45
/Classes/Frontend/ContentObject/HandlebarsTemplateContentObject.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "handlebars".
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17

18
namespace CPSIT\Typo3Handlebars\Frontend\ContentObject;
19

20
use CPSIT\Typo3Handlebars\Renderer;
21
use Symfony\Component\DependencyInjection;
22
use TYPO3\CMS\Core;
23
use TYPO3\CMS\Frontend;
24

25
/**
26
 * HandlebarsTemplateContentObject
27
 *
28
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
29
 * @license GPL-2.0-or-later
30
 */
31
#[DependencyInjection\Attribute\AutoconfigureTag('frontend.contentobject', ['identifier' => 'HANDLEBARSTEMPLATE'])]
32
final class HandlebarsTemplateContentObject extends Frontend\ContentObject\AbstractContentObject
33
{
34
    public function __construct(
15✔
35
        private readonly Frontend\ContentObject\ContentDataProcessor $contentDataProcessor,
36
        private readonly Renderer\Template\Path\ContentObjectPathProvider $pathProvider,
37
        private readonly Renderer\Renderer $renderer,
38
        private readonly Core\TypoScript\TypoScriptService $typoScriptService,
39
    ) {}
15✔
40

41
    /**
42
     * @param array<string, mixed> $conf
43
     */
44
    public function render($conf = []): string
15✔
45
    {
46
        /* @phpstan-ignore function.alreadyNarrowedType */
47
        if (!\is_array($conf)) {
15✔
48
            $conf = [];
×
49
        }
50

51
        // Create rendering context
52
        $context = $this->createContext($conf);
15✔
53

54
        // Resolve template paths
55
        /** @var array<string, mixed> $templatePaths */
56
        $templatePaths = $this->typoScriptService->convertTypoScriptArrayToPlainArray(
15✔
57
            array_intersect_key(
15✔
58
                $conf,
15✔
59
                [
15✔
60
                    'partialRootPath' => true,
15✔
61
                    'partialRootPaths.' => true,
15✔
62
                    'templateRootPath' => true,
15✔
63
                    'templateRootPaths.' => true,
15✔
64
                ],
15✔
65
            ),
15✔
66
        );
15✔
67

68
        // Populate template paths for availability in subsequent renderings
69
        $this->pathProvider->push($templatePaths);
15✔
70

71
        $context->assignMultiple($this->resolveVariables($conf));
15✔
72

73
        $this->renderPageAssetsIntoPageRenderer($conf);
15✔
74

75
        try {
76
            $content = $this->renderer->render($context);
15✔
77
        } finally {
78
            // Remove current content object rendering from path provider stack
79
            $this->pathProvider->pop();
15✔
80
        }
81

82
        if (isset($conf['stdWrap.'])) {
10✔
83
            return $this->cObj?->stdWrap($content, $conf['stdWrap.']) ?? $content;
1✔
84
        }
85

86
        return $content;
9✔
87
    }
88

89
    /**
90
     * @param array<string, mixed> $config
91
     */
92
    private function createContext(array $config): Renderer\RenderingContext
15✔
93
    {
94
        $format = $this->cObj?->stdWrapValue('format', $config, null);
15✔
95
        $context = new Renderer\RenderingContext();
15✔
96

97
        if (is_string($format)) {
15✔
98
            $context->setFormat($format);
2✔
99
        }
100

101
        if (isset($config['templateName']) || isset($config['templateName.'])) {
15✔
102
            return $context->setTemplatePath(
2✔
103
                (string)$this->cObj?->stdWrapValue('templateName', $config),
2✔
104
            );
2✔
105
        }
106

107
        if (isset($config['template']) || isset($config['template.'])) {
13✔
108
            return $context->setTemplateSource(
9✔
109
                (string)$this->cObj?->stdWrapValue('template', $config),
9✔
110
            );
9✔
111
        }
112

113
        if (isset($config['file']) || isset($config['file.'])) {
4✔
114
            return $context->setTemplatePath(
2✔
115
                (string)$this->cObj?->stdWrapValue('file', $config),
2✔
116
            );
2✔
117
        }
118

119
        return $context;
2✔
120
    }
121

122
    /**
123
     * @param array<string, mixed> $config
124
     * @return array<string|int, mixed>
125
     */
126
    private function resolveVariables(array $config): array
15✔
127
    {
128
        // Process content object variables and simple variables
129
        if ($this->cObj !== null && \is_array($config['variables.'] ?? null)) {
15✔
UNCOV
130
            $processor = Renderer\Variables\VariablesProcessor::for($this->cObj);
×
UNCOV
131
            $variables = $processor->process($config['variables.']);
×
132
        } else {
133
            $variables = [];
15✔
134
        }
135

136
        // Add current context variables
137
        $variables['data'] = $this->cObj->data ?? [];
15✔
138
        $variables['current'] = $this->cObj?->data[$this->cObj->currentValKey] ?? null;
15✔
139

140
        // Process variables with configured data processors
141
        if ($this->cObj !== null) {
15✔
142
            $variables = $this->contentDataProcessor->process($this->cObj, $config, $variables);
15✔
143
        }
144

145
        // Make settings available as variables
146
        if (isset($config['settings.'])) {
15✔
147
            $variables['settings'] = $this->typoScriptService->convertTypoScriptArrayToPlainArray($config['settings.']);
1✔
148
        }
149

150
        return $variables;
15✔
151
    }
152

153
    /**
154
     * @param array<string, mixed> $config
155
     */
156
    private function renderPageAssetsIntoPageRenderer(array $config): void
15✔
157
    {
158
        if (is_string($config['headerAssets'] ?? null) && is_array($config['headerAssets.'] ?? null)) {
15✔
159
            $headerAssets = $this->cObj?->cObjGetSingle($config['headerAssets'], $config['headerAssets.']) ?? '';
1✔
160
        } else {
161
            $headerAssets = '';
15✔
162
        }
163

164
        if (is_string($config['footerAssets'] ?? null) && is_array($config['footerAssets.'] ?? null)) {
15✔
165
            $footerAssets = $this->cObj?->cObjGetSingle($config['footerAssets'], $config['footerAssets.']) ?? '';
1✔
166
        } else {
167
            $footerAssets = '';
15✔
168
        }
169

170
        if (\trim($headerAssets) !== '') {
15✔
171
            $this->getPageRenderer()->addHeaderData($headerAssets);
1✔
172
        }
173

174
        if (\trim($footerAssets) !== '') {
15✔
175
            $this->getPageRenderer()->addFooterData($footerAssets);
1✔
176
        }
177
    }
178
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc