‪TYPO3CMS  9.5
RenderViewHelper.php
Go to the documentation of this file.
1 <?php
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 
19 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
20 
30 class ‪RenderViewHelper extends AbstractViewHelper
31 {
32 
36  protected ‪$escapeOutput = false;
37 
41  public function ‪initializeArguments()
42  {
43  parent::initializeArguments();
44  $this->registerArgument('debug', 'boolean', 'If true, the admin panel shows debug information if activated,', false, true);
45  $this->registerArgument('section', 'string', 'Section to render - combine with partial to render section in partial', false, null);
46  $this->registerArgument('partial', 'string', 'Partial to render, with or without section', false, null);
47  $this->registerArgument('arguments', 'array', 'Array of variables to be transferred. Use {_all} for all variables', false, []);
48  $this->registerArgument('optional', 'boolean', 'If TRUE, considers the *section* optional. Partial never is.', false, false);
49  $this->registerArgument('default', 'mixed', 'Value (usually string) to be displayed if the section or partial does not exist', false, null);
50  $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);
51  }
52 
58  public function ‪render()
59  {
60  $isDebug = $this->arguments['debug'];
61  $section = $this->arguments['section'];
62  $partial = $this->arguments['partial'];
63  $arguments = (array)$this->arguments['arguments'];
64  $optional = (boolean)$this->arguments['optional'];
65  $contentAs = $this->arguments['contentAs'];
66  $tagContent = $this->renderChildren();
67 
68  if ($contentAs !== null) {
69  $arguments[$contentAs] = $tagContent;
70  }
71 
72  $content = '';
73  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
74  if ($partial !== null) {
75  $content = $viewHelperVariableContainer->getView()->renderPartial($partial, $section, $arguments, $optional);
76  } elseif ($section !== null) {
77  $content = $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 = $this->arguments['default'] ?? $tagContent;
84  }
85 
86  // if debug is disabled, return content
87  if (!$isDebug) {
88  return $content;
89  }
90 
91  $cssRules = [];
92  $cssRules[] = 'display: block';
93  $cssRules[] = 'background-color: #fff';
94  $cssRules[] = 'padding: 5px';
95  $cssRules[] = 'border: 1px solid #f00';
96  $cssRules[] = 'color: #000';
97  $cssRules[] = 'overflow: hidden';
98  $cssWrapper = implode(';', $cssRules);
99  $cssRules[] = 'font-size: 11px';
100  $cssRules[] = 'font-family: Monospace';
101  $cssTitle = implode(';', $cssRules);
102 
103  $debugInfo = [];
104  if (isset($this->arguments['partial'])) {
105  $path = $this->renderingContext->getTemplatePaths()->getPartialPathAndFilename($partial);
106  $path = str_replace(
107  [
108  ‪Environment::getBackendPath() . '/ext/',
111  ],
112  'EXT:',
113  $path
114  );
116  $debugInfo['Partial'] = 'Partial: ' . $path;
117  }
118  if (isset($this->arguments['section'])) {
119  $debugInfo['Section'] = 'Section: ' . htmlspecialchars($section);
120  }
121 
122  $debugContent = sprintf(
123  '<strong>%s</strong>',
124  implode('<br />', $debugInfo)
125  );
126 
127  return sprintf(
128  '<div class="t3js-debug-template" title="%s" style="%s"><span style="%s">%s</span>%s</div>',
129  htmlspecialchars(implode('/', array_keys($debugInfo))),
130  $cssTitle,
131  $cssWrapper,
132  $debugContent,
133  $content
134  );
135  }
136 }
‪TYPO3\CMS\Fluid\ViewHelpers\Debug\RenderViewHelper\initializeArguments
‪initializeArguments()
Definition: RenderViewHelper.php:40
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:23
‪TYPO3\CMS\Fluid\ViewHelpers\Debug\RenderViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: RenderViewHelper.php:35
‪TYPO3\CMS\Fluid\ViewHelpers\Debug\RenderViewHelper\render
‪string render()
Definition: RenderViewHelper.php:57
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static string stripPathSitePrefix($path)
Definition: PathUtility.php:371
‪TYPO3\CMS\Core\Core\Environment\getFrameworkBasePath
‪static string getFrameworkBasePath()
Definition: Environment.php:234
‪TYPO3\CMS\Fluid\ViewHelpers\Debug
Definition: RenderViewHelper.php:2
‪TYPO3\CMS\Core\Core\Environment\getBackendPath
‪static string getBackendPath()
Definition: Environment.php:223
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Fluid\ViewHelpers\Debug\RenderViewHelper
Definition: RenderViewHelper.php:31
‪TYPO3\CMS\Core\Core\Environment\getExtensionsPath
‪static string getExtensionsPath()
Definition: Environment.php:245