CSS, HTML, and some PHP - part 2

October 16th, 2008

Before I dive too deep into it, I’m going to give a brief overview of the files and folders. I’ll go more into them when building the site. You can download the basic set-up we’ll be working with here.

DONOTUPLOAD Folder

When working on a site, I like to keep a folder on my development machine, and in the site folder called DONOTUPLOAD. I keep all of my working files in this folder. They’re usually photoshop files. Sometimes there is only one, for the entire site, sometimes there are many if I have image templates for a gallery. I just add a new layer when I add a new image. This ensures that all images are the same size.

images Folder

This is my folder for images. I keep all the common images here, for like the header, background, etc. I use subfolders in this file for images that appear only on single pages. On really large sites, I’ll have subdirectories for section in the main site directory. Each subdirectory gets its own images folder.

js Folder

This is the folder where javascript files go. I always start off with a copy of jquery in it because for the most part, if I’m using javascript it’s going to be built off of the jquery library.

css Folder

This is pretty straight forward, but it is where I stash my style sheets. I always start off with 2, and try to stick to only having 2. The first is the one that I use to build the site off of. The second one, named ie6_style.css I link to from the header from a conditional statement. If I have to use any css trickery to make ie6 work then this is where it goes. Sometimes my css folder grows larger. I may need an additional conditional style sheet for IE7, sometimes I’ll break up my style sheet for jquery plugins.

includes Folder

If you’ve been following along up to this point, here is where you might run across something new. I use includes for things like the header, footer, and most often the main navigation of the site. This makes it easier because these items are almost always common to every page. If I add something to main navigation, it’s much easier adding it one time, to one file, then it is to add that link to every file in my site. Using includes means using PHP. This is why we installed Apache.

Inside the includes folder there are 3 files, one for the head, one for the foot, and one for the main navigation. If the main nav is horizontal and at the top, then I’ll include the main nav file inside the header include. The main nav include is currently blank, because we don’t have any pages yet. But the footer file, named inc.foot.php has some simple html in it, which we’ll add to. But right now, you’ll see a closing div tag, commented to show that it closes the wrapper div. There is also a closing body tag, and a closing html tag. Not too hard.

Now open up the header include named inc.head.php. Looks somewhat familiar. We see it starts of with a document declaration, in this case HTML 4. Then we see our opening html tag, our content declaration, standard meta tags used by some search engines like yahoo and msn, our title tag, a link to our style sheet, a conditional statement for the ie6 style sheet, we close our head, then we have the body tag, with an id (this will be used for navigation), and the opening of our page wrapper div.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" “http://www.w3.org/TR/html4/strict.dtd”> <html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ > <meta name=”keywords” content=”<?php echo $keywords; ?>” > <meta name=”description” content=”<?php echo $description; ?>”> <title><?php echo $title; ?></title> <link rel=”stylesheet” href=”css/default.css” type=”text/css” media=”screen” charset=”utf-8″> <?php echo $extras; ?> <!–[if lt IE 7]> <link rel=”stylesheet” type=”text/css” href=”css/ie6_style.css” /> <![endif]–> </head> <body id=”<?php echo $page; ?>”> <div id=”wrapper”>

The confusing part, if any are the sprinkled bits of php. This is really basic - it is simply echoing - which is like printing - the value of a variable, which in php begins with the $ sign. Where we pass those values is in the actual page, because these are the unique parts of the header to every page.

index.php Page

This doesn’t look like an html page that you’re used to, but it is. Here at the top, wrapped in php tags are the variables that we’re passing to the header. At the end of those variables we’re inserting the code from our header file. Then we’ll see a comment for the content, which where we put in the stuff that is displayed on screen. Finally, we call the footer to close the page out. <?php $title = ‘Page Title’; $page = ‘home’; $keywords = ‘ ‘; $description = ‘ ‘; $extras = ‘ ‘; include(’includes/inc.head.php’); ?> <!– content here –> <?php include(’includes/inc.foot.php’); ?>

We use an include for the footer for things like Google Analytics. Why copy and paste some javascript at the bottom of every page when you can include it in your footer?

If we look closer at the variables, the variable names are pretty easy to understand - most of them anyway. $title is the page title, that is inserted in the area. $keywords are the keywords for the meta tag keywords, $description is for the meta tag description. $extras is something I use for things that need to be in the header, but doesn’t need to be on every page, like linking to a javascript file like jquery. The $page variable is unique to every page, and has to be for what we’re using it for. This sets an id on the body tag of the page, so you can do things to certain pages with css. I mainly use this for navigation, but have used it in other ways.

