Mixing Other Templates into Static Navigation

So - you’ve used one of the approaches here on Train-ee.com to build dynamic navigation from posts in an ExpressionEngine weblog—but now you want to mix in a link to a different page type - say a contact form.  And, you want this link to be sorted in the middle of the navigation items coming from weblog posts.  How to do that?

 

For the purposes of demonstration, let’s assume I have an existing section of simple static content with navigation to each page showing in the sidebar.  I now want to add a “Contact Us” page in this section, and the page needs to have a fill in the blank form.  I want the navigation to this new contact page to show up in the same list as the other pages in this section, but would rather use a different template to create the page.

Here’s what I would do:

  • Create a new post in the weblog holding my static content.  In this case I’d title it “Contact Us” which would get me a URL title of “contact_us”. Make sure to note the URL title.
  • Enter some dummy content - it won’t appear on the website so it doesn’t matter what you put in.  I usually put a note of “Don’t delete this entry as it’s used to create navigation”.
  • Save the new entry.
  • Just for kicks, go to your site and refresh things to ensure the new entry is showing up in the navigation.
  • Now return to the template group that contains the templates used to create your static content.  Create a new template.
  • This is the important part - make sure to name the new template such that it matches the URL title as your placeholder entry - so in my case the new template is called “contact_us”.
  • Add the necessary code to the new template and save it.
  • Go back to your site, navigate away from the placeholder page and click the “Contact Us” in the navigation again.  EE should load your new template rather than the placeholder entry from the static content weblog.

What we’re taking advantage of here is how EE processes URLs and the order it looks for things.  It looks for templates before content entries, so the new template will get loaded rather than the static content placeholder entry.

Overall this is just a neat little trick that lets us use weblog entries from one weblog to build navigation to a number of different templates - templates that may or may not draw content from the same weblog as the navigation comes from.

Note that you could keep all this functionality in one EE template, and use segments and conditionals to analyze the URL and either show static content or the contact form.  How you approach the build is up to you - for me it seems each site has more or less a fixed amount of complexity, and I can either build fewer but more complex templates or more templates that are individually less complex.  In other words - I can squish that complexity into either a tall and narrow shape, or a short and fat shape - but the shape has the same surface area...if that makes sense.  Often I’ll make a decision based on what’s going to be the quickest to figure out a year from now when I’ve forgotten how the site went together and have to look at the code to remember...which for me tends to be the more-but-simpler-templates approach.

Category Navigation

<< Previous Entry   

 

Previous Comments

Picture of MichelG

by

Date: Sunday, September 28th, 2008
Comment: #1

Hello Mike.

“What we’re taking advantage of here is how EE processes URLs and the order it looks for things.”

Is this behavior working with EE 1.6.4?

It is not working for me. The entry page content is displayed even if both the entry and template have the same URL title. And...yes...both share the same template group.

Mike Boyink

by Mike Boyink (Author)

Date: Sunday, September 28th, 2008
Comment: #2

Hi Michel - should be no change.  I have this running on client sites with the latest build of EE.

Have you compared the URL that is generated when you view the template directly, vs. what’s generated by the navigation?

Picture of MichelG

by MichelG

Date: Monday, September 29th, 2008
Comment: #3

OK. I figured it out.

Your trick only works if the URL_title_path is to the template group’s index page. In my case it was linking to another template page in that group. (Because my index template is unique to the site’s home page.)

So...I created an if/if:else condition in my menu to make it work. Or...I could also create another template group just for my section pages.

Thanks for your reply. Good tutorials. Very useful for all EE rookies like me.

Michel

Mike Boyink

by Mike Boyink (Author)

Date: Monday, September 29th, 2008
Comment: #4

Hey Michel:

Not sure what’s going on, but I’ve only ever used this approach when the oddball template I wanted to link to wasn’t the index template in the group.

You should need no conditional.

Did you compare the URLs that were generated?

Here’s one client example, running the latest build of EE:

It’s in their about section:
http://www.espec.com/na/about/

The template group has the following templates:
contact_us
detail
index
news

The index content of the page, “Why Choose Espec” , and Jobs all run through the index template.

The “What’s New” and “Contact Us” are both unique templates, but the navgiation to them is still generated by placeholder entries in the About weblog.  The url_titles of those two placeholder entries match the template names such that the path that is generated causes EE to pull the unique templates before it would load the about entry in the index template.

Re-reading your comment, I wonder if I wasn’t clear on the whole purpose of this particular tutorial - which was to dynamically generate a link to another template in the template group.  That, indeed, was the whole point.

Mike Boyink

by Mike Boyink (Author)

Date: Monday, September 29th, 2008
Comment: #5

Ah - yea I see what mean now. Yes, I usually use this approach to build exceptions within a template group where the index template is the default. If that’s not the case then this approach isn’t so clean.

Picture of MichelG

by MichelG

Date: Monday, September 29th, 2008
Comment: #6

Exactly. For those who are now confused, and using Mike’s site example above:

This will work:

<a href="{url_title_path=about}">{title}</a>

(Because the default index template is used for all the entries that are NOT “news” and “contact_us.")

But if you want your entries to use another template such as “page,” this will not work:

<a href="{url_title_path=about/page}">{title}</a>

Picture of Richard

by Richard

Date: Tuesday, September 30th, 2008
Comment: #7

Hi Mike,

Really like this idea and have just implemented it without any trouble on the site I am currently working on. Thanks a lot for showing the technique.

Its weird, I can implement some pretty impressive stuff with EE, yet I still struggle with some of the fundamentals. Currently struggling with categories/templates/relationships for use in a staff/course directory - but thats another story!

Add Your Comment

Comment on this Post

  

Unless otherwise stated all content is © Michael Boyink of Train-ee.com & Boyink Interactive. Please don't steal - I've got kids to feed...