Building page titles and setting navigation active state dynamically.
This is the seventh chapter in the Building an ExpressionEngine Site Series, and in Chapter 6 we finally moved off the Home Page of our mock business site, and implemented the About page.
The job isn’t quite done, however, as neither the page title up in the browser title bar or the main navigation indicates that we’ve moved off the Home page. We’ll fix that in this chapter by using more embedded variables in our embedded templates.
Let’s tackle the titles first. The overall approach is very similar to how we implemented the Section Intros in Chapter 5. We want to pass a variable containing the current page title to the embedded template that holds our HTML header. So we need to:
Modifying the HTML header template
You’ll want to reference the EE docs on Embedding Templates within Other Templates as they’re helpful for showing how things need get formatted.
You need to edit the title html by adding the embedded variable to the end of the title declaration. I’ve named my variable “my_page_title”. Before editing you should have the following between your opening and closing title tags: (spaces added to prevent EE from parsing the variables):
{ site_name } | Building an ExpressionEngine Site
After editing it should be (again, remove the spaces):
{ site_name } | Building an ExpressionEngine Site | {embed:my_page_title }
Here is a text version of the updated html_header template for your reference:
chapter_7_html_head.txt
Modifying the Page Templates
With the html_header template setup to receive the variable, now we need to modify the Index and About templates to send the desired page title. You need to edit the very first embed statement, and add your variable name and value onto the end of it.
Before editing, the code is:
{embed="building_series_ch7_embeds/html_header"}
After editing, the code for the Home Page is:
{embed="building_series_ch7_embeds/html_header" my_page_title="Ch. 7 Home"}
And the code for the About page is:
{embed="building_series_ch7_embeds/html_header" my_page_title="Ch. 7 About"}
Note: I’ll link you to text versions of the Home and About templates below, after we’ve made the changes for the navigation.
Once you’ve made those edits and saved your changes, you should be able to move between the Home and About pages and see the browser title bar change.
Granted - this is a pretty simple implementation of this approach. As we get another layer deeper into the site (I’m thinking of areas like specific product pages, etc) we’ll have to put more code in there to get the desired page title - including weblog:info or weblog:entries tags. But that’s down the road, this will suffice for now.
Dynamic Navigation Styling
The other - more visual - issue with the results of Chapter 6 was that the main navigation still indicated we were on the Home page although we had navigated to the About page. If you look at the HTML for the main navigation, you’ll see that the tabs of the design are triggered by applying a “class=selected” to the one you want to be active.
So, the question is, how do we both keep our main navigation as one embedded template but still provide the proper class to set the active tab in different areas of the site?
The easiest and cleanest approach is one that EE’s Derek Jones detailed in his Behind the Curtain - Part One on the EE company blog. Essentially the idea is to use embedded variables along with conditionals to set a variable in each page template that tells the embedded main nav template which item to apply the selected class to. So we need to:
Modifying the Page Templates
This is very similar to the process above, where we added variables for the page titles. The only difference here is the text we’re entering as the value of the variable will never be displayed on the site—it’s just something for the conditional in the embedded template to work with.
OK -so starting with the Home page we have currently have the following code that pulls in the main navigation:
{embed="building_series_ch7_embeds/main_nav"}
This needs to change to:
{embed="building_series_ch7_embeds/main_nav" my_location="home"}
The About page is currently:
{embed="building_series_ch7_embeds/main_nav"}
This needs to change to:
{embed="building_series_ch7_embeds/main_nav" my_location="about"}
Modifying the main_nav Template
The page templates now send a variable to the main navigation template, now the main navigation template needs to be modified to change the HTML depending on what the contents of the “my_location” variable. Per Derek’s approach we’ll put a conditional within each list item. Since we pass only one value, only one of these will ever evaluate as “true”, so only one active class will be applied. The easiest way to present and see the code is to look at the text version of the template:
Note that you have to be a bit picky with the spacing - mainly so that when the conditional evaluates to false and the active class is not applied, you don’t get extra spaces in your linking code.
And - as promised - here are the updated text versions of the Home and About page templates. Again note - if you are following along and using these in another install of EE you’ll probably need to update the template group names, as they are coded to be chapter-specific.
Home Page Template: chapter_7_index.txt
About Page Template: chapter_7_about.txt
Here is the working example - you should be able to switch between the Home and About pages as expected.
What’s next? Let’s begin on the Products section. We might have to spend a chapter or three there as the Products section will be the most complex in the site as it will need a new field group, a new category group, two new templates, and updating the embedded “latest products” template.
FYI - I also found a stylesheet issue that was preventing some images from appearing in Firefox on Windows Vista - the paths were being rendered with double slashes because I had coded links as { site_name }/rest_of_path_here and they should be { site_name }rest_of_path_here.
Visit Train-ee.com for the latest in ExpressionEngine training designed with one goal in mind - to get you up to speed on ExpressionEngine® as quickly as possible.
The latest Train-ee Products:
The latest from the Train-ee blog:
September 19, 2007
He does it again! Thanks for yet another installment in this excellent series. Can’t wait for the products tutorials now!
September 21, 2007
Thanks for keeping up with this series.
It is fantastic!
September 27, 2007
What a wonderful tutorial! I haven’t been able to find anything else on the web that teaches the same process in such a clear and easy-to-follow manner.
Thanks again for all your hard work here and keep them coming! I’m particularly interested in custom Gallery templating.
September 27, 2007
This series is really helpful to me.
I have played with WYSIWYG design, but am quite new to proper XHTML/CSS and working with a dynamic CMS.
I settled on EE a while back (after a brief stint with Wordpress) after reading forums and such, and coming to value it’s flexible, extensible nature.
But I’ve had it installed for months and not really done much with it.
I never liked any of the available templates, and was at a loss about how to structure templates, and relate weblogs/fields/catagories/etc.
I’ve browsed the documentation/forums quite a bit, but they are largely tailored to specific technical questions; and here I am lacking basic clarity.
These tutorials have quickly brought me up to speed and got me coding. I’m not sure if I’m at “Aha” yet, but am feeling infinitely more confident and engaged.
Now I’m itching to de-construct other templates or simply code from scratch.
Thanks a ton!
September 28, 2007
Thanks for the comments guys.
It’s been a crazy week, but hope to get the next chapter up soon.
December 03, 2007
Excellent tutorials. Easy to use and understand.
Made me want to learn PHP and to do more with Expression Engine as Ive come from a Cold Fusion background.
December 03, 2007
Thanks to this tutorial, I was able to get over the learning curve and have released my very first Expression Engine site for my client. Click the link attached to my name to see the site.
Thanks again Mike! You made it happen!
December 03, 2007
Thanks again guys. Ian - congrats on the site launch.
December 13, 2007
Hi,
Many many thanks for this, I’m building a dummy site which goes away from the tutorial a little and I’m really struggling with the dynamic page titles.
Is there any way you could share the code you use on your own blog? It seems a shame to have to remember to define the titles in the templates when there is already data inthe name of the site, weblog, and individual posts.
December 13, 2007
Just to give a little back, you may want to check out the HTML and css of the site I’m building
http://seotraining.org.uk/
It puts the ‘real’ content right at the start of the page which is much better for SEO purposes and for devices like mobiles and text browsers which often read without the benefit of CSS.
December 13, 2007
Hi Steve -
If you keep moving through the chapters you’ll find templates that do more in the title tag - like chapter 12 where the template uses a weblog:entries tag to grab the title and send it to the embed.
December 13, 2007
Thanks Mike
Looks like I sped ahead a bit too quickly and missed that bit!
January 15, 2008
FYI: The Building an ExpressionEngine Site series is now available for purchase as an eBook on the newly-launched Train-ee.com.
January 31, 2008
For some reason, when I click on my ‘About’ navigational tab, the ‘Home’ navigational tab remains active (highlighted).
If I click on the ‘Home’ tab again, the ‘About’ tab deactivates as expected.
Any idea what would cause this?
January 31, 2008
Nevermind…
Turns out, on my main_nav template, I had an extra class="selected"
after my HREF for the home (index) tab. Therefore, it was remaining selected no matter what. I’m surprised it would handle the condition of having the variable ‘my_location’ equal to two different values.
The later at night I work on these tutorials, the more likely I am to make mistakes like this one.
February 01, 2008
Hello. I’ve got a question about the menu code:
a{if ‘{embed:my_location}’=="home"} class="selected"{/if} href="/splaat/building_series/chapter_7_index/" ><span>Home</span></a>
I understand what is happening and I can successfully copy what you’ve written but I don’t understand why the first {embed} bit goes between single quotes (and I can’t find any explanation of that from the EE docs) – and I don’t understand why the whole {if} statement goes immediately after the opening a. I would have guessed that it would sit at the same place as the class did previously.
At the moment I’m succeeding by copying you but I’m not sure I’m understanding so that I can apply that myself. (Having said that I am learning and understanding loads so thanks!)
February 01, 2008
Hi Bruce -
On the first question - it’s a matter of using the contents of an embed variable in combination with a security risk presented by using double quotes around the embed - see this KB entry. I’m still learning about different security risks work, in this case I pretty much just look at it as the programmers know what they are talking about, it works with single quotes, so that’s what I used..
On the second issue - you could move the whole conditional to sit at the same place as the class statement did. Since the class statement can appear either before the href or after it does’nt matter. I just preferred to see the conditional first.
February 07, 2008
I wanted to comment on the use of operators adding the selected class to the navigation links - You can accomplish that with the use of CSS and I find it much easier than embedding if statements.
-- An example --
navigation file:
<div id="menu">
<ul id="nav">
<li class="home">Home</li>
<li class="clients">Clients</li>
<li class="products">Products</li>
<li class="contact">Contact</li>
</ul>
</div>
CSS file:
#home ul#nav li.home a,
#clients ul#nav li.clients a,
#products ul#nav li.products a,
#contact ul#nav li.contact a {
background: #FFF;
color: #003;
border: 1px solid #E7F1F8;
border-bottom: none;
}
The first id is in the body tag, the class on the list item (li) is in the link. Each link is only highlighted when the body tag matches in comparison. What is your opinion to using this as opposed to the embedded if statements?
February 07, 2008
Either way is valid, and I’ve done both. I prefer the if statements as I can add sections/links w/o having to edit the stylsheet.
Overall I prefer it when my CSS isn’t content-dependent.