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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With