ale
ale

Reputation: 11800

Including JS in default View in Cake PHP

I can do this in any of my View file in Cake PHP:

<?php echo $this->Html->script('myjs.js', false); ?>

but if I do the same thing in my default view (default.ctp) then the JS files don't load i.e. they don't get echoed. I have tried moving includes above and below <?php echo $scripts_for_layout ?> in the default view but they still don't get printed:

<?php echo $scripts_for_layout ?>  
<?php echo $this->Html->script('myjs.js'); ?>

<?php echo $this->Html->script('myjs.js'); ?>
<?php echo $scripts_for_layout ?>  

Any ideas?

Thank you :).

Upvotes: 0

Views: 5542

Answers (4)

user1334546
user1334546

Reputation:

Another thing I'd DEFINITELY recommend is to check that loading your JS in a view is 100% OK with everything.

Do NOT use $this->Html->script if you are unsure about:

--- the dependency of scripts on each other

--- the dependency of some plugins on other scripts

--- how many different layouts and/or views you want to use in the future.

For example, some plugins use jQuery but they require jQuery to be loaded BEFORE the plugin itself.

So if you put jQuery in a view or layout, the plugin's JS loads before jQuery and plugin may not work. If you want to make sure some script loads first, you can try this:

in your app/View/Helper/AppHelper.php:

class AppHelper extends Helper {
    public $helpers = array('Html', 'Session');

    public function beforeRender($viewFile){
            echo $this->Html->script('jquery', array('inline'=>false));
        }
}

Therefore you don't have to include jQuery in multiple views and it loads first.

Upvotes: 1

Irishka
Irishka

Reputation: 1136

with or without an extension - this is not the reason why the script do not get echoed,

as mensch mentioned before it makes no difference because of:

source of html helper cakephp 2

if (strpos($url, '?') === false && substr($url, -3) !== '.js') {
  $url .= '.js';
}

the reason is: you have already inserted this script inside your view,

cake checks if the script is already inserted on the page

options - 'once' - Whether or not the script should be checked for uniqueness. If true scripts will only be included once, use false to allow the same script to be included more than once per request.

by default the value of once is set to TRUE

remove the script from your view first and then try it with or without '.js'

P.S.: why the petervaz answer has worked for you: because this check:

if ($options['once'] && isset($this->__includedScripts[$url])) {
    return null;
}

made before check for file extension

so isset(__includedScripts['myjs']) == false // because first key was __includedScripts['myjs.js']

and you've got your script included

Upvotes: 3

mensch
mensch

Reputation: 4411

$scripts_for_layout in a layout is a placeholder for all the scripts included in views, so if you include it or don't won't have any effect if you load scripts directly in the default.ctp.

That being said, adding the extension or leaving it out of the $this->Html->script() call shouldn't make a difference. What could be the case is that the HtmlHelper isn't called correctly (it's called by default in Cake 2.0), but you should be receiving error messages. Is debug set to 2 in /app/Config/core.php?

Upvotes: 0

petervaz
petervaz

Reputation: 14205

I have a project with scripts added just before $scripts_for_layout and they are displayed fine. The only difference is that I'm not adding the extension .js to the call, like this:

default.ctp:

<head>
    <?php echo $this->Html->charset(); ?>
    <title>
        <?php echo "Site > $title_for_layout"; ?>
    </title>
<?php    
  echo $this->Html->script('jquery-1.6.4');
  echo $this->Html->script('jquery-ui-1.8.16.custom.min');
  echo $this->Html->script('mercado');
  echo $scripts_for_layout;
?>
</head>

Upvotes: 1

Related Questions