CakePHP - exclude css based on specific pages - cakephp

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...

Related

Metadata/Elements in Views and Layouts

I'm using CakePHP 3.x. I have just installed the source files and the first page that comes up uses the default.ctp layout which calls the home.ctp view. My understanding at the moment is that the view gets placed here in the layout,
<?= $this->fetch('content') ?>
My question is, how come in both the view and the layout some elements and metadata are duplicated. So for instance both call
<?= $this->Html->charset() ?>
<?= $this->Html->meta('icon') ?>
and both duplicate the html structure by using <html>, <head> and <body> tags.
To summarise, if i view the code of the webpage, lots of the structure is duplicated.
Thanks
home.ctp on fresh installed cakephp is just example.
opet that file and see line 22
$this->layout = false;
if (!Configure::read('debug')):
throw new NotFoundException('Please replace src/Template/Pages/home.ctp with your own version.');
endif;
As we see this home.ctp not use layout .ctp, simple clear all code from that file, and add for example:
<h1>This is my new landing page</h1>
save, reload, now your home.ctp use default.ctp layout with meta tags etc..

CakePHP 3.x - Load CSS in head tag for a specific action

So far I can only load a specific CSS file for a specific action by include that CSS file within <body> tag from the corresponding view file. But I want to include that CSS file within <head> tag when a specific action is called?
In your template, this can be achieved by linking to the css file using the block option:
Creates a link(s) to a CSS style-sheet. If the block option is set to true, the link tags are added to the css block which you can print inside the head tag of the document.
I.e. anywhere in the relevant template:
$this->Html->css('special', ['block' => true]);
This is output by the fetch call in the layout:
<?= $this->fetch('css') ?>

How to add a link rel code in Drupal 7?

I am a new developer and I want to implement a popup screen in Drupal 7.
I found a code online and it works, except for the "link rel" code the example has at the <head> section.
The code is the following:
<link rel="stylesheet" href="http://www.jacklmoore.com/colorbox/example1/colorbox.css" />
I tried opening this link and adding the whole code at the styles.css file, but the popup appears without any style.
Is there a way to do it correctly?
If you added the CSS content to your CSS file maybe you should clear the CSS cache to see changes.
You also should check the selectors match to the CSS rules (for instance with Firebug in Firefox browser).
Or in the template.php you can add external CSS in this way:
function mytheme_preprocess_html(&$variables) {
`drupal_add_css('http://www.jacklmoore.com/colorbox/example1/colorbox.css',`
`array('type' => 'external'));`
}

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 linking css files and javascript files

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.

Resources