Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Created May 24, 2011 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwaidelich/988628 to your computer and use it in GitHub Desktop.
Save bwaidelich/988628 to your computer and use it in GitHub Desktop.
Variant View (for Fluid v1.3+ based on Peter Niederlags version: https://gist.github.com/822613)
<?php
/* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation, either version 3 of the License, or (at your *
* option) any later version. *
* *
* This script is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
* TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
* General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with the script. *
* If not, see http://www.gnu.org/licenses/lgpl.html *
* *
* The TYPO3 project - inspiring people to share! *
* */
/**
* Extended Fluid Template View that supports different "variants"
* Usage:
* In your controller add following lines:
*
* protected function setViewConfiguration(Tx_Extbase_MVC_View_ViewInterface $view) {
* parent::setViewConfiguration($view);
* $view->setLayoutVariant($this->settings['layoutVariant']);
* }
*
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
*/
class Tx_YourExtension_View_VariantView extends Tx_Fluid_View_TemplateView {
/**
* Layout to use for this view.
*
* @var string
*/
protected $layoutVariant = NULL;
/**
* @var string
*/
protected $templatePathAndFilenamePattern = '@templateRoot/@controller/@action.@variant.@format';
/**
* @param string $layoutVariant
* @return void
*/
public function setLayoutVariant($layoutVariant) {
$this->layoutVariant = $layoutVariant;
}
/**
* Overwrite/adopted for @variant
* Resolves the possible template/layout/partial paths as usual and then replaces "@layout" in the
* paths by $this->layoutVariant.
* Note: By default, only $templatePathAndFilenamePattern contains this marker, but you can use this for
* layouts/partials too by setting $this->layoutPathAndFilenamePattern / partialPathAndFilenamePattern
*
* @param string $pattern Pattern to be resolved
* @param boolean $bubbleControllerAndSubpackage if TRUE, then we successively split off parts from @controller and @subpackage until both are empty.
* @param boolean $formatIsOptional if TRUE, then half of the resulting strings will have .@format stripped off, and the other half will have it.
* @return array unix style path
* @see Tx_Fluid_View_TemplateView::expandGenericPathPattern()
*/
protected function expandGenericPathPattern($pattern, $bubbleControllerAndSubpackage, $formatIsOptional) {
$paths = parent::expandGenericPathPattern($pattern, $bubbleControllerAndSubpackage, $formatIsOptional);
foreach($paths as &$path) {
if ($this->layoutVariant !== NULL && $this->layoutVariant !== '') {
$path = str_replace('@variant', $this->layoutVariant, $path);
} else {
$path = str_replace('@variant', '', $path);
$path = str_replace('..', '.', $path);
}
}
return $paths;
}
}
?>
<?php
/***************************************************************
* Copyright notice
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Modify your controller like follows:
*/
class Tx_YourExtension_Controller_YourController extends Tx_Extbase_MVC_Controller_ActionController {
/**
* @var string
*/
protected $defaultViewObjectName = 'Tx_YourExtension_View_VariantView';
/**
* @param Tx_Extbase_MVC_View_ViewInterface $view It's a Tx_YourExtension_View_VariantView here!
* @return void
*/
protected function setViewConfiguration(Tx_Extbase_MVC_View_ViewInterface $view) {
parent::setViewConfiguration($view);
$extbaseFrameworkConfiguration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
if (isset($extbaseFrameworkConfiguration['view']['layoutVariant'])
&& strlen($extbaseFrameworkConfiguration['view']['layoutVariant']) > 0
&& method_exists($view, 'setLayoutVariant')) {
$view->setLayoutVariant($extbaseFrameworkConfiguration['view']['layoutVariant']);
}
}
// [...]
}
?>
<T3DataStructure>
<sheets>
<sDEF>
<ROOT>
<TCEforms>
<sheetTitle>Options</sheetTitle>
</TCEforms>
<type>array</type>
<el>
<view.layoutVariant>
<TCEforms>
<label>Layout</label>
<config>
<type>select</type>
<items type="array">
<numIndex index="0" type="array">
<numIndex index="0">- Default -</numIndex>
<numIndex index="1"></numIndex>
</numIndex>
<numIndex index="1" type="array">
<numIndex index="0">Foo</numIndex>
<numIndex index="1">bar</numIndex>
</numIndex>
</items>
<maxitems>1</maxitems>
<size>1</size>
</config>
</TCEforms>
</view.layoutVariant>
</el>
</ROOT>
</sDEF>
</sheets>
</T3DataStructure>
@adymorz
Copy link

adymorz commented Aug 2, 2017

I started a discussion about a solution ready for TYPO3 8.7 on https://stackoverflow.com/q/44568353/7173655

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment