Wednesday, March 28, 2012

Dynamic Linking with Tridion and NetBiscuits

Even though the biggest boom of must having everything mobile seems to have gone already there a lot of companies that want to build a new mobile web site. Sometimes you might even encounter a company or two that want a mobile website or an app just for the sake of it. I might write a separate post (=rant) about going mobile without having any strategy for it, but that's a story for a later date.

But now I'm getting side-tracked, the point today is how to utilize dynamic linking with your mobile websites when you're using Tridion and NetBiscuits. I'm going straight to the good parts so if you're just starting with NetBiscuits I suggest you start from here and there.

The Problem

NetBiscuits uses its own markup-language called BiscuitML (BML), in addition of that the RTF-fields in BML use a subset of BBCode to make things even more interesting. Neither BML or BBCode support the standard anchor-elements which the out-of-the-box Tridion-controls output. Using the standard controls actually results in malformed BML and breaking the whole page.

The (partial) Solution

Fortunately the solution is actually quite simple. The approach I took to solve this was instead of creating brand new controls I extended the existing Tridion-controls. The benefit of this is that you can use the same control for outputting the regular HTML-links as well as NetBiscuit BBCode or Link-elements. Also it means that if anything changes with the original control you can just recompile the project with the new DLL.

To use the new features just add an additional attribute "NetBiscuitLinkType" to the ComponentLink-control with one of the three values URL, URLNoFollow or Link (case insensitive). For example: You should use URL and URLNoFollow-attributes when you want to use your links inside RTF-fields (i.e. BBCode) and Link-attribute when you want an independent link (see BiscuitML-reference for more details). The output for each attribute:

Output with value "Url": "UrlNoFollow" respectively: ...And with value "Link":
Because you have to set the attribute to the control manually you still have to figure in which context the (dynamic) link is used. Fortunately, it is relatively simple, because you add the control-tags in your templates so in most of the cases you know if the link will be inside RTF-field or if it is used independently.

You can use simple RegEx to parse all the ComponentLink-tags and add the additional attribute to them that way (yes, go ahead, you can safely match the tags in the HTML with RegEx because they're not arbitrary, you won't be summoning any Great Old Ones). I will try to add some example TBB(s) how to do this part of the solution later on.

Where to get it?

You can grab the project from GitHub. At the moment it is only available for .Net and only handles Component Links.


Any suggestions and contributions are welcome!

EDIT: The project is now updated with a TBB to handle the conversion to ComponentLink-tags, see GitHub for installation/usage instructions

Monday, March 26, 2012

...but when I do, I do it in production

So, I finally gave up my fight against the masses and decided to create my first blog ever (I'm still fighting though, e.g. I don't have a Twitter account...but I'm quite tempted to get one). I also wonder if I should curse or thank my colleagues for encouraging me to start blogging (maybe they're the ones that will regret this later on), only time will tell.

First question probably is that what are you going to find from here. I wish I could tell you, I usually have more than a few things in my head that I want to write about (at the moment of writing this I have six drafts here and few coding projects waiting to be finished and shared) but my problem is that I always end up polishing them until end of times. I think it is safe to say that there will be code, a lot. Most of it is probably Tridion related due to my current job.

I guess the other part of the blog will be my thoughts about the software industry in general but in stereotypical Finnish way I'm a man of few words, that is, unless I'm ranting about something or if I'm having a few beers, so don't expect too much.

You can find some code that I've written from GitHub. I'm planning to provide full-blown projects there for any code samples I might share here (or other way around, base the code samples on some project I have in GitHub). If you're new to Git/GitHub then you should start from here and here. Suffice to say I selected GitHub because of the ease of contributing to projects so anyone interested in my examples can contribute or reuse my code. Unfortunately I haven't released a lot yet but I hope that I can get it growing considerably during this year.

I guess I should also explain the title of the post (and url of this blog). It all comes from the time I started with Tridion almost 2 years ago and was working on my first Tridion-project (at the time I wasn't working for SDL though). During one day I was demonstrating TOM.Net API for a customer and the first question I got was "So, how do you write unit tests for your templates?". Of course inexperienced as I was I answered without flinching something in manner of "Well, you just mock the classes and test your logic, just like you would do without Tridion". People with Tridion experience can probably guess how wrong my statement turned out to be...

As some people might know, I might be quite stubborn sometimes. And most of all, I'm not really one to accept defeat easily. So naturally ever since that occasion I've been on a never ending quest to prove myself right, that you can unit test your Tridion templates. My latest endeavor to solve this can be found in StackOverflow (Please contribute if you have any ideas!). I know there will be a beautiful day when I can finally write see that lovely green color of passing tests in my (Tridion) template code. On that fateful day I do not have to test my code in production anymore...

(For the nitpickers: Yes, you usually test your code in dev/test/acceptance/whatever first but I've seen cases where the input differs enough for the code to fail in production but not in others, so ultimately you test your code in production)

tl;dr: Hello World! Welcome to my first blog, Here I will be writing mostly about code (probably a lot is Tridion related due to my work). And if you're wondering, URL of the blog comes from the fact that I can't always test my code even though I really want to :(