blairmeister
blairmeister

Reputation: 915

CakePHP script function in default.ctp

How would I put this in my cakephp default.ctp file?

Im pretty novice at cakephp as ive just started using it.

     <script>
        $(function()
        {
          $('#slider-id').codaSlider();
        });
    </script>

Thanks, in advance.

Upvotes: 0

Views: 886

Answers (3)

Reactgular
Reactgular

Reputation: 54741

In CakePHP 2.x there are now things called blocks, and blocks are just chunks of output stored in memory until they are fetched for output.

There use to be a $scripts_for_layout variable that you could put your script into, but now we use the $this->fetch('scripts'); to get any JavaScript needed for the Html.

There are a few ways to inject JavaScript into the scripts block using the HtmlHelper.

To add it to the block, and this can be done in Views or Layouts. Just run this code before you fetch the block.

$this->Html->scriptBlock("$('#slider-id').codaSlider();",array('inline'=>false));

To output the scripts in your layout is easy.

$this->fetch('scripts');

The advantage of this approach is you can add JavaScript from multiple places in CakePHP, but they will be outputted in the layout at the location you desire.

Upvotes: 0

Dave
Dave

Reputation: 29121

  1. open the app/View/Layout/default.ctp file in your favorite code editor
  2. add the following between your <head> and </head>:

     <script>
        $(function()
        {
          $('#slider-id').codaSlider();
        });
     </script></li>
    

Upvotes: 0

thaJeztah
thaJeztah

Reputation: 28987

Although your question is too vague to answer in its current state, you should have a look at the JsHelper,especially Js->buffer(). This allows you to append script in your views and output them all at once in your layout.

http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#working-with-buffered-scripts

Upvotes: 2

Related Questions