1624283bfd18faca01ce4c11cfd2e2d1dc3c378c
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Debug / RenderViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Debug;
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
17 use TYPO3\CMS\Core\Utility\PathUtility;
18 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
19
20 /**
21 * Debuggable version of f:render - performs the same
22 * rendering operation but wraps the output with HTML
23 * that can be inspected with the admin panel in FE.
24 *
25 * Replaces `f:render` when the admin panel decides
26 * (see ViewHelperResolver class). Also possible to use
27 * explicitly by using `f:debug.render` instead of the
28 * normal `f:render` statement.
29 *
30 * @api
31 */
32 class RenderViewHelper extends AbstractViewHelper
33 {
34
35 /**
36 * @var bool
37 */
38 protected $escapeOutput = false;
39
40 /**
41 * Initializes additional arguments available for this view helper.
42 */
43 public function initializeArguments()
44 {
45 parent::initializeArguments();
46 $this->registerArgument('section', 'string', 'Section to render - combine with partial to render section in partial', false, null);
47 $this->registerArgument('partial', 'string', 'Partial to render, with or without section', false, null);
48 $this->registerArgument('arguments', 'array', 'Array of variables to be transferred. Use {_all} for all variables', false, []);
49 $this->registerArgument('optional', 'boolean', 'If TRUE, considers the *section* optional. Partial never is.', false, false);
50 $this->registerArgument('default', 'mixed', 'Value (usually string) to be displayed if the section or partial does not exist', false, null);
51 $this->registerArgument('contentAs', 'string', 'If used, renders the child content and adds it as a template variable with this name for use in the partial/section', false, null);
52 }
53
54 /**
55 * Renders the content.
56 *
57 * @return string
58 * @api
59 */
60 public function render()
61 {
62 $section = $this->arguments['section'];
63 $partial = $this->arguments['partial'];
64 $arguments = (array) $this->arguments['arguments'];
65 $optional = (boolean) $this->arguments['optional'];
66 $contentAs = $this->arguments['contentAs'];
67 $tagContent = $this->renderChildren();
68
69 if ($contentAs !== null) {
70 $arguments[$contentAs] = $tagContent;
71 }
72
73 $content = '';
74 if ($partial !== null) {
75 $content = $this->viewHelperVariableContainer->getView()->renderPartial($partial, $section, $arguments, $optional);
76 } elseif ($section !== null) {
77 $content = $this->viewHelperVariableContainer->getView()->renderSection($section, $arguments, $optional);
78 }
79 // Replace empty content with default value. If default is
80 // not set, NULL is returned and cast to a new, empty string
81 // outside of this ViewHelper.
82 if ($content === '') {
83 $content = isset($this->arguments['default']) ? $this->arguments['default'] : $tagContent;
84 }
85
86 $cssRules = [];
87 $cssRules[] = 'display: block';
88 $cssRules[] = 'background-color: #fff';
89 $cssRules[] = 'padding: 5px';
90 $cssRules[] = 'border: 1px solid #f00';
91 $cssRules[] = 'color: #000';
92 $cssRules[] = 'overflow: hidden';
93 $cssWrapper = implode(';', $cssRules);
94 $cssRules[] = 'font-size: 11px';
95 $cssRules[] = 'font-family: Monospace';
96 $cssTitle = implode(';', $cssRules);
97
98 $debugInfo = [];
99 if (isset($this->arguments['partial'])) {
100 $path = $this->renderingContext->getTemplatePaths()->getPartialPathAndFilename($partial);
101 $path = PathUtility::stripPathSitePrefix($path);
102 $path = str_replace('typo3conf/ext/', 'EXT:', $path);
103 $path = str_replace('typo3/sysext/', 'EXT:', $path);
104 $debugInfo['Partial'] = 'Partial: ' . $path;
105 }
106 if (isset($this->arguments['section'])) {
107 $debugInfo['Section'] = 'Section: ' . htmlspecialchars($section);
108 }
109
110 $debugContent = sprintf(
111 '<strong>%s</strong>',
112 implode('<br />', $debugInfo)
113 );
114
115 return sprintf(
116 '<div class="t3js-debug-template" title="%s" style="%s"><span style="%s">%s</span>%s</div>',
117 htmlspecialchars(implode('/', array_keys($debugInfo))),
118 $cssTitle,
119 $cssWrapper,
120 $debugContent,
121 $content
122 );
123 }
124 }