If my navigation looks like this:

<ul class="mainNav"> <li><a class=”home” href=”index.php”>Home</a></li> <li><a class=”about” href=”about.php”>About</a></li> <li><a class=”contact” href=”contact.php”>Contact</a></li> </ul>

Then I can set active states of those nav elements by telling them in my css that if the body id is home, and the class is home, do something. So my css may look like:

ul.mainNav {list-style: none; padding: 0; margin: 0;} ul.mainNav a, ul.mainNav a:visited {color: #fff;} ul.mainNav a:hover {color: #999;} #home ul.mainNav .home, #about ul.mainNav .about, #contact ul.mainNav .contact {color: #666;}

I just told it that a normal link in my nav is white (#fff). When a link is hovered it is really light grey (#999), but when a link is active, as in you’re on the page that the link goes to, it is a medium shade of grey (#666). We do this because every page has a unique id attached to the body tag, in this case home, about, or contact. We then give each link a specific class, I usually name the class the same as the id to make it easier. So when a link with the class of home is displayed on a page with an id of home, it is medium grey - our active color, and all the other links would be white.

Sorry for the long post - but could you image going into all of this while building a site?

Tags: , ,

9 Responses to “CSS, HTML, and some PHP - part 2”

  1. Steve Says:

    October 16th, 2008 at 1:22 pm

    Dude,

    Seriously, I’m loving all of this. This is great stuff and very use full.

  2. Jason Says:

    October 16th, 2008 at 4:22 pm

    I’m glad it helps. I’m working on the rest of this series - so you should see some more over the weekend.

  3. Jonathan Cutting Says:

    November 13th, 2008 at 8:40 am

    The tip about passing a page specific name to the header and using it as the body id is brilliant - thanks!

  4. manu Says:

    November 13th, 2008 at 11:11 am

    The conditional is wrong, that’s for IE6 not 7.

    Nice stuff btw.

  5. Jason Says:

    November 13th, 2008 at 11:24 am

    @Jonathan - thanks

    @manu - I’m aware it’s for IE6, which is why the style sheet’s name is ie6_style.css

  6. mattgarvin Says:

    November 13th, 2008 at 1:10 pm

    We do things very similarly, and I guess your layout evolved over time like mine did. I am interested in your naming conventions, especially regarding images and class names. Also, do you put “Navigations Items”/”Website Sections” in their own sub-directories, one level deep from the site root, as you do with the other directories?

    I too use a “images”, “css”, “includes”, “js” and “donotupload” directory for resources. The only difference is I call “donotupload” something like “_SITEINFO” or “!SITEINFO”, with the first character causing that folder to show up at the top of all directory listings (in Windows Explorer, for instance.)

    Good Stuff!

  7. Jason Says:

    November 13th, 2008 at 1:24 pm

    On large sites I’ll break up the content into more directories. With that said, I try to keep them all on the same level so that I can use the same navigation include for all of my files, including my index page at the root level. I tend to be an organization freak though, so subfolders help me stay sane.

    I’ve lately started using ZZ_DONOTUPLOAD, to move that folder to the bottom of my directory listing after partially uploading illustrator and photoshop files one too many times.

  8. Nicholas Says:

    November 16th, 2008 at 11:49 pm

    Rather than inserting directly into your markup, why don’t you separate logic and presentation with a templating engine like Smarty or patTemplate?

    Makes everything MUCH cleaner and a great deal simpler when those two layers are clearly separate. It eases maintenance and dealing with unforeseen scalability issues.

    I would suggest Smarty, personally. Guarantee that you’ll absolutely fall in love with the concept once you give it a shot.

  9. Josip Says:

    March 18th, 2009 at 3:50 am

    Smarty is OK for splitting presentation and logic but can cause a lot of problems later in production environment.
    I had alot of problems with smarty cache files, because the technical user has no “write” rights in the production environment. This means – I have to run each and every template on the dev machine (so that the cached templates are created) and copy the cached templates (templates_c) to webserver. If I forget to run a certain page locally (which happens often if you have a big project) this page want be shown - all you will get is the “Unable to write to … directory” smarty error. Because of this you also can’t change smarty templates dynamically with java script (e.g inner HTML). I know, the main cause of this problem are the user access settings from my provider, but this is something what I can’t change so that is why I’m not using smarty templates anymore.

Leave a Reply

Name:

Email (will not be published):

Website:

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>