d7a9690c9ffb097e81e14e4fba4bbe6df927b21b
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / FluidTemplateContentObject.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2011 Xavier Perseguers <typo3@perseguers.ch>
8 * (c) 2010-2011 Steffen Kamper <steffen@typo3.org>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Contains TEMPLATE class object.
32 *
33 * @author Xavier Perseguers <typo3@perseguers.ch>
34 * @author Steffen Kamper <steffen@typo3.org>
35 * @author Bastian Waidelich <bastian@typo3.org>
36 */
37 class FluidTemplateContentObject extends \TYPO3\CMS\Frontend\ContentObject\AbstractContentObject {
38
39 /**
40 * Rendering the cObject, FLUIDTEMPLATE
41 * configuration properties are:
42 * - file string+stdWrap the FLUID template file
43 * - extbase.pluginName, extbase.controllerExtensionName,
44 * - extbase.controllerName, extbase.controllerActionName
45 * - layoutRootPath filepath+stdWrap by default,
46 * - partialRootPath filepath+stdWrap the
47 * - variables array of cObjects, the keys are the variable names in fluid
48 *
49 * an example would be
50 * 10 = FLUIDTEMPLATE
51 * 10.file = fileadmin/templates/mytemplate.html
52 * 10.partialRootPath = fileadmin/templates/partial/
53 * 10.variables {
54 * mylabel = TEXT
55 * mylabel.value = Label from TypoScript coming
56 * }
57 *
58 * @param array $conf Array of TypoScript properties
59 * @return string The HTML output
60 * @author Steffen Ritter <info@steffen-ritter.net>
61 * @author Benjamin Mack <benni@typo3.org>
62 * @author Bastian Waidelich <bastian@typo3.org>
63 */
64 public function render($conf = array()) {
65 // check if the needed extensions are installed
66 if (!\TYPO3\CMS\Core\Extension\ExtensionManager::isLoaded('fluid')) {
67 return 'You need to install "Fluid" in order to use the FLUIDTEMPLATE content element';
68 }
69 /** 1. initializing Fluid StandaloneView and setting configuration parameters */
70 $view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
71 // Fetch the Fluid template
72 $file = isset($conf['file.']) ? $this->cObj->stdWrap($conf['file'], $conf['file.']) : $conf['file'];
73 $templatePathAndFilename = $GLOBALS['TSFE']->tmpl->getFileName($file);
74 $view->setTemplatePathAndFilename($templatePathAndFilename);
75 // Override the default layout path via typoscript
76 $layoutRootPath = isset($conf['layoutRootPath.']) ? $this->cObj->stdWrap($conf['layoutRootPath'], $conf['layoutRootPath.']) : $conf['layoutRootPath'];
77 if ($layoutRootPath) {
78 $layoutRootPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($layoutRootPath);
79 $view->setLayoutRootPath($layoutRootPath);
80 }
81 // Override the default partials path via typoscript
82 $partialRootPath = isset($conf['partialRootPath.']) ? $this->cObj->stdWrap($conf['partialRootPath'], $conf['partialRootPath.']) : $conf['partialRootPath'];
83 if ($partialRootPath) {
84 $partialRootPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($partialRootPath);
85 $view->setPartialRootPath($partialRootPath);
86 }
87 // Override the default format
88 $format = isset($conf['format.']) ? $this->cObj->stdWrap($conf['format'], $conf['format.']) : $conf['format'];
89 if ($format) {
90 $view->setFormat($format);
91 }
92 // Set some default variables for initializing Extbase
93 $requestPluginName = isset($conf['extbase.']['pluginName.']) ? $this->cObj->stdWrap($conf['extbase.']['pluginName'], $conf['extbase.']['pluginName.']) : $conf['extbase.']['pluginName'];
94 if ($requestPluginName) {
95 $view->getRequest()->setPluginName($requestPluginName);
96 }
97 $requestControllerExtensionName = isset($conf['extbase.']['controllerExtensionName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerExtensionName'], $conf['extbase.']['controllerExtensionName.']) : $conf['extbase.']['controllerExtensionName'];
98 if ($requestControllerExtensionName) {
99 $view->getRequest()->setControllerExtensionName($requestControllerExtensionName);
100 }
101 $requestControllerName = isset($conf['extbase.']['controllerName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerName'], $conf['extbase.']['controllerName.']) : $conf['extbase.']['controllerName'];
102 if ($requestControllerName) {
103 $view->getRequest()->setControllerName($requestControllerName);
104 }
105 $requestControllerActionName = isset($conf['extbase.']['controllerActionName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerActionName'], $conf['extbase.']['controllerActionName.']) : $conf['extbase.']['controllerActionName'];
106 if ($requestControllerActionName) {
107 $view->getRequest()->setControllerActionName($requestControllerActionName);
108 }
109 /** 2. variable assignment */
110 $reservedVariables = array('data', 'current');
111 // Accumulate the variables to be replaced
112 // and loop them through cObjGetSingle
113 $variables = (array) $conf['variables.'];
114 foreach ($variables as $variableName => $cObjType) {
115 if (is_array($cObjType)) {
116 continue;
117 }
118 if (!in_array($variableName, $reservedVariables)) {
119 $view->assign($variableName, $this->cObj->cObjGetSingle($cObjType, $variables[$variableName . '.']));
120 } else {
121 throw new \InvalidArgumentException(('Cannot use reserved name "' . $variableName) . '" as variable name in FLUIDTEMPLATE.', 1288095720);
122 }
123 }
124 $view->assign('data', $this->cObj->data);
125 $view->assign('current', $this->cObj->data[$this->cObj->currentValKey]);
126 /** 3. render the content */
127 $theValue = $view->render();
128 if (isset($conf['stdWrap.'])) {
129 $theValue = $this->cObj->stdWrap($theValue, $conf['stdWrap.']);
130 }
131 return $theValue;
132 }
133
134 }
135
136
137 ?>