Skip to content

Instantly share code, notes, and snippets.

@khuppenbauer
Last active August 29, 2015 14:01
Show Gist options
  • Save khuppenbauer/bcdbf0050166861a295d to your computer and use it in GitHub Desktop.
Save khuppenbauer/bcdbf0050166861a295d to your computer and use it in GitHub Desktop.
Render a partial from another Package in TYPO3 Flow / Fluid
<?php
namespace Your\Package\ViewHelpers;
/* *
* This script belongs to the TYPO3 Flow package "TYPO3.Fluid". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use TYPO3\Flow\Mvc\Exception;
use TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* ViewHelper that renders a section or a specified partial
*
* == Examples ==
*
* <code title="Passing all variables to a partial">
* <f:render partial="somePartial" arguments="{_all}" package="Your.Package" />
* </code>
* <output>
* the content of the partial "somePartial".
* Using the reserved keyword "_all", all available variables will be passed along to the partial
* </output>
*
* @api
*/
class RenderViewHelper extends AbstractViewHelper {
/**
* Renders the content.
*
* @param string $section Name of section to render. If used in a layout, renders a section of the main content file. If used inside a standard template, renders a section of the same file.
* @param string $partial Reference to a partial.
* @param array $arguments Arguments to pass to the partial.
* @param boolean $optional Set to TRUE, to ignore unknown sections, so the definition of a section inside a template can be optional for a layout
* @param string $package Package Key from which the partial should be rendered
* @return string
* @api
*/
public function render($section = NULL, $partial = NULL, $arguments = array(), $optional = FALSE, $package = NULL) {
$arguments = $this->loadSettingsIntoArguments($arguments);
if ($partial !== NULL) {
if ($package !== NULL) {
try {
$partialRootpaths = $this->viewHelperVariableContainer->getView()->getOption('partialRootPaths');
$this->viewHelperVariableContainer->getView()->setOption('partialRootPaths', array('resource://' . $package . '/Private/Partials'));
$content = $this->viewHelperVariableContainer->getView()->renderPartial($partial, $section, $arguments);
$this->viewHelperVariableContainer->getView()->setOption('partialRootPaths', $partialRootpaths);
return $content;
} catch(Exception $exception) {
return $this->viewHelperVariableContainer->getView()->renderPartial($partial, $section, $arguments);
}
}
} elseif ($section !== NULL) {
return $this->viewHelperVariableContainer->getView()->renderSection($section, $arguments, $optional);
}
return '';
}
/**
* If $arguments['settings'] is not set, it is loaded from the TemplateVariableContainer (if it is available there).
*
* @param array $arguments
* @return array
*/
protected function loadSettingsIntoArguments($arguments) {
if (!isset($arguments['settings']) && $this->templateVariableContainer->exists('settings')) {
$arguments['settings'] = $this->templateVariableContainer->get('settings');
}
return $arguments;
}
}
{namespace yourPackage=Your\Package\ViewHelpers}
<yourPackage:render partial="partialName" package="Your.PackageWithThePartial" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment