Search code examples
asp.netkentico

Kentico 9 - Display language selector inside transformation


I am trying to display a language selector inside a transformation.

It works if I add the web parts to a page but defining them inside a transformation doesn't work.

This is what I have currently inside my transformation but the repeater just displays "Nothing!".

<cms:LanguageDataSource ID="dsLanguages" ExcludeUntranslatedDocuments="false" ExcludeCurrentCulture="false" runat="server" />

<ul class="sub-menu">
  <cms:BasicRepeater ID="rptLanguages" DataSourceName="dsLanguages" ZeroRowsText="Nothing!" runat="server">
    <ItemTemplate>
      <li class="menu-item">
        <a href="<%# Eval("URL") %>"><span class="flag-icon flag-icon-<%# Eval("CultureCode") %>"></span><%# Eval("CultureName") %></a>
      </li>
    </ItemTemplate>
  </cms:BasicRepeater>
</ul>

Solution

  • This is really good question and I see that there is generally a lot of confusion about using Web parts inside Transformations or ASPX templates.

    The main point here is that

    Web parts are not generally supported in transformations or ASPX templates

    Since Web parts are generally user controls you can register them in transformations, but due to their specific life cycle there is no guarantee that they will actually work. More complex web parts will certainly not work and this seems to be the case for the language selector as well.

    The official documentation states that: (SEE UPDATE)

    Using controls in transformations

    You can register and use user controls and servers controls inside transformations. See that done Transformation examples. However, using web parts in transformations is not recommended for performance reasons.

    However, this is not entirely accurate as they are not supported at all. I have already submitted a request to technical writes in Kentico to remedy this.

    You could eventually try to make a regular User control out of the language selector and register that, although I'm not entirely sure whether it would help. I know that some Kentico default transformation are indeed registering User controls so it might help in some cases.

    Generally speaking try to avoid using User controls in transformations whenever you can. I would recommend you to just place the language selector web part outside the transformation, it will be the easiest way to get it working properly without any other adjustments.

    UPDATE:

    After talking to Kentico teams the note here was changed to reflect the current state to:

    We do NOT recommend using web parts within the code of transformations. This could lead to poor performance and certain web parts may not work correctly within transformations due to their life cycle.