Skip to content

Instantly share code, notes, and snippets.

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 Norcoen/3b1f004dd99e73958c9ab44036dc6cf4 to your computer and use it in GitHub Desktop.
Save Norcoen/3b1f004dd99e73958c9ab44036dc6cf4 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;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment