Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TYPO3 Fluid ViewHelper to check if a partial name exists ("My/Partial")
<?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;
}
}
?>
@X-Tender
Copy link

X-Tender commented Aug 29, 2018

Works like a charm. Thank you.

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