Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Created October 20, 2016 17:45
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 bwaidelich/de03b02567da16c35bb6203cd48948b5 to your computer and use it in GitHub Desktop.
Save bwaidelich/de03b02567da16c35bb6203cd48948b5 to your computer and use it in GitHub Desktop.
Very simple Template View for Neos Flow
<?php
namespace Some\Package\View;
use TYPO3\Flow\Mvc\View\AbstractView;
use TYPO3\Flow\Reflection\ObjectAccess;
class SimpleTemplateView extends AbstractView
{
/**
* @var array
*/
protected $supportedOptions = [
'templateSource' => [null, 'Source of the template to render', 'string', true],
];
/**
* Renders the view
*
* @return string The rendered view
* @api
*/
public function render()
{
$source = $this->getOption('templateSource');
return preg_replace_callback('/\{([a-zA-Z0-9\-_.]+)\}/', function($matches) {
return ObjectAccess::getPropertyPath($this->variables, $matches[1]);
}, $source);
}
}
@bwaidelich
Copy link
Author

Usage:

$view = new SimpleTemplateView(['templateSource' => 'Some template with {simple} and {nested.name} placeholders.']);
$view->assignMultiple([
    'simple' => 'SIMPLE',
    'nested' => $someObject
]);
echo $view->render();

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