Keith Power
Keith Power

Reputation: 14141

cakephp how to add script to view

I wish to add some script to a view and have it que and load at the bottom of the page within the default layout. At the moment it runs before query is loaded and causes an error.

I am using cakephp 2.5

Is there a way to buffer it?

myview.ctp:

<script type="text/javascript">
  FWDEVPUtils.onReady(function(){

  FWDEVPlayer.useYoutube = "yes";
  FWDEVPlayer.videoStartBehaviour = "pause";

  new FWDEVPlayer({   
    //main settings
    instanceName:"player",
    parentId:"player",
    mainFolderPath:"content"
  });
});

default.ctp

echo $this->Html->script('//code.jquery.com/jquery-1.10.2.min.js');
echo $this->Html->script('bootstrap.min');
echo $this->Html->script('FWDEVPlayer');

echo $this->fetch('script');
echo $this->Js->writeBuffer();

Upvotes: 0

Views: 1776

Answers (1)

PGBI
PGBI

Reputation: 710

In myview.ctp:

<?php $this->start('script'); ?>
<script type="text/javascript">
  FWDEVPUtils.onReady(function(){

  FWDEVPlayer.useYoutube = "yes";
  FWDEVPlayer.videoStartBehaviour = "pause";

  new FWDEVPlayer({   
    //main settings
    instanceName:"player",
    parentId:"player",
    mainFolderPath:"content"
  });
});
<?php $this->end(); ?>

You script will then be included in your layout default.ctp where you wrote: echo $this->fetch('script');

Upvotes: 2

Related Questions