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   

Next Entry >>

 

Previous Comments

Picture of MichelG

by MichelG

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

Hello Mike.

“What we

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!

Picture of Arda

by Arda

Date: Monday, November 24th, 2008
Comment: #8

Very nice tutorial and it’s exactly what i need but somehow I couldn’t make it work correctly. I think that’s because of I am very new to EE or maybe because of situation is little bit different.
In few words, what i want to add between this “about” entries, is not a contact page, but a link to another template which is getting data from another weblog.

I list the sub-pages on “index.php/site/about/”
And they link to pages “index.php/site/page/profile/”, “index.php/site/page/history/” ....

But I want to add a new link between them, to another template, which is getting its data from another weblog.
The link should be “index.php/site/board/” .

As a note, my template group is “site”. There are three templates named “about”, “page”, “board”.

It seems like it’s possible, but I just don’t understand why I couldn’t succeed.

Thanks again for this tutorial, and also for the others.

Picture of Arda

by Arda

Date: Monday, November 24th, 2008
Comment: #9

I solved it in another way. I created another template group named “page”. And moved my “board” template to there. And I removed the “site” part from my url paths, because it’s already default template group.

So now when I click the link “index.php/page/history/” it opens history post with “page” template. But when i click “index.php/page/board/”, it opens “board” template from “page” template group. :-)

I am not sure if it’s a healthy way, but it’s working..

Mike Boyink

by Mike Boyink (Author)

Date: Monday, November 24th, 2008
Comment: #10

Hi Arda -

It shouldn’t matter what the new template is doing - so long as you have an entry in your weblog that’s building the navigation that has a url_title that matches the name of the template you want to have EE load it should work.

However I think I see the issue.

Here’s where this tutorial works - If you have an About weblog and About template group, so you have URL’s of:

index.php/about/ (loads an index entry)
index.php/about/our-officers (loads the our officers post into the about/index template)
index.php/about/our-history (loads the our-history post into the about index template)

etc.

And now you want, say, a blog in the about section.  You’d create the new blog template and save it in the about template group, so it has a url of:

/index.php/about/blog

Then you’d create an entry in your About weblog with a url-title of “blog”, and EE would create the navigation - with a url of

index.php/about/blog

So the new template would load.

But you’re not using an index template to display specific posts like this approach assumes, you’re using the “page” template to display them instead, so this won’t work.

Unless you can re-arrange your templates and create some template groups you’ll probably need to hardcode nav.

Picture of Arda

by Arda

Date: Monday, November 24th, 2008
Comment: #11

Thank you very much Mike.

As an information, creating a template group did not work correctly. So, people shouldn’t consider the method that I wrote in my previous comment.

Now I am trying to solve this by using URL segment variables, combined with conditional variables.

Thanks again

Picture of lebisiol

by lebisiol

Date: Monday, July 19th, 2010
Comment: #12

Can’t help but to wonder…“why bother?” faking an empty entry just to generate URL and then have an actual static content served from a template rather than entry itself? You can easily handpick an entry and show it on template even if it is an index of the template group.
Is it to prevent users from deleting the (possibly) html code etc located in ‘contact form’ by not giving access to edit templates vs. they can delete the entry?
Is it so simple I am just not seeing benefits? :)
Thanks Mike.

Mike Boyink

by Mike Boyink (Author)

Date: Monday, July 19th, 2010
Comment: #13

Mainly the benefit is in the title - assume you have a section of content with the nav being built dynamically but then client wants to add a new “page” into the mix but for whatever reason (it’s a contact page, a different content type like a blog - not necessarily static like you mention) in the middle of generic pages, whatever) you can’t just have it presented through the same template as the rest of the entries.

And you can’t just lump a link to it at the top or bottom of the nav list because it won’t be in the right order.

Picture of Droppygreendy

by Droppygreendy

Date: Saturday, January 28th, 2012
Comment: #14

Hola
 
I’ve been lurking here for a long time but this is my first post. 
 
This board has been a wealth of knowledge but I wonder about additional resources you use for this type of stuff. 
 
Please let me know other sites/forums you use
 
I’ll go post an intro now!

Picture of Ross

by Ross

Date: Thursday, April 12th, 2012
Comment: #15

Nice Mike. Your articles helped me a lot. Very glad I’ve found your site.

Mike Boyink

by Mike Boyink (Author)

Date: Thursday, April 12th, 2012
Comment: #16

Glad to hear it!

Add Your Comment

Commenting is not available in this channel entry.

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

dy>