tags:

views:

96

answers:

4

Hi all, I am learning cakePHP, everything seems alright except that I am very confused of
how to make use of the default.ctp and what should be put inside the Elements folder.

Here is the default.ctp file that I have been using since my very first lesson on learning cakePHP:
(I copied from this URL http://book.cakephp.org/view/96/Layouts)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title><?php echo $title_for_layout?></title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<!-- Include external files and scripts here (See HTML helper for more info.) -->
<?php echo $scripts_for_layout ?>
</head>
<body>

<!-- If you'd like some sort of menu to 
show up on all of your views, include it here -->
<div id="header">
    <div id="menu">...</div>
</div>

<!-- Here's where I want my views to be displayed -->
<?php echo $content_for_layout ?>

<!-- Add a footer to each displayed page -->
<div id="footer">...</div>

</body>
</html>

But the problem is that the layout will take effect to all web pages that I have created.
Let's see the case that I have recently encountered.
In one of the .ctp files, I need to use JQuery function
and I need to ass some and tags in the .ctp file. Here are the and tags I used:

<Script language="javascript">
$(document).ready(function()
{
// some functions here
});
</Script>

<style type="text/css">
{
#toppage{
 width:800px;
}

But when I followed the default.ctp file,
I noticed that these tags (i.e. <Style> and <Script>) happened to appear below the tag.
As far as I know, the and self-defined Javascript functions
should be put inside the tag of the HTML instead.
I have considered to add the <Style> and <Script> in the default.ctp file,
but then these codes would appear in every web pages instead of just a particular web page.

Please help.

A: 

Put a page switch in the top of your layout and include the two files conditionally. Or make the files conditional on what's on the page.

In your jQuery, look to see if the element you want to work with exists before doing things to it. Perhaps your css, you should wrap the elements in a div with a unique ID, so the css only effects those elements.

DavidYell
+3  A: 

If you could create an external stylesheet for page-specific styles and keep your JavaScript in a separate file as well, then you would be able to use the HTML helper in a view to insert a link tag to the stylesheet where the $stylesheets_for_layout variable appears in the layout, and a script tag to the JavaScript file in place of the $scripts_for_layout variable. To do that, pass an array with the inline key set to false as the second argument to the script() method or as the third argument of the css() method.

echo $this->Html->script('extra-js', array('inline' => false));
echo $this->Html->css('extra-css', 'stylesheet', array('inline' => false));
Mike
A: 

You can also create another layout and use it for the specific methods as described in the manual.

bancer
A: 

Apparently I'm too new to vote something up, but I like bancer's solution. It's the simplest.

Imperatorr