onsdag den 14. maj 2014

Templates and inheritance of layouts... And why you should never do it

Okay, it's been a while since my last blog post, but I've been busy with other stuff.

Anyway, the other day, I ran into an interresting problem on one of the solutions we have made for a customer a while back.

It seems like that if you are inheriting from templates that has a layout (and sublayouts) defined, you might end up with a really weird acting Presentation Details window.

Here is what we had - these four templates:

Article Main Site
Article Sub Sites
News Article Main Site
News Article Sub Sites

All four templates had their presentation details set on their standard values, like they should.
The Main Site templates was using the layout "Main Layout" and the Sub Sites templates was using the "Sub Sites Layout" layout.

Now comes the problem - the customer was wondering how all the sudden, when they created news articles on their subsites, the layout of the news article was using the layout from the main site.

So, what to do? Well ofcouse, you just open the Sitecore backend, navigate to one of the news articles from a subsite, that acts weird, and takes a look at the Presentation Details, to see what is defined there.... To my surprise - it shows the "Sub Sites Layout" as the layout being used on this item.

So now we have an item showing layout A in the backend, but using layout B when rendering... Okay, maybe something hasn't been published correct, so I took a look at the web database - looks the same...

Back to the master database, going to the News Article Sub Sites template, and then to it's standard values - looking at Presentation Details here, everything looks correct... So the item is showing the wrong layout - so back to the news folder, and create a new test news item, so we know that layout hasn't been changed.... That one is also broken!

As you might imagine by now, I was getting pretty puzzled by this, untill somebody suggested that maybe it was a problem with inheritance...

He was right - it turned out, that "News Article Main Site" was inheriting from "Article Main Site", which seemed to work, since they where supposed to have the same presentations, so no problem showing up there.
However, "News Article Sub Sites" was inheriting from "Article Main Site", which meant that it inherited totally different presentations, resulting in this weird behaviour.

The solution was to switch the inheritance to inherit from the right template.

This is (one of) the reason that you should never inherit from a template with defined Presentation Details - it will break badly.

Instead, what you should do, is create a template for each field you want to have on your templates, and then inherit the fields from these templates (so you only have to define them once) - and then copy Presentation Details from template to template - this means that you have maintain them for each template, but thats better than having to debug this problem, and trying to clean up after it.

Ingen kommentarer:

Send en kommentar