CakePHP linking css files and javascript files - cakephp

How do you link css files and javascript/jquery files to a controller/view??
I am using CakePHP_1.3 and have located the following code online, but I cant seem to figure out where to put this, and secondly, where to place the css file named css_file.
<?php $this->Html->css("css_file", null, array("inline"=>false)); ?>
Any help appreciated guys...

You would put the CSS file in:
app/
webroot/
css/ <-- here
You would put the line of code in your default.ctp layout. This is located in (1.3):
cake/
libs/
view/
layouts/
default.ctp <-- copy this to app/views/layouts/
Then open that up app/views/layouts/default.ctp and look at the HTML. You'll see where Cake is already using this command. Simply replace the filename with the file you added. Do not add the .css to the end when you add the line of code.

you use echo $this->Html->css('css_file_name_without_the_extension'); and echo $this->Html->script('same') in the view

Based on your question, it sounds like you were asking how to do this on a view-by-view basis.
This is where $scripts_for_layout comes in useful.
You just need to make sure that it's in your <head> tag in /app/views/layouts/default.ctp as <?php echo $scripts_for_layout; ?>
Then, you can literally add the code you included in your question into any of your views, at literally any point you like. Because you have 'inline' => false it won't actually appear at that position in the view:
<?php $this->Html->css("css_file", null, array("inline"=>false)); ?>
...and you'll find that css_file.css is automatically linked in your <head> whenever that particular view is loaded. This is a great way to only load specific CSS files on a per-view basis when they're needed, yet make sure they appear in the <head> tag where they should be.
This also works for JavaScript as follows:
<?php $this->Html->script("js_file", null, array("inline"=>false)); ?>
If you want to have one or more CSS files loaded in all views, just put a shortened version your code in the <head> of default.ctp as follows:
<?php echo $html->css(array('each','one','of','your','css','files'));
Or, again, for JavaScript:
<?php echo $html->script(array('each','one','of','your','js','files'));
Don't include the .css or .js extension in the array.

Related

CakePHP - exclude css based on specific pages

I have a default css file for all page,but just one page have different css not default.In this situation can I exclude css link by cakephp html helper ?
One way I can think of is you can add css links in your view file to the head using CakePHP's blocks feature. See http://book.cakephp.org/2.0/en/views.html#using-view-blocks
For example, in the layout, somewhere in the head have:
<head>
...
<?php echo $this->fetch('css'); ?>
...
</head>
This indicates where the css block will displayed. Then, anywhere in your view files,
$this->Html->css('style', null, array('block' => 'css'));
And this style sheet will appear in the head. This way, you can control which css is linked to for each individual action/view.
Another option, is to include in your layout some code like this:
<head>
...
<?php
if($defaultCss) {
...
} else {
...
}
?>
...
</head>
Then in your appController, add something like:
function beforeFilter() {
$this->set('defaultCss', true);
}
And in the action that's the exception:
$this->set('defaultCss', false);
I think you can't exclude a css, but you can use a different layout for that page...

where to load javascript in a view page Cakephp

i am working on a Cakephp 2.x .. right now i am loading my js and css files like this
View/Layout/default.ctp
<?php echo $this->fetch('css'); ?>
<?php echo $this->fetch('script'); ?>
<?php echo $this->fetch('scriptBottom');?>
and in each view file i am doing this because there are some pages i am loading different css and js files
for example
View/users/index.ctp
echo $this->Html->script('libs/modernizr.custom',array('inline' => false));
$this->Html->css('reset3860.css', null, array('inline' => false));
echo $this->Html->script('libs/jquery-1.8.2.min', array('block' => 'scriptBottom'));
now the first problem i am facing right now is when i have to write js at the bottom of view page for example if i am submitting a form through ajax .. i can't write at the bottom of the index page ... because if i write there the js will go at the middle or center of the page so now what i do is i go the default.ctp and write there..
so i want to write at the bottom of the index page so it can easily managable and i can see it well what i am doing
the second question is what is the best way to manage all this ... or is there a way that i can make one file of cs and js ... in which there is only the css and js files are loaded ..and then i include that file in my default . ctp page
You should place scriptBottom block at the bottom of your layout, after content block:
<body>
<?php
echo $this->fetch('script');
echo $this->fetch('content');
echo $this->fetch('scriptBottom');
?>
</body>
And then in your views (notice - NO echo):
$this->Html->script(
'libs/jquery-1.8.2.min',
array('block' => 'scriptBottom')
);
$this->Html->scriptBlock(
"alert('Boom!');",
array('block' => 'scriptBottom')
);
here is very good concept and also i have applied in one of my application, it is working very well
it compatible with CakePHP 2.1, and packaged it as a plugin. And also upgraded the CSS compression from CSSTidy to the more recent and better maintained CSS Min.
The plugin is quick and easy to install. The installation instructions are somewhat long - but that's just to provide clarity.
for detail view you can refer Detail link Cakephp.org also you can see the plugin document and how to use it in your appliation

