07 - Tweaking the Static Templates
In this chapter I get fussy with the code formatting and make some structural changes that should set us up for a better ExpressionEngine implementation down the road.
In today’s nudge-along on this series we stay at the HTML/CSS level, making some changes purely out of preference and some that should help net us a better end result after pulling apart the templates and doing the EE implementation.
Here’s what I will cover in this post:
- Cleaning up code formatting
- Adding ID’s to the body tag
- Cleaning styles out of the HTML
- Changing the Doctype
Cleaning Up Code Formatting
This is one of those things that, at first glance, can appear simply as fussiness on the part of the coder. However, I think well-formatted code is actually a productivity gain well worth the time investment. Why? Well-formatted code makes it easier to find specific items, easier to find unclosed div tags, and will make it easier to chunk up the still-complete page templates into embedded EE templates.
The challenge, if you work with EE code in the default template editor with it’s textareas, is nicely formatting code using tabs for indentation. One solution is the TabInTA Firefox Add-on. It changes the default behavior of FF - so that rather than the tab key moving you out of the current textarea to the next one, it inserts a tab into the textarea’s content. This little add-on has really helped me improve the formatting of my EE code.
I won’t cover the specifics of how I format HTML-based templates as it should be pretty obvious from the companion files. Overall it’s just trying to reflect the nesting of the document structure with indent levels.
On the CSS front, I prefer all declarations to be in this format:
#name {
property: value;
property: value;
property: value;
property: value;
}
I know this style of formatting can add some size to the CSS file, but I’ll give up a bit of size for an easier to read file. I’ve also taken to using nesting in CSS, so if - in the above example - there was another item within the #name div that was inheriting styles from #name it would appear in the CSS as:
#name {
property: value;
property: value;
property: value;
property: value;
}
#name .another_thing{
property: value;
property: value;
property: value;
}
A hat-tip to Andy Ford at AloeStudios for that CSS approach - I’ve found it really helps understand the CSS being used and find the declaration I’m after when I need to make a change.
Adding ID’s to the Body Tag
Now we move from formatting changes to structural changes. Here are the issues I see with the templates as they come out of the box:
- The banner divs are unique to each page - and the value of the banner ID is what determines the banner image for that page
- The main navigation lives inside of that banner div
Why are these issues?
I want to have one embedded template that both pulls a unique page banner image and sets the active state on the correct navigation item - this way my main navigation code is only stored in one place which makes future changes really easy. This arrangement was going to make it tough to do that elegantly.
My solution was to add an ID tag to the body tag. The ID will tell the template which page is being viewed. For example, the index page now has:
<body id="home">
This lets me:
- Have a generic banner div in the HTML - better for creating an embedded EE template to hold my main nav
- Only specify the size of the banner div once in the CSS
- Specify the banner image in the CSS using inheritance
- Be able to specify other page-specific CSS using inheritance
- Be able to use an EE variable to set both the body ID and main nav location (in a future post)
So where the CSS had:
#bannerHome { background:url(../images/banner1.jpg) no-repeat; width:770px; height:225px; clear:both }
#bannerMinistries { background:url(../images/banner2.jpg) no-repeat; width:770px; height:225px; clear:both }
I now have:
#banner {
width:770px;
height:225px;
clear:both;
}
body#home #banner {
background:url({site_url}/images/interface/banner1.jpg) no-repeat;
}
body#ministries #banner {
background:url({site_url}/images/interface/banner2.jpg) no-repeat;
}
You’ll be able to see the full extent of the changes in the companion files.
Cleaning Styles Out of the HTML
I also prefer to have all styles in the CSS template. I can see that the way the templates came coded it lets certain styles be re-used in slightly different ways in different HTML templates - which means a smaller CSS file. But I’ll trade off a slightly larger CSS file, containing some page-specific styles, for a more complete separation of content and presentation. I just know that, down the road 8 or 10 months, if I had to come back to this site and add a new content type and tweak some styles, that it would take me longer if some of the styles were contained in the HTML templates.
For the index template there were two sections to edit; the Join Us area in the right column and the footer. What I did for both was add some style declarations to the CSS - which you can see in the Companion Files. The new footer items are found indented under the parent footer declaration and the new Join Us styles are added at the bottom.
I tend to work this way with CSS documents - fitting new items into the existing structure if it makes sense, or adding a new area at the bottom. At the end of the project I might then re-arrange/re-organize the CSS.
Changing the Doctype
I also changed the Doctype of the HTML from HTML 4.01 Transitional to XHTML 1.0 Strict. I know there are great numbers of blog posts out there talking about the differences between these two and when you might use one over the other. For me the choice was a bit more pragmatic as I know that ExpressionEngine offers three formatting choices for content fields:
- “None”
- ”< br />”
- “XHTML”
I prefer to use “XHTML” where possible as I think it does a nicer job of marking up user-entered content. However, if I use XHTML formatting for content fields and leave the Doctype set to HTML 4.01 Transitional then the site will likely not validate. Why? The tags that ExpressionEngine adds in the content areas will have the XHTML style tags that won’t validate under a HTML Doctype.
For example, HTML 4.01 wants break tags like:
<br>
while XHTML wants (and EE will output) break tags like:
<br />
Again - a bit of fussiness on my part but in this case it took very little work to change out the Doctype and make the necessary changes to get the template to validate as XHTML 1.0 Strict. Doing this now will be easier than down the road, and it should be easier to keep the site validating even with EE generating content from fields set to XHTML formatting.
The Results
Visually our rendered index page still looks the same. But the W3C Validator is happy with it, and I’m happier for having the code cleaned up a bit and having all styles in the CSS document.
In the next installment we’ll start the chunking up process, whereby we’ll create embedded templates to contain all the elements that repeat on the page. You can get a head start on me by reviewing the EE Docs on Embedding Templates within Other Templates.
Cheers!
Category Navigation







by tzTrainEE
Date: Monday, April 28th, 2008
Comment: #1
Ahh, good good. I was thinking of going html strict myself, but with EE it’s better to go with xhtml doctype, which is no small point. Looks like it’s time for me to move to xhtml once and for all then.
Thanks for taking the time to fuss with the templates. It gets to be a habit for me, but I really like having the validator giving me that green light and saying the page code validates.
There are firefox browser extensions that put any page a right-click away from the validator, and the validator makes it easy to catch errors in the coding, like we’ve seen people having already in your earlier series comments threads, great for troubleshooting.
Link: https://addons.mozilla.org/en-US/firefox/addon/2250
It will be entirely possible now with the cleaned up templates and EE set to xhtml coding.