Planning Ahead with your ExpressionEngine Templates
Posted April 30, 2011 by Ryan McLaughlin - nonactive Short URL: http://bcw.im/b5a1
Creating building-blocks of elements can save time and headaches later.
When first learning ExpressionEngine, the complete "ground-up" functionality of the template system can be daunting to say the least. What is the best way to organize a site you're building? A lot of websites have unique elements in certain pages or sections, so the old-fashined homepage & subpage template style is fairly lacking. There was always the option of having one template per page, but that kind of defeated the purpose of using templates in the first place. You could always create a bunch of canned layouts for future use, but then dealing with unique IDs and classes for those unique elements was always a pain.
Regardless of how you decided to set up your templates, the main issue was that once you created a site and got it up and running, there would inevitably be some changes from the client in terms of adding new pages, moving sections around, or completely changing the layout of an area. When you end up embedding sections with reckless abandon, those "simple" tweaks from the client can become hairy tangles of embeds that blow through your leftover budget.
So, what's the best way to "future-proof" your template structure so that you can speed up development time, cut down on redundant code, and most importantly, make those later changes without much trouble?
Well, let's look at an example site and dissect its different sections:
- Home
-
Products
-
Widgets
- Blue Widgets
- Green Widgets
-
Thingamajigs
- Tall thingamajigs
- Short thingamajigs
-
Whirleygigs
- Cheap
- Medium
- Expensive
-
Widgets
- Services
-
Company
- About Us
- Our History
- Mission Statement
-
Support
- Technical
- Sales
- Contact
The right tools for the job
Setting up shop
_inc (the underscore keeps it above all other folders when looking in your FTP client... you are saving your templates as external files, right?). This contains the header, my footer, and the meta tag template. These are things that appear on nearly every page, so we want to manage them separately. This is a good place to put your stylesheet if you plan on running it through EE's native parser. products, interior, and support. What goes in here? Well, one template for each type of layout in that section. Each folder will have an index file by default, so for home we'll just use that one. In the others, we can use index for the default, and then create others as we see fit. You can also chunk up re-usable parts in a section, say for example a common sidebar on all product pages, into include templates in these folders (it's too bad EE doesn't let you nest template folders)._shell, and create one template for each unique section we have. In this case, we mirror the template folder structure with home, products, interior, and support. The code in these templates is short, but here it is:
{embed="_inc/header"}
{exp:switchee variable="{segment_1}"}
{case value="your_section"}
{embed="content/your_section"}
{/case}
{case value="another"}
{embed="content/another_section"}
{/case}
{case value="last"}
{embed="content/last_section"}
{/case}
{/exp:switchee}
{embed="_inc/footer"}
Organizing your channels
_shell/products. Then we can use that shell template to look at the URL and dictate where things need to go. It essentially lets you change your mind about which templates certain sections use, without having to go in and change every single entry. Pretty sweet, right?_shell/products template to go use a different template when a user visits the page, and it's done.Conclusion & Disclaimer
Here's what other people had to say
Nice points.
Just a question, slightly off the point: what are the pros and cons of running the stylesheet through EE’s parser? I thought I was better of minimizing both the javascript file and the CSS file out of the EE file.
Emmanuel, I started typing a response here but it got too lengthy… I went ahead and posted a new article here. Let us know if that helps!
I just worked on a site and used Structure for the first time. I’ll keep this in mind for next time, thanks for writing it up!
I havent used Structure yet, but like how it sounds.
I just want to make sure I have something correct to load embedded templates out of this section, the embed tags should read something like
{embed="_shell/your_section"}
not
{embed="content/your_section"}
right?
This is interesting way of doing things. If you have multiple sections with “articles” in each, though, it seems like you can’t really get around using a structure like:
Section 1:
—index template
—story template
Section 2:
—index template
—story template
Section 3:
—index template
—story template
I was hoping to use a technique to get around the need to cut down on templates as sections expand (one site I work on uses 30 sections!) I can see how the embed comes in handy for rapidly changing very similar layouts. But it would be nice not to have 30 story templates at all. I guess, in theory, we could make a “story” template in a section called “articles” and make everything link there, then have some code on the story template so that it “knows” (based on the story category) how to decorate the story template.
Just a point of info: sites start to get sluggish with Embeds. I’ve been using Solspace’s Template Morsels for awhile, but I’ve only just started using one of its features called “Embed Harvest.” Between the two, I’m able to get rid of almost all regular embeds and the site speed is much better & hits to the database are fewer.
very good cms solution with a massive list of features. It doesn’t match Wordpress as far as available themes and modules but it has alot of the key options out of the box without the need to installing anything additionally..
Seeing that you first embed the header in a “shell” template, what solution do you use to set “title” and “meta-description” elements into the header?
Your suggestions for developing website are appreciable,I thing every professional must follow these instructions before development.
All the sections are really interesting. And the code example is excellent.
I’m Looking forward to see the output of the new expression engine. Thanks for this info!
Many many quality piotns there.








