[FEATURE] Add TemplateRootPaths support to cObject FLUIDTEMPLATE 31/38331/10
authorFrans Saris <franssaris@gmail.com>
Sun, 29 Mar 2015 11:46:45 +0000 (13:46 +0200)
committerFrank Nägler <typo3@naegler.net>
Fri, 8 May 2015 21:37:01 +0000 (23:37 +0200)
cObject FLUIDTEMPLATE is extended with `templateRootPaths` and
`templateName`. Now you can set a template name and when rendering the
template this name is used together with the set format to find the
template in the given templateRootPaths with the same fallback logic as
layoutRootPath and partialRootPath.

Resolves: #66111
Releases: master
Change-Id: I30d7fa63f63f818b5da31194bad1dccce95929e2
Reviewed-on: http://review.typo3.org/38331
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Frank Nägler <typo3@naegler.net>
Tested-by: Frank Nägler <typo3@naegler.net>
typo3/sysext/core/Documentation/Changelog/master/Feature-66111-AddTemplaterootpathsSupportToCobjectFluidtemplate.rst [new file with mode: 0644]
typo3/sysext/frontend/Classes/ContentObject/FluidTemplateContentObject.php
typo3/sysext/frontend/Tests/Unit/ContentObject/FluidTemplateContentObjectTest.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-66111-AddTemplaterootpathsSupportToCobjectFluidtemplate.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-66111-AddTemplaterootpathsSupportToCobjectFluidtemplate.rst
new file mode 100644 (file)
index 0000000..0a41369
--- /dev/null
@@ -0,0 +1,82 @@
+========================================================================
+Feature: #66111 - Add TemplateRootPaths support to cObject FLUIDTEMPLATE
+========================================================================
+
+Description
+===========
+
+cObject FLUIDTEMPLATE is extended with ``templateRootPaths`` and ``templateName``. Now you can set a template name and when rendering the template this name is used together with the set format to find the template in the given templateRootPaths with the same fallback logic as layoutRootPath and partialRootPath
+
+- templateName = string/stdWrap
+- templateRootPaths = array of file paths with "EXT:" prefix support
+
+
+Example 1:
+---------------
+
+.. code-block:: typoscript
+
+       lib.stdContent = FLUIDTEMPLATE
+       lib.stdContent {
+               templateName = Default
+               layoutRootPaths {
+                       10 = EXT:frontend/Resources/Private/Layouts
+                       20 = EXT:sitemodification/Resources/Private/Layouts
+               }
+               partialRootPaths {
+                       10 = EXT:frontend/Resources/Private/Partials
+                       20 = EXT:sitemodification/Resources/Private/Partials
+               }
+               templateRootPaths {
+                       10 = EXT:frontend/Resources/Private/Templates
+                       20 = EXT:sitemodification/Resources/Private/Templates
+               }
+               variable {
+                       foo = bar
+               }
+       }
+
+Example 2:
+---------------
+
+.. code-block:: typoscript
+
+       lib.stdContent = FLUIDTEMPLATE
+       lib.stdContent {
+
+               templateName = TEXT
+               templateName.stdWrap {
+                       cObject = TEXT
+                       cObject {
+                               data = levelfield:-2,backend_layout_next_level,slide
+                               override.field = backend_layout
+                               split {
+                                       token = frontend__
+                                       1.current = 1
+                                       1.wrap = |
+                               }
+                       }
+                       ifEmpty = Default
+               }
+               layoutRootPaths {
+                       10 = EXT:frontend/Resources/Private/Layouts
+                       20 = EXT:sitemodification/Resources/Private/Layouts
+               }
+               partialRootPaths {
+                       10 = EXT:frontend/Resources/Private/Partials
+                       20 = EXT:sitemodification/Resources/Private/Partials
+               }
+               templateRootPaths {
+                       10 = EXT:frontend/Resources/Private/Templates
+                       20 = EXT:sitemodification/Resources/Private/Templates
+               }
+               variable {
+                       foo = bar
+               }
+       }
+
+
+Impact
+======
+
+If templateName and templateRootPaths are set the template and file options are neglected.
\ No newline at end of file
index 0d90220..ac16243 100644 (file)
@@ -14,18 +14,13 @@ namespace TYPO3\CMS\Frontend\ContentObject;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Extbase\Utility\ArrayUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Service\TypoScriptService;
 use TYPO3\CMS\Fluid\View\StandaloneView;
 
 /**
- * Contains TEMPLATE class object.
- *
- * @author Xavier Perseguers <typo3@perseguers.ch>
- * @author Steffen Kamper <steffen@typo3.org>
- * @author Bastian Waidelich <bastian@typo3.org>
- * @author Steffen Ritter <info@steffen-ritter.net>
- * @author Benjamin Mack <benni@typo3.org>
+ * Contains FLUIDTEMPLATE class object
  */
 class FluidTemplateContentObject extends AbstractContentObject {
 
@@ -49,9 +44,10 @@ class FluidTemplateContentObject extends AbstractContentObject {
         *
         * Example:
         * 10 = FLUIDTEMPLATE
-        * 10.template = FILE
-        * 10.template.file = fileadmin/templates/mytemplate.html
-        * 10.partialRootPaths.10 = fileadmin/templates/partial/
+        * 10.templateName = MyTemplate
+        * 10.templateRootPaths.10 = EXT:site_configuration/Resources/Private/Templates/
+        * 10.partialRootPaths.10 = EXT:site_configuration/Resources/Private/Patials/
+        * 10.layoutRootPaths.10 = EXT:site_configuration/Resources/Private/Layouts/
         * 10.variables {
         *   mylabel = TEXT
         *   mylabel.value = Label from TypoScript coming
@@ -68,10 +64,10 @@ class FluidTemplateContentObject extends AbstractContentObject {
                        $conf = array();
                }
 
+               $this->setFormat($conf);
                $this->setTemplate($conf);
                $this->setLayoutRootPath($conf);
                $this->setPartialRootPath($conf);
-               $this->setFormat($conf);
                $this->setExtbaseVariables($conf);
                $this->assignSettings($conf);
                $this->assignContentObjectVariables($conf);
@@ -105,10 +101,20 @@ class FluidTemplateContentObject extends AbstractContentObject {
         * @throws \InvalidArgumentException
         */
        protected function setTemplate(array $conf) {
-               // Fetch the Fluid template
-               if (!empty($conf['template']) && !empty($conf['template.'])) {
+               // Fetch the Fluid template by templateName
+               if (!empty($conf['templateName']) && !empty($conf['templateRootPaths.']) && is_array($conf['templateRootPaths.'])) {
+                       $templateRootPaths = array();
+                       foreach ($conf['templateRootPaths.'] as $key => $path) {
+                               $templateRootPaths[$key] = GeneralUtility::getFileAbsFileName($path);
+                       }
+                       $this->view->setTemplateRootPaths($templateRootPaths);
+                       $templateName = isset($conf['templateName.']) ? $this->cObj->stdWrap($conf['templateName'], $conf['templateName.']) : $conf['templateName'];
+                       $this->view->setTemplate($templateName);
+               // Fetch the Fluid template by template cObject
+               } elseif (!empty($conf['template']) && !empty($conf['template.'])) {
                        $templateSource = $this->cObj->cObjGetSingle($conf['template'], $conf['template.']);
                        $this->view->setTemplateSource($templateSource);
+               // Fetch the Fluid template by file stdWrap
                } else {
                        $file = isset($conf['file.']) ? $this->cObj->stdWrap($conf['file'], $conf['file.']) : $conf['file'];
                        /** @var $templateService \TYPO3\CMS\Core\TypoScript\TemplateService */
@@ -283,5 +289,4 @@ class FluidTemplateContentObject extends AbstractContentObject {
                }
                return $content;
        }
-
 }
index 21477f5..836277c 100644 (file)
@@ -184,6 +184,60 @@ class FluidTemplateContentObjectTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
        /**
         * @test
         */
+       public function renderSetsTemplateFileByTemplateNameInView() {
+               $this->addMockViewToSubject();
+
+               $this->standaloneView
+                       ->expects($this->any())
+                       ->method('getFormat')
+                       ->will($this->returnValue('html'));
+               $this->standaloneView
+                       ->expects($this->once())
+                       ->method('setTemplate')
+                       ->with('foo');
+
+               $this->subject->render(array(
+                       'templateName' => 'foo',
+                       'templateRootPaths.' => array(
+                               0 => 'dummyPath1/',
+                               1 => 'dummyPath2/')
+                       )
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function renderSetsTemplateFileByTemplateNameStdWrapInView() {
+               $this->addMockViewToSubject();
+
+               $this->contentObjectRenderer
+                       ->expects($this->once())
+                       ->method('stdWrap')
+                       ->with('TEXT', array('value' => 'bar'))
+                       ->will($this->returnValue('bar'));
+               $this->standaloneView
+                       ->expects($this->any())
+                       ->method('getFormat')
+                       ->will($this->returnValue('html'));
+               $this->standaloneView
+                       ->expects($this->once())
+                       ->method('setTemplate')
+                       ->with('bar');
+
+               $this->subject->render(array(
+                       'templateName' => 'TEXT',
+                       'templateName.' => array('value' => 'bar'),
+                       'templateRootPaths.' => array(
+                               0 => 'dummyPath1/',
+                               1 => 'dummyPath2/')
+                       )
+               );
+       }
+
+       /**
+        * @test
+        */
        public function renderSetsLayoutRootPathInView() {
                $this->addMockViewToSubject();
                $this->standaloneView