Last active
October 14, 2018 01:38
-
-
Save arnekolja/211df6fd61ae2d121f1f to your computer and use it in GitHub Desktop.
TYPO3 Fluid ViewHelper to check if a partial name exists ("My/Partial")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Krbu\Utility\ViewHelpers; | |
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; | |
/** | |
* Copyright note: Some parts are copied from the Fluid package. | |
* Usage example: | |
* <f:if condition="{krbu:PartialExists(partial: 'Category/{item.category.id}/DetailCol1')}"> | |
* <f:then><f:render partial="Category/{item.category.id}/DetailCol1" arguments="{_all}" /></f:then> | |
* <f:else><f:render partial="Category/Default/DetailCol1" arguments="{_all}" /></f:else> | |
* </f:if> | |
*/ | |
class PartialExistsViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper { | |
/** | |
* Pattern to be resolved for "@partialRoot" in the other patterns. | |
* Following placeholders are supported: | |
* - "@packageResourcesPath" | |
* | |
* @var string | |
*/ | |
protected $partialRootPathPattern = '@packageResourcesPath/Private/Partials'; | |
/** | |
* Does the given partial exist? | |
* | |
* @author Arne-Kolja Bachstein | |
* @param string $partial | |
* | |
* @return boolean | |
*/ | |
public function render($partial) { | |
$actionRequest = $this->controllerContext->getRequest(); | |
$possibilities = array(str_replace( | |
'@packageResourcesPath', | |
ExtensionManagementUtility::extPath($actionRequest->getControllerExtensionKey()) . 'Resources/', $this->partialRootPathPattern) | |
); | |
foreach($possibilities as $p) { | |
if (file_exists($p . "/".$partial.".html")) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works like a charm. Thank you.