Skip to content

Instantly share code, notes, and snippets.

@pniederlag
Created February 11, 2011 16:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pniederlag/822613 to your computer and use it in GitHub Desktop.
Save pniederlag/822613 to your computer and use it in GitHub Desktop.
Tx_Pnutility_View_VariantView
<?php
class Tx_Polarmedia_Controller_MediaController extends Tx_Extbase_MVC_Controller_ActionController {
/**
* @var defaultViewObjectName use custom extended Tx_Fluid_View_TemplateView
*/
protected $defaultViewObjectName = 'Tx_Pnutility_View_VariantView';
public function teaserAction() {
$variant = $this->settings['variant']); // or anything else(userState...)
// $variant = 'mini';
// -> Teaser.Mini.html
// You must not(!) have Teaser.html in the Temaplte Directory or you will run into an exception(!)
$this->view->setLayoutVariant($variant);
}
?>
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* This is a little adoption to Tx_Fluid_View_TemplateView
*
* its purpose is to allow differnet variants of a layout (very similar to @format)
*
* use cases could be: differnet templates for different colPos or based on any other criteria
* Just a different layout/display of the very same data
*
*
* @author pn
*/
class Tx_Pnutility_View_VariantView extends Tx_Fluid_View_TemplateView {
//put your code here
/**
* we add another param to resolve the name of the template
*
* same action/sameformat but different temnplates by using the variant param
*
* usage: Controler->view->setLayoutVariant('foo');
*
* @var string
*/
protected $layoutVariant = NULL;
protected $templatePathAndFilenamePattern = '@templateRoot/@controller/@action.@variant.@format';
public function setLayoutVariant($layoutVariant) {
$this->layoutVariant = $layoutVariant;
}
/**
*
* overwrite/adopted for @variant
*
* attention: due to the currentw ay of ActionController->resolveView() wee need to utilize initializeView() [seee below] to set templateRootPatg ourselfs
*
* Resolve the template path and filename for the given action. If $actionName
* is NULL, looks into the current request.
*
* @param string $actionName Name of the action. If NULL, will be taken from request.
* @return string Full path to template
* @throws Tx_Fluid_View_Exception_InvalidTemplateResourceException
* @author Sebastian Kurfürst <sebastian@typo3.org>
*/
protected function resolveTemplatePathAndFilename($actionName = NULL) {
if ($this->templatePathAndFilename !== NULL) {
return $this->templatePathAndFilename;
}
$actionName = ($actionName !== NULL ? $actionName : $this->controllerContext->getRequest()->getControllerActionName());
$actionName = ucfirst($actionName);
$paths = $this->expandGenericPathPattern($this->templatePathAndFilenamePattern, FALSE, FALSE);
foreach ($paths as &$path) {
// *************
// adoption start
if ($this->layoutVariant !== NULL) {
$path = t3lib_div::fixWindowsFilePath(str_replace('@variant', $this->layoutVariant, $path));
} else {
$path = t3lib_div::fixWindowsFilePath(str_replace('.@variant', '', $path));
}
// removed upstream fallback to lowercase
$path = str_replace('@action', $actionName, $path);
if (file_exists($path)) {
return $path;
}
// adoption end
// *************
}
throw new Tx_Fluid_View_Exception_InvalidTemplateResourceException('Template could not be loaded. I tried "' . implode('", "', $paths) . '"', 1225709595);
}
/**
* wee need to apply the TypoScript-Setp again ourselfs
*/
public function initializeView() {
parent::initializeView();
$extbaseFrameworkConfiguration = Tx_Extbase_Dispatcher::getExtbaseFrameworkConfiguration();
if (isset($extbaseFrameworkConfiguration['view']['templateRootPath']) && strlen($extbaseFrameworkConfiguration['view']['templateRootPath']) > 0) {
$this->setTemplateRootPath(t3lib_div::getFileAbsFileName($extbaseFrameworkConfiguration['view']['templateRootPath']));
}
}
}
?>
@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