从 CakePHP 中的模板将代码块添加到 body 的末尾和内部的 head

add code blocks to end of body and inside head from template in CakePHP

我使用过 Django 并且在 Django 模板引擎中我们定义了像这样的块

<head>
    {% block head %} {% endblock head %}
</head>
<body>
    {% block content %} {% endblock content %}
    .....
    {% block script %} {% endblock script %}
</body>
</html>

在扩展此布局的每个模板文件中,我们在特定块中包含代码以分别放入 headbodyscript

现在,我正在使用 CakePHP 并希望实现相同的效果,因为我在正文标记末尾有很多 js 文件,并且在它们下面包括 script,这是专门针对每个页面的。也就是说,该脚本只会从特定页面调用,因此我不能将它放在所有页面上。

CakePHP 的布局看起来像

<html>
<head>
    <!-- all head elements -->
</head>
<body>
    <?= $this->fetch('content') ?>

    <!-- all js includes -->
</body>
</html>

如何在 CakePHP 3 的 body 标签末尾添加脚本?

说明书上都有说明here

在您的视图文件中

<?php
    $this->Html->script('yourjavascriptfile', ['block' => 'scriptBottom']);
?>

然后在您的布局中

<body>
    <?= $this->fetch('content') ?>
    <?= $this->fetch('scriptBottom') ?>
</body>