Last active
April 26, 2018 09:12
-
-
Save lukaslabryszewski/68f91de16d70564a53a157c06574d3e6 to your computer and use it in GitHub Desktop.
Zend Framework 2: Render Template with Registered Helpers and template path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Zend\View\Model\ViewModel; | |
use Zend\View\Renderer\PhpRenderer; | |
/** | |
* Render and return the current template | |
* @param string $template Template Name | |
* @param array $variables Template Variables | |
* @return string HTML content | |
*/ | |
function renderTemplate($template, $variables = []) | |
{ | |
$viewRender = new PhpRenderer(); | |
$viewRender->resolver()->addPath('module/Application/view/'); | |
// Configure Helpers | |
$pluginManager = $viewRender->getHelperPluginManager(); | |
$helperConfig = new \Zend\Form\View\HelperConfig(); | |
$helperConfig->configureServiceManager($pluginManager); | |
// Add any custom helpers not included with HelperConfig | |
// $pluginManager->setInvokableClass('formselect', 'Zend\Form\View\Helper\FormSelect'); | |
$layout = new ViewModel(); | |
$layout->setTemplate($template); | |
$layout->setVariables($variables); | |
return $viewRender->render($layout); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment