简体   繁体   English

在Phalcon视图中包含JavaScript

[英]Including JavaScript in Phalcon views

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. 但是x.js文件依赖于jquery.js所以它应该放在它之前。

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 您可以在顶部的app/views/index.phtml中定义以下内容

<?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 然后在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 come in the master view with the 这将使jQuery.jsmyother.js脚本在x.js之前加载,因为x.js将进入主视图

<?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 HTH

Not sure if it fits the OP's use case, but for others searching, this pattern is pretty useful: 不确定它是否适合OP的用例,但对于其他搜索,这种模式非常有用:

class MyController extends Phalcon\Mvc\Controller
{

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

    [...etc]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM