TYPO3 CMS  TYPO3_8-7
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 
32 class RenderViewHelper extends AbstractViewHelper
33 {
34 
38  protected $escapeOutput = false;
39 
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 
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 }