‪TYPO3CMS  10.4
RenderViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
21 
31 class ‪RenderViewHelper extends AbstractViewHelper
32 {
33 
37  protected ‪$escapeOutput = false;
38 
42  public function ‪initializeArguments()
43  {
44  parent::initializeArguments();
45  $this->registerArgument('debug', 'boolean', 'If true, the admin panel shows debug information if activated,', false, true);
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 
59  public function ‪render()
60  {
61  $isDebug = $this->arguments['debug'];
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  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
75  if ($partial !== null) {
76  $content = $viewHelperVariableContainer->getView()->renderPartial($partial, $section, $arguments, $optional);
77  } elseif ($section !== null) {
78  $content = $viewHelperVariableContainer->getView()->renderSection($section, $arguments, $optional);
79  }
80  // Replace empty content with default value. If default is
81  // not set, NULL is returned and cast to a new, empty string
82  // outside of this ViewHelper.
83  if ($content === '') {
84  $content = $this->arguments['default'] ?? $tagContent;
85  }
86 
87  // if debug is disabled, return content
88  if (!$isDebug) {
89  return $content;
90  }
91 
92  $cssRules = [];
93  $cssRules[] = 'display: block';
94  $cssRules[] = 'background-color: #fff';
95  $cssRules[] = 'padding: 5px';
96  $cssRules[] = 'border: 1px solid #f00';
97  $cssRules[] = 'color: #000';
98  $cssRules[] = 'overflow: hidden';
99  $cssWrapper = implode(';', $cssRules);
100  $cssRules[] = 'font-size: 11px';
101  $cssRules[] = 'font-family: Monospace';
102  $cssTitle = implode(';', $cssRules);
103 
104  $debugInfo = [];
105  if (isset($this->arguments['partial'])) {
106  $path = $this->renderingContext->getTemplatePaths()->getPartialPathAndFilename($partial);
107  $path = str_replace(
108  [
109  ‪Environment::getBackendPath() . '/ext/',
112  ],
113  'EXT:',
114  $path
115  );
117  $debugInfo['Partial'] = 'Partial: ' . $path;
118  }
119  if (isset($this->arguments['section'])) {
120  $debugInfo['Section'] = 'Section: ' . htmlspecialchars($section);
121  }
122 
123  $debugContent = sprintf(
124  '<strong>%s</strong>',
125  implode('<br />', $debugInfo)
126  );
127 
128  return sprintf(
129  '<div class="t3js-debug-template" title="%s" style="%s"><span style="%s">%s</span>%s</div>',
130  htmlspecialchars(implode('/', array_keys($debugInfo))),
131  $cssTitle,
132  $cssWrapper,
133  $debugContent,
134  $content
135  );
136  }
137 }
‪TYPO3\CMS\Fluid\ViewHelpers\Debug\RenderViewHelper\initializeArguments
‪initializeArguments()
Definition: RenderViewHelper.php:41
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Fluid\ViewHelpers\Debug\RenderViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: RenderViewHelper.php:36
‪TYPO3\CMS\Fluid\ViewHelpers\Debug\RenderViewHelper\render
‪string render()
Definition: RenderViewHelper.php:58
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static string stripPathSitePrefix($path)
Definition: PathUtility.php:372
‪TYPO3\CMS\Core\Core\Environment\getFrameworkBasePath
‪static string getFrameworkBasePath()
Definition: Environment.php:261
‪TYPO3\CMS\Fluid\ViewHelpers\Debug
Definition: RenderViewHelper.php:16
‪TYPO3\CMS\Core\Core\Environment\getBackendPath
‪static string getBackendPath()
Definition: Environment.php:250
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Fluid\ViewHelpers\Debug\RenderViewHelper
Definition: RenderViewHelper.php:32
‪TYPO3\CMS\Core\Core\Environment\getExtensionsPath
‪static string getExtensionsPath()
Definition: Environment.php:271