我遇到了一些问题。从视图上看,appendFile似乎无法工作。如果我将其更改为prependFile,它将具有相同的行为。
layout.phtml
<!doctype html>
<html lang="en" dir="ltr">
<head>
<?php
$this->headScript()->appendFile('http://code.jquery.com/ui/1.10.3/jquery-ui.js');
$this->headScript()->appendFile('/theme/javascripts/application.js');
$this->headScript()->appendFile('/js/own.js');
?>
</head>
<body>
<?php echo $this->layout()->content; ?>
<?php echo $this->headScript() ?>
</body>
</html>index.phtml
<?php $this->headScript()->appendFile('/js/another.js') ?>输出
<!doctype html>
<html lang="en" dir="ltr">
<head>
</head>
<body>
<script type="text/javascript" src="/js/another.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript" src="/theme/javascripts/application.js"></script>
<script type="text/javascript" src="/js/own.js"></script>
</body>
</html>如您所见,/ js /另一个.js将是第一个js。这不是我想要的,我想把它放在最后。有人知道哪里出问题了吗?
发布于 2013-08-10 21:50:56
/js/another.js位于开头,因为layout.phtml首先被调用,然后当您调用<?php echo $this->layout()->content; ?>时,它将调用index.phtml文件。
现在,在您的代码中,您已经:
<?php echo $this->layout()->content; ?>
<?php echo $this->headScript() ?>
所以它实际上调用了layout(),然后调用了index.phtml,然后调用了headscript(),它包含了其余的元素。它附加了index.phtml中提到的js,然后附加了headscript()中的其余部分。换一种方式试试...
您应该尝试:
<!doctype html>
<html lang="en" dir="ltr">
<head>
<?php
$this->headScript()->appendFile('http://code.jquery.com/ui/1.10.3/jquery-ui.js');
$this->headScript()->appendFile('/theme/javascripts/application.js');
$this->headScript()->appendFile('/js/own.js');
?>
</head>
<body>
<?php echo $this->headScript() ?>
<?php echo $this->layout()->content; ?>
</body>
</html>理想情况下,您应该在<HEAD>部分中输出所有脚本。
发布于 2013-08-11 00:36:37
视图脚本在布局之前呈现,因此当您附加/js/another.js时,没有其他脚本,这就是为什么稍后由布局添加的脚本在布局之后结束。
通过将布局中的所有appendFile()调用更改为prependFile(),您应该能够实现所需的顺序。(您可能需要颠倒它们的顺序,因为您现在是前置的。)然后,执行顺序应为:
前缀/js/another.js
/js/own.js
/theme/javascripts/application.js
http://code.jquery.com/ui/1.10.3/jquery-ui.js前缀
此外,您可能希望考虑使用内联脚本帮助器(以相同的方式工作),因为让head脚本在<body>中输出脚本可能会使未来处理代码的开发人员感到困惑。
https://stackoverflow.com/questions/18162159
复制相似问题