Articles
Basic ExpressionEngine Layout Tips
I’m already very impressed with ExpressionEngine’s versatility. It’s caused me to reconsider a few things about how content management systems can work.
I used to use WordPress and force it to be a content management system. For a while there, I played around with Chyrp, because it was so lightweight that I could go in and mangle it into whatever I wanted to be. Basically, I preferred systems that allowed me to dig right into their code and adjust things at my leisure.
I thought ill of systems that had their own templating language, but EE’s templating system is so well-done and comprehensive that I haven’t had to write one line in PHP yet. This kind of functionality — not to mention the brilliantly comprehensive documentation — is miles away from the days when I was cross-referencing WordPress functions to try to deduce what kind of data a particular object contained.
EE is great. But, for some reason, the built-in template is quite wasteful. There are too many files, code is repeated everywhere … it’s a bad initial example for anyone looking at EE for the first time. So, for your benefit (hopefully), here are some templating tricks I’ve learned from my first big dive into ExpressionEngine.
Content Separation Is The Best
The first thing you need to know when moving from something like Wordpress to EE is that the templates don’t have to directly correspond to your weblogs. Your actual site layout is defined by the template, but at no point is a template required to display some preset list of posts. Your layout’s index.php file doesn’t have to display your posts in reverse chronological order unless you want it to.
Keep this in mind when you lay out your site. You’re not constrained by blogging-system-type constraints.
For example, my main index page has two exp:entries loops: one pulls the 3 newest posts from my Work weblog, and the other pulls the 3 newest posts from my Articles weblog. These are all the parameters you need to make the exp:entries tag do that:
exp:weblog:entries limit="3" weblog="articles"
exp:weblog:entries limit="3" weblog="work"
I realize that this may seem obvious to some readers, but if you’re a long-time Wordpress user, this kind of flexibility will be a huge relief.
Layout Separation Is Also The Best
The default EE template doesn’t include good layout separation, and I don’t know why, becuse EE’s implementation is just killer. By “layout separation”, I just mean proper handling of the code that will be on every page — like your header and footer.
You can embed any template in any other template with embed='template_group/template'. You can also pass variables through these embed tags — for example, you could pass your page title through to your header template, so your page title can change according to whatever section your visitor is in (EE nicely explains embedding here).
Here’s my nice & lightweight page layout technique. I have a “layout” template group. Within it, I keep all of my common page elements:
- index.php — my site homepage
- .header.php
- .footer.php
- .sidebar.php — each of the sidebar (or, in some pages, top-bar) code sections that you see throughout the site are in here, within an if statement that chooses which to display based on the URL segment (I’ll talk about those in a minute).
- css_core.php — I almost always use php files as css; maybe I’ll write a post about that sometime.
So, all of my common content is tidily placed within the “layout” folder, cutting down on redundancy and clutter. (By the way, starting template names with a period defines them as “hidden”, so visitors can’t access them directly.) I also recommend pulling any other common code blocks out into their own file, like your post comment listing.
Other Spiffy Tips
Here’s some other stuff I had jotted down to mention:
- URL segments are so very convenient. You can quickly and easily access any piece of the current URL as
segment_1,segment_2and so on. This is powerful enough to cause cheating — for example, instead of using the built-inif category_requesttag to find out if the user is viewing a category page, you could sayif segment_2 == 'category'. Don’t do this — it breaks if you change your category path structure — but it’s just an example of what you can easily pull off with segments. - For applying alternating styles to a list of items, the
switchtag is your new favorite. - If you aren’t using them already, Markdown and SmartyPants are wonderful ways to simplify and enrich your blogging experience. They’re available as plugins for EE, either separately or together (as MarkyPants).
I had another section written about tidying EE’s overgrown URL structure, but this post is running long, and that section was even longer, so I’ll save it for next time. Until then, if you’re considering switching to EE or just started using it, I hope this post has been useful to you in some fashion.
Information
Cameron Daigle is a designer who scribbles information in notebooks, in his head, or (ever so occasionally) on this website.