3

I'm using CakePHP 3.3.10. I need to add a JavaScript file to a specific view.

// default.ctp  
// I need to remove this script in default.ctp and load only in a specific view.  

<?= $this->Html->script(['otherfile.js', 'otherfile.js', folder/scripts.js']) ?>  

How can I load the js file only in this view?:

// index.ctp  
// I need to load the scripts.js file only in this view
3
  • stackoverflow.com/questions/37457293/… Commented Dec 14, 2016 at 17:24
  • @ndm I've tried it but it's not loading correctly. I need to load it after all the scripts in default.ctp. Commented Dec 14, 2016 at 17:30
  • 2
    Then position the block accordingly, and/or create a new, custom one. And if you tried something, please show what you've tried and describe what exactly the problem with it is. Also don't just do 5 minutes of trial & error, read the answer, read the docs (2), then implement it. Commented Dec 14, 2016 at 18:19

5 Answers 5

10
<?php echo $this->Html->css('style.css',['block'=>true]); ?>

The block = true option causes the css to load inside the head tag. It will be loaded where you put the code <?php echo $ this-> fetch ('css'); ?> in your template

Sign up to request clarification or add additional context in comments.

Comments

5

Go to your index.ctp file and insert this code at the bottom.

For JS

echo $this->Html->script('/otherdir/scripts');

or

echo $this->Html->script('http://code.jquery.com/jquery.min.js');

Will output:

<script src="http://code.jquery.com/jquery.min.js"></script>

The first parameter can be an array to include multiple files.

echo $this->Html->script(['jquery', 'wysiwyg', 'scripts']);

Will output:

<script src="/js/jquery.js"></script>
<script src="/js/wysiwyg.js"></script>
<script src="/js/scripts.js"></script>

You can append the script tag to a specific block using the block option:

echo $this->Html->script('wysiwyg', ['block' => 'scriptBottom']);

Then, in your layout, make sure to have the said block so they get outputted:

<?= $this->fetch('scriptBottom')?>

For CSS

This method of CSS inclusion assumes that the CSS file specified resides inside the webroot/css directory if path doesn’t start with a ‘/’.

echo $this->Html->css('forms');

Will output:

<link rel="stylesheet" href="/css/forms.css" />

The first parameter can be an array to include multiple files.

echo $this->Html->css(['forms', 'tables', 'menu']);

Will output:

<link rel="stylesheet" href="/css/forms.css" />
<link rel="stylesheet" href="/css/tables.css" />
<link rel="stylesheet" href="/css/menu.css" />

1 Comment

In my case, output new line not work? like following <link rel="stylesheet" href="/css/forms.css" /><link rel="stylesheet" href="/css/tables.css" /><link rel="stylesheet" href="/css/menu.css" /> How can i solve this?
0

Use $this->request->params['controller'] to get the current controller and $this->request->params['action'] for controller action.

Example:

<?php 
  // default.ctp 
   echo $this->Html->script(['otherfile.js', 'otherfile.js']);
   if (in_array($this->request->params['action'], ['index'])){
    // your conditional script block
    echo $this->Html->script(['folder/scripts.js']);
  }

?>

Hope it should help you to solve your problem.

Comments

0

Add below code in your view :

<?php echo $this->Html->script('/otherdir/scripts'); ?>

Comments

0

Add css and js file on the top of view file:

echo $this->Html->script(array('ajaxupload.3.5.js'));

echo $this->Html>css(array('new_layout.css'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.