12
Jul
2006
Custom Blog Links
So it’s 11:15 and I’m sitting here hammering away at a new blog skin for this site. One of the challenges I’m facing with Community Server is creating a custom format for the links at the top of the blog. For the current skin, I don’t use <CS:NavigationMenu> to generate the navigation links. Instead, I make direct calls to the individual links that I want, such as <Blog:HomeLink> and <Blog:AboutLink>. These links render as list items by default, and you can pass in an “IsListItem” attribute to override this behavior. But what if you want even more control than that?
For the new skin, what I need is to generate the following markup for the links:
<ul>
<li><a href=”#”><span>Link Text</span></a></li>
</ul>
The only difference, in terms of the link itself, is that there is a span tag inside the anchor tag. As far as I’ve been able to tell, the built in blog controls do not provide an easy way to do this. However, after some sleuthing in the CS source code, I’ve managed to hack out a way to generate this markup. It’s not at all pretty, but it seems to work. Here’s how it goes:
The first thing you’ll need to do is to add the following namespace import directives to the top of your page.
<%@ Import Namespace=”CommunityServer.Components” %>
<%@ Import Namespace=”CommunityServer.Blogs.Components” %>
These provide you access to the two utility classes that you’ll need to use to make this happen: CommunityServer.Blogs.Components.BlogUrls and CommunityServer.Components.CSContext. The former provides methods to retrieve the URLs related to your blog. These methods require you to pass in your blog’s application key as a parameter, however; CSContext provides you with access to your application key, among other things.
How you put all of this together is pretty simple, actually (assuming you’re familiar with the basics of ASP.NET). The code to generate the link is as follows:
<a href="<%= BlogUrls.Instance().HomePage( CSContext.Current.ApplicationKey ) %>">
<span>
<CS:ResourceControl ResourceName="Weblog_BlogLinks_Home" runat="server" />
</span>
</a>
That’s a lot of text! Let’s pick it apart.
CSContext.Current.ApplicationKey gives you the application key for the current blog. The application key provides a unique identifier for each blog.
BlogUrls.Instance().HomePage() is the method that gets you the link to a blog’s home page. Which blog? Whichever blog corresponds to the application key you feed it. BlogUrls contains identical methods for the other links on your blog, such as About() and ContactForm().
The <CS:ResourceControl> control simply retrieves text from your Resources.xml file. You pass in a resource name via the ResourceName attribute and the control gets that text from the XML file.
And, of course, you can see my span tag inside of the anchor, which was my goal in the first place. You can substitute whatever markup you please, of course, and I’m sure that there are ways to turn this into a user control, though I’m not sure how that would integrate with CS. If anyone has a better (read: cleaner) way of achieving the same effect, I’d love to hear it.