Skip to content

Instantly share code, notes, and snippets.

@arnekolja
Last active October 14, 2018 01:38
Show Gist options
  • Save arnekolja/211df6fd61ae2d121f1f to your computer and use it in GitHub Desktop.
Save arnekolja/211df6fd61ae2d121f1f to your computer and use it in GitHub Desktop.
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

Works like a charm. Thank you.

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