也许我错过了一些东西 - 但是为什么你不能加载另一个只包含 js 代码的视图?
您的模板:
$this->load->view("header.php");
$this->load->view("content.php", $data);
$this->load->view("footer.php", $load_js);
然后在footer.php中:
// start of footer stuff here
$this->load->view($load_js);
</body>
</html>
然后在 page1.php 中:
<script>
// Your scripts here
</script>
或者:
您的模板:
$this->load->view("header.php");
$this->load->view("content.php", $data);
然后在每个“content.php”文件中:
// Content goes here
$data['load_js'] = "page1.php";
$this->load->view("footer.php", $data);
然后在单个“footer.php”文件中:
// start of footer stuff here
$this->load->view($load_js);
</body>
</html>
然后在 page1.php 中:
<script>
// Your scripts here
</script>
第二种方法可能更符合您的要求。这确实意味着您需要在每个内容文件中调用页脚 - 但它实际上是一行 - 所以您并没有真正重复自己 - 但它使您可以完全控制内容文件内部,特别是要加载的任何 js 文件。
您可以扩展它并使 $load_js 变量成为要加载的 js 文件数组。