‪TYPO3CMS  ‪main
RenderViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
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 
19 
22 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
23 
33 final class ‪RenderViewHelper extends AbstractViewHelper
34 {
38  protected ‪$escapeOutput = false;
39 
40  public function ‪initializeArguments(): void
41  {
42  parent::initializeArguments();
43  $this->registerArgument('debug', 'boolean', 'If true, the admin panel shows debug information if activated,', false, true);
44  $this->registerArgument('section', 'string', 'Section to render - combine with partial to render section in partial', false);
45  $this->registerArgument('partial', 'string', 'Partial to render, with or without section', false);
46  $this->registerArgument('arguments', 'array', 'Array of variables to be transferred. Use {_all} for all variables', false, []);
47  $this->registerArgument('optional', 'boolean', 'If TRUE, considers the *section* optional. Partial never is.', false, false);
48  $this->registerArgument('default', 'mixed', 'Value (usually string) to be displayed if the section or partial does not exist', false);
49  $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);
50  }
51 
52  public function ‪render(): string
53  {
54  $isDebug = $this->arguments['debug'];
55  $section = $this->arguments['section'];
56  $partial = $this->arguments['partial'];
57  $arguments = (array)$this->arguments['arguments'];
58  $optional = (bool)$this->arguments['optional'];
59  $contentAs = $this->arguments['contentAs'];
60  $tagContent = $this->renderChildren();
61 
62  if ($contentAs !== null) {
63  $arguments[$contentAs] = $tagContent;
64  }
65 
66  $content = '';
67  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
68  if ($partial !== null) {
69  $content = $viewHelperVariableContainer->getView()->renderPartial($partial, $section, $arguments, $optional);
70  } elseif ($section !== null) {
71  $content = $viewHelperVariableContainer->getView()->renderSection($section, $arguments, $optional);
72  }
73  // Replace empty content with default value. If default is
74  // not set, NULL is returned and cast to a new, empty string
75  // outside this ViewHelper.
76  if ($content === '') {
77  $content = $this->arguments['default'] ?? (string)$tagContent;
78  }
79 
80  // if debug is disabled, return content
81  if (!$isDebug) {
82  return $content;
83  }
84 
85  $cssRules = [];
86  $cssRules[] = 'display: block';
87  $cssRules[] = 'background-color: #fff';
88  $cssRules[] = 'padding: 5px';
89  $cssRules[] = 'border: 1px solid #f00';
90  $cssRules[] = 'color: #000';
91  $cssRules[] = 'overflow: hidden';
92  $cssWrapper = implode(';', $cssRules);
93  $cssRules[] = 'font-size: 11px';
94  $cssRules[] = 'font-family: Monospace';
95  $cssTitle = implode(';', $cssRules);
96 
97  $debugInfo = [];
98  if (isset($this->arguments['partial'])) {
99  $path = $this->renderingContext->getTemplatePaths()->getPartialPathAndFilename($partial);
100  $path = str_replace(
101  [
104  ],
105  'EXT:',
106  $path
107  );
109  $debugInfo['Partial'] = 'Partial: ' . $path;
110  }
111  if (isset($this->arguments['section'])) {
112  $debugInfo['Section'] = 'Section: ' . htmlspecialchars($section);
113  }
114 
115  $debugContent = sprintf(
116  '<strong>%s</strong>',
117  implode('<br />', $debugInfo)
118  );
119 
120  return sprintf(
121  '<div class="t3js-debug-template" title="%s" style="%s"><span style="%s">%s</span>%s</div>',
122  htmlspecialchars(implode('/', array_keys($debugInfo))),
123  $cssTitle,
124  $cssWrapper,
125  $debugContent,
126  $content
127  );
128  }
129 }
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static stripPathSitePrefix(string $path)
Definition: PathUtility.php:428
‪TYPO3\CMS\Fluid\ViewHelpers\Debug\RenderViewHelper\initializeArguments
‪initializeArguments()
Definition: RenderViewHelper.php:39
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Fluid\ViewHelpers\Debug\RenderViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: RenderViewHelper.php:37
‪TYPO3\CMS\Core\Core\Environment\getExtensionsPath
‪static getExtensionsPath()
Definition: Environment.php:253
‪TYPO3\CMS\Fluid\ViewHelpers\Debug
Definition: RenderViewHelper.php:18
‪TYPO3\CMS\Core\Core\Environment\getFrameworkBasePath
‪static getFrameworkBasePath()
Definition: Environment.php:245
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Fluid\ViewHelpers\Debug\RenderViewHelper
Definition: RenderViewHelper.php:34
‪TYPO3\CMS\Fluid\ViewHelpers\Debug\RenderViewHelper\render
‪render()
Definition: RenderViewHelper.php:51