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

CPS-IT / handlebars / 22660183928

04 Mar 2026 07:53AM UTC coverage: 90.285% (-8.8%) from 99.094%
22660183928

push

github

eliashaeussler
Merge branch '1.x'

1277 of 1418 new or added lines in 66 files covered. (90.06%)

2 existing lines in 1 file now uncovered.

1329 of 1472 relevant lines covered (90.29%)

6.92 hits per line

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

96.7
/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\DataProcessing;
21
use CPSIT\Typo3Handlebars\Exception;
22
use CPSIT\Typo3Handlebars\Frontend\Assets;
23
use CPSIT\Typo3Handlebars\Renderer;
24
use Symfony\Component\DependencyInjection;
25
use TYPO3\CMS\Core;
26
use TYPO3\CMS\Frontend;
27

28
/**
29
 * HandlebarsTemplateContentObject
30
 *
31
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
32
 * @license GPL-2.0-or-later
33
 */
34
#[DependencyInjection\Attribute\AutoconfigureTag('frontend.contentobject', ['identifier' => 'HANDLEBARSTEMPLATE'])]
35
final class HandlebarsTemplateContentObject extends Frontend\ContentObject\AbstractContentObject
36
{
37
    use DataProcessing\DataSource\SupportsDataSourceAwareProcessing;
38

39
    public function __construct(
35✔
40
        private readonly Frontend\ContentObject\ContentDataProcessor $contentDataProcessor,
41
        private readonly Renderer\Template\Path\ContentObjectPathProvider $pathProvider,
42
        private readonly Renderer\Renderer $renderer,
43
        private readonly Core\TypoScript\TypoScriptService $typoScriptService,
44
        private readonly Assets\AssetHandler $assetHandler,
45
    ) {}
35✔
46

47
    /**
48
     * @param array<string, mixed> $conf
49
     */
50
    public function render($conf = []): string
35✔
51
    {
52
        /* @phpstan-ignore function.alreadyNarrowedType */
53
        if (!\is_array($conf)) {
35✔
NEW
54
            $conf = [];
×
55
        }
56

57
        // Create rendering context
58
        $context = $this->createContext($conf);
35✔
59

60
        // Resolve template paths
61
        /** @var array<string, mixed> $templatePaths */
62
        $templatePaths = $this->typoScriptService->convertTypoScriptArrayToPlainArray(
35✔
63
            array_intersect_key(
35✔
64
                $conf,
35✔
65
                [
35✔
66
                    'partialRootPath' => true,
35✔
67
                    'partialRootPaths.' => true,
35✔
68
                    'templateRootPath' => true,
35✔
69
                    'templateRootPaths.' => true,
35✔
70
                ],
35✔
71
            ),
35✔
72
        );
35✔
73

74
        // Populate template paths for availability in subsequent renderings
75
        $this->pathProvider->push($templatePaths);
35✔
76

77
        // Resolve and assign template variables
78
        $context->assignMultiple($this->resolveVariables($conf));
35✔
79

80
        // Process configured assets (using AssetCollector and PageRenderer)
81
        $this->processAssets($conf);
35✔
82

83
        try {
84
            $content = $this->renderer->render($context);
34✔
85
        } finally {
86
            // Remove current content object rendering from path provider stack
87
            $this->pathProvider->pop();
34✔
88
        }
89

90
        if (isset($conf['stdWrap.'])) {
29✔
91
            return $this->cObj?->stdWrap($content, $conf['stdWrap.']) ?? $content;
1✔
92
        }
93

94
        return $content;
28✔
95
    }
96

97
    /**
98
     * @param array<string, mixed> $config
99
     */
100
    private function createContext(array $config): Renderer\RenderingContext
35✔
101
    {
102
        $format = $this->cObj?->stdWrapValue('format', $config, null);
35✔
103
        $context = new Renderer\RenderingContext(request: $this->request);
35✔
104

105
        if (is_string($format)) {
35✔
106
            $context->setFormat($format);
2✔
107
        }
108

109
        if (isset($config['templateName']) || isset($config['templateName.'])) {
35✔
110
            return $context->setTemplatePath(
2✔
111
                (string)$this->cObj?->stdWrapValue('templateName', $config),
2✔
112
            );
2✔
113
        }
114

115
        if (isset($config['template']) || isset($config['template.'])) {
33✔
116
            return $context->setTemplateSource(
29✔
117
                (string)$this->cObj?->stdWrapValue('template', $config),
29✔
118
            );
29✔
119
        }
120

121
        if (isset($config['file']) || isset($config['file.'])) {
4✔
122
            return $context->setTemplatePath(
2✔
123
                (string)$this->cObj?->stdWrapValue('file', $config),
2✔
124
            );
2✔
125
        }
126

127
        return $context;
2✔
128
    }
129

130
    /**
131
     * @param array<string, mixed> $config
132
     * @return array<string|int, mixed>
133
     * @throws Exception\ConfiguredProcessorIsUnsupported
134
     * @throws Exception\ReservedVariableCannotBeUsed
135
     * @throws Frontend\ContentObject\Exception\ContentRenderingException
136
     */
137
    private function resolveVariables(array $config): array
35✔
138
    {
139
        $collection = new DataProcessing\DataSource\DataSourceCollection();
35✔
140
        $collection->set(DataProcessing\DataSource\DataSource::ContentObjectRenderer, $this->cObj->data ?? []);
35✔
141
        $collection->set(DataProcessing\DataSource\DataSource::ProcessorConfiguration, $config);
35✔
142

143
        if ($this->cObj !== null) {
35✔
144
            if (\is_array($config['variables.'] ?? null)) {
35✔
NEW
145
                $variables = $config['variables.'];
×
146
            } else {
147
                $variables = [];
35✔
148
            }
149

150
            // Trigger pre-processors
151
            $variables = $this->triggerDataSourceAwareProcessors(
35✔
152
                $config,
35✔
153
                'preProcessing',
35✔
154
                $variables,
35✔
155
                $collection,
35✔
156
                $this->cObj,
35✔
157
            );
35✔
158

159
            // Process content object variables and simple variables
160
            $processor = Renderer\Variables\VariablesProcessor::for($this->cObj);
35✔
161
            $variables = $processor->process($variables);
35✔
162
        } else {
NEW
163
            $variables = [];
×
164
        }
165

166
        // Add current context variables
167
        $variables['data'] = $this->cObj->data ?? [];
35✔
168
        $variables['current'] = $this->cObj?->data[$this->cObj->currentValKey] ?? null;
35✔
169

170
        // Process variables with configured data processors
171
        if ($this->cObj !== null) {
35✔
172
            $variables = $this->contentDataProcessor->process($this->cObj, $config, $variables);
35✔
173
        }
174

175
        // Make settings available as variables
176
        if (isset($config['settings.'])) {
35✔
177
            $variables['settings'] = $this->typoScriptService->convertTypoScriptArrayToPlainArray($config['settings.']);
1✔
178
        }
179

180
        // Trigger post-processors
181
        if ($this->cObj !== null) {
35✔
182
            $variables = $this->triggerDataSourceAwareProcessors(
35✔
183
                $config,
35✔
184
                'postProcessing',
35✔
185
                $variables,
35✔
186
                $collection,
35✔
187
                $this->cObj,
35✔
188
            );
35✔
189
        }
190

191
        return $variables;
35✔
192
    }
193

194
    /**
195
     * Process and register assets from TypoScript configuration.
196
     *
197
     * @param array<string, mixed> $config
198
     * @throws Exception\InvalidAssetConfigurationException
199
     */
200
    private function processAssets(array $config): void
35✔
201
    {
202
        if (is_array($config['assets.'] ?? null)) {
35✔
203
            $this->assetHandler->collectAssets(
16✔
204
                $this->typoScriptService->convertTypoScriptArrayToPlainArray($config['assets.']),
16✔
205
            );
16✔
206
        }
207

208
        if (is_string($config['headerAssets'] ?? null) && is_array($config['headerAssets.'] ?? null)) {
34✔
209
            $headerAssets = $this->cObj?->cObjGetSingle($config['headerAssets'], $config['headerAssets.']) ?? '';
3✔
210
        } else {
211
            $headerAssets = '';
32✔
212
        }
213

214
        if (is_string($config['footerAssets'] ?? null) && is_array($config['footerAssets.'] ?? null)) {
34✔
215
            $footerAssets = $this->cObj?->cObjGetSingle($config['footerAssets'], $config['footerAssets.']) ?? '';
1✔
216
        } else {
217
            $footerAssets = '';
34✔
218
        }
219

220
        if (\trim($headerAssets) !== '') {
34✔
221
            $this->getPageRenderer()->addHeaderData($headerAssets);
3✔
222
        }
223

224
        if (\trim($footerAssets) !== '') {
34✔
225
            $this->getPageRenderer()->addFooterData($footerAssets);
1✔
226
        }
227
    }
228
}
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