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.

index.php

error_reporting(E_ALL);
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();
	$phpRenderer->setResolver($resolver);
	$plugin = $phpRenderer->getHelperPluginManager();

	$layout = new ViewModel;
	$layout->setTemplate('layout.phtml');

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

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

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

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

	$header = new ViewModel;
	$header->setTemplate('header.phtml');

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

	echo $view->render($layout);

} catch(Exception $e) {
	var_dump($e);
}

index.phtml

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

layout.phtml

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

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

<html>
<head>
    <meta charset="utf-8"/>
</head>
<body>
<h1>Hello, John Doe</h1>
<script type="text&#x2F;javascript">
    //<!--
    alert('ok')
    //-->
</script></body>
</html>

(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();
$phpRenderer->setResolver($templateResolver);

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

// Attach event listener to add the renderer
$view->getEventManager()->attach(
    Zend\View\ViewEvent::EVENT_RENDERER,
    static function () use ($phpRenderer) {
        return $phpRenderer;
    }
);

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

// Create layout model
$layoutModel = new Zend\View\Model\ViewModel;
$layoutModel->setTemplate('layout');

// Add view model as child to layout
$layoutModel->addChild($viewModel);

// 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.

	<html>
<head>
    <meta charset="utf-8"/>
</head>
<body>

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

</body>
</html>

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

<html>
<head>
    <meta charset="utf-8"/>
</head>
<body>

<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: