Setting a twig block from a route

Is it possible to set the content of a block of a twig template directly from php inside a slim route instead of extending the template ?

$app->get('/myRoute', function (Request $request, Response $response) {
    $twig = Twig::fromRequest($request);
    $env = $twig->getEnvironment();
    $template = $env->load('layout/standard.twig');

    // Do something like this:
    // $template->setBlock('javascript', '<script src="myAdditionalScript.js">);

    $body = $template->render([ 'lang' => 'en', 'title' => 'myPageTitle']);
    $response->getBody()->write($body);
    
return $response;
});

A simple twig template base.twig

<!DOCTYPE html>
<html lang="{{ lang }}" dir="ltr">
<head>
    <title>{{ title }}</title>
    <script defer src="/scripts/js/default.js"></script>
    {% block javascript %}{% endblock %}
</head>
<body>
</body>
</html>

Just found a (good?) solution:

    $template = $env->createTemplate('{% extends "layout/standard.twig" %}
      {% block javascript %}
        <script src="/js/photo.js" type="module"></script>
      {% endblock %}
    ');