Best way of use of ajax in cakephp?

I am using ajax in my cakephp application by using JS helper.so I do not required to write jquery code.
Js helper automatically add the code in my file.Following is the line by which JS helper write the code.
echo $this->Js->writeBuffer(array('cache'=>true));
When I set the true value of cache attribute,every time a new js file created in js folder and a new script is added in my code in following manner.
<script type="text/javascript" src="filename.js"></script>
But when I change the cache value to false,all the js code added in my file line by line.
Now my question is, which way is best way by which,page execution fast.
And my second question is that when I set true value of the cache, js file add only once, right now js helper add the js file again and again,when page is reload or refresh.
For the record this question could have been worded far better.
First question, execution speed: Saving a new file for every bit of JS probably isn't the way to go. Hopefully the code below should explain a better way and render your second question moot.
// In /app/View/Layouts/default.ctp
...
<head>
...
<?php echo $this->fetch('script'); ?>
...
</head>
<body>
...
<?php echo $this->Js->writeBuffer();
...
</body>
Once that's in place, you can add a script file to your views or layouts with
<?php echo $this->Html->script('scriptname', array('inline'=>false)); ?>
(note no .js extension)
Or add custom Js:
<?php
$customJs = "alert('Hi!');";
echo $this->Html->scriptBlock($customJs, array('inline'=>false));
?>
Hope that helps.

CakePHP Elements - how to put javascript?

I've just started using cakephp elements and they're awesome (I used to use include).
I have an element gallery and an element called comments (for a certain page) and for them I have some javascript code attached to both of them. Do you have any suggestions on how I could include that javascript code in the element? If I simply add it like that it will load javascript before loading the html after the element and I don't think it's very wise.
You could put the Javascript code directly into the element file, or put the Javascript code in into your webroot folder, <cake directory>/app/webroot/js/ and include the file in your layout by using the HTML helper:
echo $html->script("myCode");
If you're worried about the Javascript code executing before the page has completely loaded, then use window.onload or $(document).ready() if you're using JQuery.
If I understand you correctly, you want to have JS specific to a page loading in the header when you call a certain element, but that the JS could be different for each element. You also want the JS to be referenced at the beginning of your HTML document.
This is actually quite easy to do. Just make sure that you have <?php echo $scripts_for_layout; ?> in you <head> tag in the layout you are using.
Then, within the element, just do:
<?php $this->Html->script("js_file", array("inline"=>false)); ?>
js_file is the name of your JavaScript file in app/webroot/js/. In this case, the file would be called js_file.js but you must leave off the .js when referencing it as above.
It doesn't matter where abouts in the element file you put this because the "inline"=>false part ensures it won't actually appear at that stage in the code. Instead, it will appear in the <head> wherever you put <?php echo $scripts_for_layout; ?> in your layout.
In cakephp 3 instead of array('inline' => false) you should use array('block' => true) if anyone is looking for that answer like I was.

cakephp problem with frameset

i Have default.ctp.
default.ctp include top.html,center.html,down.html.
And then center.html include left.html, right.html.
I want put my $content_for_layout in the right.html,But I can't do it. if I change right.html to right.php It will tell me
Notice: Undefined variable: content_for_layout in /opt/lampp/htdocs/app/webroot/right.php on line 37. if I change right.html to right.ctp Then it doesn't identify the file.
all the pages I put them in app/webroot/
Could somebody tell me how to do it?
Each of your frames will be loaded through a separate HTTP request and need to pass through the entire Cake framework for their content to be rendered. Just putting $contents_for_layout in some random file will not do much. As such, your frames need to link to Cake URLs:
src="<?php echo $this->Html->url(array('controller' => 'foo', 'action' => 'bar')); ?>"
But really, you shouldn't use framesets in this day and age.

Resources