views:

29

answers:

3

I'm confused by this as I can't find good documentation on how to do this, I have a jquery file called "index" located in my cakephp site folder at:

/app/webroot/js/index.js

I am trying to include it in my view with:

<?php
    echo $javascript->link('index', false);
?>

but I get:

Fatal error: Call to a member function link() on a non-object in /var/www/site1/app/views/uiemails/index.ctp on line 4 

I have also put this in my layout's header:

<?php
    //load the jQuery core
    $javascript->link('jquery-1.4.2.min', false);
?>

I would appreciate any advice as to what I need to do to get this to work.. thanks

Edit

(I am using cakephp version 1.3.2 I have now tried:

putting this in my controller:

var $helpers = array('Form', 'Html');

then, in my view:

<?php
echo $this->html->script('index', false);
?>

but I still get:

Fatal error: Call to a member function script() on a non-object in /var/www/site1/app/views/uiemails/index.ctp on line 4 
+1  A: 

It looks like you're forgetting to include the Javascript helper in your controller. The syntax also depends on which version of CakePHP you're using. In 1.3.x, the Javascript helper has been deprecated and you would use the Html helper instead.

Your controller:

<?php
class BakeriesController extends AppController {
    // not required if you're using the Html helper since it's included by default!
    var $helpers = array('Form', 'Html', 'Javascript');
}
?>

Your view:

<?php echo $this->Javascript->link('whatever'); ?>

or with CakePHP 1.3.x:

<?php echo $this->Html->script('whatever'); ?>

And in your filesystem:

/app/webroot/js/whatever.js

The relevant documentation:

erjiang
thanks, I will try that
Rick
Hi, I tried what you said, but am still getting an error, I have version 1.3.2.. please see my edit above for what I tried.. thanks
Rick
A: 

Well, it seems that it works when I do it this way:

<?php
//load javascript file that will be specific to this view (page)

echo $html->script('index');
?>

I was confused (using $this->html->script) since other tutorials I found said to do this but apparently its incorrect, at least in the latest version of cakephp.

Rick
A: 

This is not right:

 echo $this->html->script('index', false);

It should be:

 echo $html->script('index', false);

You should also be careful not to include any javascript twice (e.g. in a layout and in a view).

Leo