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