Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a navigation menu for different pages in SDL Tridion?

I have created different pages for all these categories: books, mobiles, comp, etc. Now I want to put this type of navigation menu (as shown in figure) in all pages and opening a page should highlight the corresponding menu link.

Should I create a schema which contains text and a link and make it multivalued? So then I create a component and finally render them in all pages?

If not please suggest any other better approach to do so.

enter image description here

like image 940
AmateurCoder Avatar asked May 26 '12 11:05

AmateurCoder


1 Answers

The most common way to generate any navigation in Tridion is to simply generate it in a C# template based on the Structure Groups and Pages.

For example a breadcrumb trail can be easily generated from within a template (either a C# fragment or a class implementing ITemplate) by something like this:

var pageDocument = package.GetByType(ContentType.Page).GetAsXmlDocument();
var current = new Page(pageDocument.DocumentElement, engine.GetSession());
var breadcrumb = page.Title;
while (current.OrganizationalItem != null)
{
    current = current.OrganizationalItem;
    breadcrumb = current.Title + " > " + breadcrumb;
}
package.PushItem("breadcrumb", 
                 package.CreateStringItem(ContentType.Text, breadcrumb));

The above fragment really only shows how to navigate the hierarchy of structure groups upwards. You'll still have to make every structure group as a link, probably by looking at PublishUrl property of every StructureGroup.

I know you were not asking about a breadcrumb trail, yours looks more like a leftnav. But the approach for all navigation elements is similar: traverse the relevant Pages and StructureGroups using the TOM.NET in your ITemplate and generate your navigation HTML from that.

To get a list of all the Pages in the current StructureGroup (and mark the current one), I'd expect something like this:

var pageDocument = package.GetByType(ContentType.Page).GetAsXmlDocument();
var current = new Page(pageDocument.DocumentElement, engine.GetSession());
var sg = (StructureGroup) page.OrganizationalItem;
string result = "<ul>";
foreach (var page in sg.GetItems())
{
    result += (page.Id != current.Id) ? "<li>" : "<li class='selected'>";
    result += page.Title;
    result += "</li>";
}
result += "</ul>";
package.PushItem("siblings", package.CreateHtmlItem(result));

Please also see this great example from Nick where he generates an entire sitemap. That is closer to what you'll need in the end, but is of course a lot more code (too much to reproduce here). Albert also shared some of his experience with this approach and mentions the alternatives.

like image 101
Frank van Puffelen Avatar answered Jan 02 '23 21:01

Frank van Puffelen