How to use inline script head tag in Zend\View component without full MVC stack

$this->inlineScript() is not work in my layout file.

Here is the full code.


ini_set('display_erros', 1);

include 'vendor/autoload.php';

use Zend\View\Model\ViewModel;
use Zend\View\Renderer\PhpRenderer;
use Zend\View\Resolver\TemplatePathStack;
use Zend\View\Resolver;
use Zend\View\View;
use Zend\View\HelperPluginManager;
use Zend\View\ViewEvent;

try {

	$resolver = new TemplatePathStack(array(
	    'script_paths' => [__DIR__.'/view'],
	$phpRenderer = new PhpRenderer();
	$plugin = $phpRenderer->getHelperPluginManager();

	$layout = new ViewModel;

	$callable = $plugin->get('viewModel');

	// $phpRenderer->setHelperPluginManager(new HelperPluginManager);  // Custom plugin manager

	$view = new View();
	    ->attach(ViewEvent::EVENT_RENDERER, function () use ($phpRenderer) {
	        return $phpRenderer;

	$viewModel = new ViewModel(array('userName' => 'John Doe'));
	$viewModel->setOption('has_parent', true);

	$header = new ViewModel;

	$layout->addChild($header, 'header');
	$layout->setOption('has_parent', true);

	echo $view->render($layout);

} catch(Exception $e) {


<h1>Hello, <?php echo $this->userName ?></h1>
	echo "alert('ok')";


    <meta charset="utf-8"/>
<?php echo $this->header ?>
<?php echo $this->content ?>
<?php echo $this->inlineScript() ?>

If I copy your entire code, add the missing header.phtml file and run the script then I get the following output:

    <meta charset="utf-8"/>
<h1>Hello, John Doe</h1>
<script type="text&#x2F;javascript">

(with version 2.11.3 of zend-view)

Btw. you do not need to call the setRoot method of the view helper ViewModel.

// Create template resolver
$templateResolver = new Zend\View\Resolver\TemplatePathStack(
        'script_paths' => [__DIR__ . '/view'],

// Create renderer
$phpRenderer = new Zend\View\Renderer\PhpRenderer();

// Initialize view
$view = new Zend\View\View();

// Attach event listener to add the renderer
    static function () use ($phpRenderer) {
        return $phpRenderer;

// Create view model
$viewModel = new Zend\View\Model\ViewModel(['userName' => 'John Doe']);

// Create layout model
$layoutModel = new Zend\View\Model\ViewModel;

// Add view model as child to layout

// Setting for direct output (is needed if no response has been added)
$layoutModel->setOption('has_parent', true);

// Render
echo $view->render($layoutModel);

(I will also add this to the documentation.)

Still i am in same situation.

    <meta charset="utf-8"/>

<?php // echo $this->header ?>
<?php echo $this->content ?>
<?php echo $this->inlineScript() ?>


When i print the inline script i get a broken page like this.

    <meta charset="utf-8"/>

<h1>Hello, John Doe</h1>

My php version is: PHP 7.2.19-0ubuntu0.18.04.1 (cli) (built: Jun 4 2019 14:48:12) ( NTS )
My composer json is like below:

"require": {
    "zendframework/zend-view": "^2.11",
    "zendframework/zend-servicemanager": "^3.3"

About the setRoot method, i need it because when i want to change the default template like this,

	<?php $this->layout('test.phtml') ?>

	<h1>Hello, <?php echo $this->userName ?></h1>

i get an exception: Zend\View\Helper\Layout::getRoot: no view model currently registered as root in renderer.

By the way thanks for the support :slight_smile: