October 8, 2006

Creating an A-Z index with ExpressionEngine

For the first article in my series about building the new HSU site with ExpressionEngine, I thought I’d write the compliment to my earlier article where I explained how I built HSUs A-Z index with Textpattern.

Setting up the framework

The first thing I did was set up the framework: a weblog (EEs name for a container for related posts) called siteindex” with 26 categories named A-Z where I would publish the entries, and a template group also called siteindex” with 26 templates named A through Z. Naming the template group and template pages in this way creates clean urls— the url of the page with the A entries will be humboldt.edu/siteindex/A. Then I entered all the data for the index into the weblog, categorizing each entry under the alphabetical letter of the page that I wanted the entry to be displayed on.

Coding the templates

One of the great things about ExpressionEngine is that it allows for user-defined variables that can be used within EE tags. These become a really convenient way to manage a group of templates that are essentially the same, differing only in the category, weblog, etc that is called by the EE tags. So, I start out each if the 26 template pages with:


The first line sets the letter of the index page, which I use in the `tag of the page and in the

` tag:

{letter}: HSU A-Z Index


# A-Z Index: {letter}

. The second line sets the category id for the weblog category A”, which happens to be 54 in this case. I use this within the EE tag that produces the table cells with the entries filed under A, with siteurl, dept-name, loc, phone and fax being fields of data entered in the siteindex weblog:

{exp:weblog:entries weblog="siteindex" category="{catid}" orderby="title" 
sort="asc" status="open" disable="member_data|pagination|trackbacks"}






The template also includes all the the other code to layout and format the page, and is repeated 26 times. Luckily, EE provides include templates that allow me to write all this code only once and call it on each of the 26 template pages:


So, each of the 26 pages actually only contains the following:


with the letter and catid varying for each letter of the alphabet.

The real advantage of EE

The best part of the A-Z index in EE is that it is now the official repository for department names and urls on the main site. EE has an amazing feature called relationship fields, which allows you to refer to information posted in one weblog from within a second weblog. So whenever I need to list a linked department name, I can refer to this siteindex weblog rather than duplicating the information. This has already proved to be a huge time saver for me, as several departments have changed their name since we went live with the new site. I changed all the references to these departments in a few seconds by changing the name in the siteindex weblog. I will be writing more about how I used relationship fields in the near future, so stay tuned.

Previous post
Changes in the air So, I have been trying out Vox for a couple of weeks, and I have to say that I’m impressed. The interface is really nice, and makes it a snap to add
Next post
You flicker and you’re beautiful Ok, this has been bugging me for years now. Am I the only one that hears The Cure every time I hear or read the name Flickr? Flickr Flickr Flickr