Skip to content

Instantly share code, notes, and snippets.

Created September 28, 2012 13:11
Show Gist options
  • Save anonymous/3799749 to your computer and use it in GitHub Desktop.
Save anonymous/3799749 to your computer and use it in GitHub Desktop.
Flow3 email rendering service
use TYPO3\FLOW3\Annotations as FLOW3;
/**
* A email rendering service
*
* @FLOW3\Scope("singleton")
*/
class Email {
/**
* @var \TYPO3\FLOW3\Mvc\Controller\ControllerContext
*/
protected $controllerContext;
/**
* @var array
*/
protected $settings;
/**
* @param array $settings
* @return void
*/
public function injectSettings(array $settings) {
$this->settings = $settings;
}
/**
* @param \TYPO3\FLOW3\Mvc\Controller\ControllerContext $controllerContext
* @return void
*/
public function setControllerContext(\TYPO3\FLOW3\Mvc\Controller\ControllerContext $controllerContext) {
$this->controllerContext = $controllerContext;
}
/**
* This getter is necessary to do proper unit testing
* @return \TYPO3\Fluid\View\StandaloneView
*/
protected function getView() {
return new \TYPO3\Fluid\View\StandaloneView();
}
/**
* Renders a Fluid template and returns the rendered string
*
* @param string $template The name of the template to render
* @param array $templateVariables The variables that are used within the template
* @return string
*/
protected function render($template, $templateVariables = array()) {
$view = $this->getView();
if ($this->controllerContext === NULL) {
$view->initializeObject();
} else {
$view->setControllerContext($this->controllerContext);
}
if (!isset($this->settings['email']['layoutRootPath'])) {
$layoutRootPath = 'resource://' . $this->controllerContext->getRequest()->getControllerPackageKey() . '/Private/Layouts';
} else {
$layoutRootPath = 'resource://' . $this->settings['email']['layoutRootPath'];
}
$templatePath = NULL;
if (!isset($this->settings['email']['templateRootPath'])) {
if ($this->controllerContext !== NULL) {
$templatePath = 'resource://' . $this->controllerContext->getRequest()->getControllerPackageKey() . '/Private/Templates/Email';
}
} else {
$templatePath = 'resource://' . trim($this->settings['email']['templateRootPath'], '/');
}
$templatePathAndFilename = $templatePath . '/' . $template . '.html';
$view->setLayoutRootPath($layoutRootPath);
$view->setTemplatePathAndFilename($templatePathAndFilename);
$view->assignMultiple($templateVariables);
$content = $view->render();
return $content;
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment