admin管理员组

文章数量:1401208

Consider following two files:

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

About will generated like:

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

But x.js file is depended on jquery.js so it should placed before it.

Consider following two files:

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

About will generated like:

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

But x.js file is depended on jquery.js so it should placed before it.

Share Improve this question edited May 16, 2017 at 14:42 Doc Roms 3,3081 gold badge22 silver badges38 bronze badges asked Oct 21, 2012 at 18:32 Real DreamsReal Dreams 18.1k25 gold badges103 silver badges182 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Suppose you have the following structure:

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

You can define the following in the app/views/index.phtml at the top

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

and then in the app/views/about/index.phtml

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

That would get the jQuery.js and myother.js scripts to load before the x.js does, since the x.js will e in the master view with the

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

Alternatively, you could set this in your master view:

<?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"); } ?>

and in your About controller

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

HTH

Not sure if it fits the OP's use case, but for others searching, this pattern is pretty useful:

class MyController extends Phalcon\Mvc\Controller
{

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

    [...etc]

docs: http://docs.phalconphp./en/latest/reference/assets.html

本文标签: phpIncluding JavaScript in Phalcon viewsStack Overflow