1<?php
2namespace TYPO3\CMS\Frontend\ContentObject;
3
4/*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17use TYPO3\CMS\Core\Html\HtmlParser;
18use TYPO3\CMS\Core\Service\MarkerBasedTemplateService;
19use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21/**
22 * Contains TEMPLATE class object.
23 */
24class TemplateContentObject extends AbstractContentObject
25{
26    /**
27     * @var MarkerBasedTemplateService
28     */
29    protected $templateService;
30
31    /**
32     * Default constructor, which also instantiates the MarkerBasedTemplateService.
33     *
34     * @param ContentObjectRenderer $cObj
35     */
36    public function __construct(ContentObjectRenderer $cObj)
37    {
38        $this->templateService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
39        parent::__construct($cObj);
40    }
41
42    /**
43     * Rendering the cObject, TEMPLATE
44     *
45     * @param array $conf Array of TypoScript properties
46     * @return string Output
47     * @see substituteMarkerArrayCached()
48     */
49    public function render($conf = [])
50    {
51        $subparts = [];
52        $marks = [];
53        $wraps = [];
54        $markerWrap = isset($conf['markerWrap.']) ? $this->cObj->stdWrap($conf['markerWrap'], $conf['markerWrap.']) : $conf['markerWrap'];
55        if (!$markerWrap) {
56            $markerWrap = '### | ###';
57        }
58        list($PRE, $POST) = explode('|', $markerWrap);
59        $POST = trim($POST);
60        $PRE = trim($PRE);
61        // Getting the content
62        $content = $this->cObj->cObjGetSingle($conf['template'], $conf['template.'], 'template');
63        $workOnSubpart = isset($conf['workOnSubpart.']) ? $this->cObj->stdWrap($conf['workOnSubpart'], $conf['workOnSubpart.']) : $conf['workOnSubpart'];
64        if ($workOnSubpart) {
65            $content = $this->templateService->getSubpart($content, $PRE . $workOnSubpart . $POST);
66        }
67        // Fixing all relative paths found:
68        if ($conf['relPathPrefix']) {
69            $htmlParser = GeneralUtility::makeInstance(HtmlParser::class);
70            $content = $htmlParser->prefixResourcePath($conf['relPathPrefix'], $content, $conf['relPathPrefix.']);
71        }
72        if ($content) {
73            $nonCachedSubst = isset($conf['nonCachedSubst.']) ? $this->cObj->stdWrap($conf['nonCachedSubst'], $conf['nonCachedSubst.']) : $conf['nonCachedSubst'];
74            // NON-CACHED:
75            if ($nonCachedSubst) {
76                // Getting marks
77                if (is_array($conf['marks.'])) {
78                    foreach ($conf['marks.'] as $theKey => $theValue) {
79                        if (!strstr($theKey, '.')) {
80                            $content = str_replace($PRE . $theKey . $POST, $this->cObj->cObjGetSingle($theValue, $conf['marks.'][$theKey . '.'], 'marks.' . $theKey), $content);
81                        }
82                    }
83                }
84                // Getting subparts.
85                if (is_array($conf['subparts.'])) {
86                    foreach ($conf['subparts.'] as $theKey => $theValue) {
87                        if (!strstr($theKey, '.')) {
88                            $subpart = $this->templateService->getSubpart($content, $PRE . $theKey . $POST);
89                            if ($subpart) {
90                                $this->cObj->setCurrentVal($subpart);
91                                $content = $this->templateService->substituteSubpart($content, $PRE . $theKey . $POST, $this->cObj->cObjGetSingle($theValue, $conf['subparts.'][$theKey . '.'], 'subparts.' . $theKey), true);
92                            }
93                        }
94                    }
95                }
96                // Getting subpart wraps
97                if (is_array($conf['wraps.'])) {
98                    foreach ($conf['wraps.'] as $theKey => $theValue) {
99                        if (!strstr($theKey, '.')) {
100                            $subpart = $this->templateService->getSubpart($content, $PRE . $theKey . $POST);
101                            if ($subpart) {
102                                $this->cObj->setCurrentVal($subpart);
103                                $content = $this->templateService->substituteSubpart($content, $PRE . $theKey . $POST, explode('|', $this->cObj->cObjGetSingle($theValue, $conf['wraps.'][$theKey . '.'], 'wraps.' . $theKey)), true);
104                            }
105                        }
106                    }
107                }
108            } else {
109                // CACHED
110                // Getting subparts.
111                if (is_array($conf['subparts.'])) {
112                    foreach ($conf['subparts.'] as $theKey => $theValue) {
113                        if (!strstr($theKey, '.')) {
114                            $subpart = $this->templateService->getSubpart($content, $PRE . $theKey . $POST);
115                            if ($subpart) {
116                                $GLOBALS['TSFE']->register['SUBPART_' . $theKey] = $subpart;
117                                $subparts[$theKey]['name'] = $theValue;
118                                $subparts[$theKey]['conf'] = $conf['subparts.'][$theKey . '.'];
119                            }
120                        }
121                    }
122                }
123                // Getting marks
124                if (is_array($conf['marks.'])) {
125                    foreach ($conf['marks.'] as $theKey => $theValue) {
126                        if (!strstr($theKey, '.')) {
127                            $marks[$theKey]['name'] = $theValue;
128                            $marks[$theKey]['conf'] = $conf['marks.'][$theKey . '.'];
129                        }
130                    }
131                }
132                // Getting subpart wraps
133                if (is_array($conf['wraps.'])) {
134                    foreach ($conf['wraps.'] as $theKey => $theValue) {
135                        if (!strstr($theKey, '.')) {
136                            $wraps[$theKey]['name'] = $theValue;
137                            $wraps[$theKey]['conf'] = $conf['wraps.'][$theKey . '.'];
138                        }
139                    }
140                }
141                // Getting subparts
142                $subpartArray = [];
143                foreach ($subparts as $theKey => $theValue) {
144                    // Set current with the content of the subpart...
145                    $this->cObj->data[$this->cObj->currentValKey] = $GLOBALS['TSFE']->register['SUBPART_' . $theKey];
146                    // Get subpart cObject and substitute it!
147                    $subpartArray[$PRE . $theKey . $POST] = $this->cObj->cObjGetSingle($theValue['name'], $theValue['conf'], 'subparts.' . $theKey);
148                }
149                // Reset current to empty
150                $this->cObj->data[$this->cObj->currentValKey] = '';
151                // Getting marks
152                $markerArray = [];
153                foreach ($marks as $theKey => $theValue) {
154                    $markerArray[$PRE . $theKey . $POST] = $this->cObj->cObjGetSingle($theValue['name'], $theValue['conf'], 'marks.' . $theKey);
155                }
156                // Getting wraps
157                $subpartWraps = [];
158                foreach ($wraps as $theKey => $theValue) {
159                    $subpartWraps[$PRE . $theKey . $POST] = explode('|', $this->cObj->cObjGetSingle($theValue['name'], $theValue['conf'], 'wraps.' . $theKey));
160                }
161                // Substitution
162                $substMarksSeparately = isset($conf['substMarksSeparately.']) ? $this->cObj->stdWrap($conf['substMarksSeparately'], $conf['substMarksSeparately.']) : $conf['substMarksSeparately'];
163                if ($substMarksSeparately) {
164                    $content = $this->templateService->substituteMarkerArrayCached($content, [], $subpartArray, $subpartWraps);
165                    $content = $this->templateService->substituteMarkerArray($content, $markerArray);
166                } else {
167                    $content = $this->templateService->substituteMarkerArrayCached($content, $markerArray, $subpartArray, $subpartWraps);
168                }
169            }
170        }
171        if (isset($conf['stdWrap.'])) {
172            $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
173        }
174        return $content;
175    }
176}
177