在Phalcon视图中包含JavaScript

时间:2012-10-21 18:32:13

标签: php javascript phalcon

请考虑以下两个文件:

// view/index.phtml
 echo \Phalcon\Tag::javascriptInclude("javascript/jquery.js"); 
// view/about/about.phtml
 echo \Phalcon\Tag::javascriptInclude("javascript/x.js");

关于生成如下:

<script src="javascript/x.js">
<script src="javascript/jquery.js">

x.js文件取决于jquery.js,因此它应放在它之前。

2 个答案:

答案 0 :(得分:4)

假设您具有以下结构:

app/views/index.phtml
app/views/about/index.phtml

您可以在顶部的app/views/index.phtml

中定义以下内容
<?php echo \Phalcon\Tag::javascriptInclude("javascript/jQuery.js"); ?>
<?php echo \Phalcon\Tag::javascriptInclude("javascript/myother.js"); ?>

然后在app/views/about/index.phtml

<?php echo \Phalcon\Tag::javascriptInclude("javascript/x.js"); ?>

这会使jQuery.jsmyother.js脚本在x.js之前加载,因为x.js将出现在主视图中

<?php echo $this->getContent() ?>

或者,您可以在主视图中设置:

<?php echo \Phalcon\Tag::javascriptInclude("javascript/jQuery.js"); ?>
<?php echo \Phalcon\Tag::javascriptInclude("javascript/myother.js"); ?>
<?php if ($is_about) { echo \Phalcon\Tag::javascriptInclude("javascript/myother.js"); } ?>

并在你的关于控制器

$this->view->setVar('is_about', TRUE);

HTH

答案 1 :(得分:2)

不确定它是否适合OP的使用案例,但对于其他搜索,这种模式非常有用:

class MyController extends Phalcon\Mvc\Controller
{

    public function initialize()
    {
        $this->assets->addJs("/path/to/myjs.js");

    [...etc]

文档: http://docs.phalconphp.com/en/latest/reference/assets.html