Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 217
Handsome Nerd Avatar asked Apr 22 '26 13:04

Handsome Nerd


1 Answers

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 come 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

like image 115
Nikolaos Dimopoulos Avatar answered Apr 25 '26 02:04

Nikolaos Dimopoulos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!