Transformations are very powerful tools within Kentico and very useful in rendering content using templates for Document Types, Custom Table data and other content. A transformation is a code template that determines how listing web parts and controls render content. The system stores transformations as virtual objects in the database, and you can easily reuse transformations for different components.
You can use several different approaches when writing the transformation code but for the purpose of this article, all the transformation examples display here are of ASCX and few Text/XML markup.
ASCX Transformations
Display Page Title.
<%# Eval("DocumentTitle") %>
Display Page Tags.
<%# Eval("DocumentTags") %>
Display Page Tags with links to Tag page.
<%# IfEmpty(Eval("DocumentTags"),"","<b>Tags:</b> " + BlogFunctions.GetDocumentTags(Eval("DocumentTagGroupID"), Eval("DocumentTags"), "~/TagPage")) %>
OR
<%# BlogFunctions.GetDocumentTags(Eval("DocumentTagGroupID"), Eval("DocumentTags"), "/TagPage") %>
Display Page Keywords (Metadata).
<%# Eval("DocumentPageKeywords") %>
Display HTML link ( tag).
<%# GetDocumentLink() %>
Display the URL of the specified page.
<%# GetDocumentUrl() %>
Display the permanent URL of the specified page (with the NodeGUID in the query string).
<%# GetDocumentUrl("NodeGUID", Eval("NodeAlias")) %>
Display the URL of the specified media file.
<%# GetMediaFileUrl(Eval("FileGUID"), Eval("FileName")) %>
Display an image tag of an image file.
<%# GetImage("NewsTeaser") %>
Display specified date.
<%# FormatDate(DateTime.Now) %>
Display formatted date and time value.
<%# FormatDateTime(DateTime.Now, "MM/dd/yyyy HH:mm") %>
Display the value of the specified field for the current search result.
<%# GetSearchValue("DocumentName") %>
Check the field for certain values and display accordingly.
<%# IfCompare(Eval("YourFieldName"), "True", "hide", "show") %>
Example usage:
<p class="<%# IfCompare(Eval("YourFieldName"), "True", "hide", "show") %>">Display This Text</p>
Display the value of a custom image field (Form Control: Media Selection) called “ThumbnailImage” only if the value is not empty
<%# IfEmpty(Eval("ThumbnailImage"), "", "<img src=\"" + Eval("ThumbnailImage") + "\" />") %>
List few custom fields only if a certain field value is not empty, in this case Value1.
<%# IfEmpty(Eval("Value1"), "", "<li><a href=\"" + Eval("Value2") +"\" title=\"Check "+ Eval("Value3")+ " For More\">Read More</a></li>") %>
Remove HTML tags from a custom field.
<%# StripTags(Eval("YourContent")) %>
Display a list of items based on a certain Tag in a Repeater using the WHERE condition by TagID.
DocumentID IN (SELECT DocumentID FROM CMS_DocumentTag WHERE TagID=999 )
Display a list of items based on a certain Tag in a Repeater using the WHERE condition pulled from a query string (tagid or tagname and groupid).
Example URL: https://www.YourSite.com/Tag?tagid=999
Example URL: https://www.YourSite.com/Tag?tagname=YourTagName&groupid=1
('{?ToInt(tagid, "")?}' = 0 AND '{?tagname?}'='') OR (DocumentID IN (SELECT DocumentID FROM CMS_DocumentTag WHERE TagID = {?ToInt(tagid, "")?} )) OR (DocumentID IN (SELECT DocumentID FROM CMS_DocumentTag WHERE TagID IN (SELECT TagID FROM CMS_Tag WHERE TagName = '{?tagname?}' AND TagGroupID = {?ToInt(groupid, "")?} )))
Get the querystring value using ASCX.
<% var myValue = Request.QueryString["parameter"]; %>
Display a list of Categories.
<asp:PlaceHolder ID="plcCategoryList" runat="server" EnableViewState="false"> </asp:PlaceHolder>
Display a list of Categories with Count.
<li class="CategoryListItem"><a href="/CategoryPage?categoryid=<%# Eval("CategoryID") %>"><%# Eval("CategoryDisplayName") %></a> <i>(<%# Eval("CategoryCount") %>)</i></li>
Display Blog teaser/summary.
<%# IfEmpty(Eval("BlogPostTeaser"), "", "<img src='"+Eval("BlogPostTeaser")+"' />") %>
Check to see if user is authenticated.
<% if (CMS.Membership.AuthenticationHelper.IsAuthenticated()) { %> Authenticated! - Display something. <% } else { %> Not Authenticated! - Display something else. <% } %>
Replace one character
Example: Replace empty space with a dash (-)
<%# Eval<string>("YourContent").Replace(" ", "-") %>
Replace two characters at the same time
Example: Replace empty space with a dash (-) and also replace | with a dash (-)
<%# Eval<string>("YourContent").Replace(" ", "-").Replace("|", "-") %>
Collapse/Dropdown Button
Create a dropdown button and collapse content using Bootstrap styles
<%# IfEmpty(Eval("YourContent"), "", " <a data-toggle='collapse' aria-expanded='false' data-target='#YourContent-"+Eval("ItemID")+"' class='btn btn-sm btn-primary mt-2'>My Button</a><div id='YourContent-"+Eval("ItemID")+"' class='collapse multi-collapse alert alert-primary p-2' data-parent='#ParentGroup-"+Eval("ItemID")+"'>"+Eval("YourContent", "")+"</div>") %>
Text/XML Transformations
Display Page Categories.
{% Object.Categories.Filter(CategoryParentID==1).Transform("<span class='category'>" + "{%DisplayName%" + "}</span> ") #%}
Display Page Categories with links to Category page.
{% Object.Categories.Filter(CategoryParentID==1).Transform("<a class='category' href='/CategoryPage?categoryid=" + "{%CategoryID%" + "}'>" + "{%DisplayName%" + "}</a> ") #%}
Display Text If Certain Category Is Not Empty.
{% if ((Object.Categories.Filter(CategoryParentID==1).Count) > 0) {return "<b>Category:</b> "} #%}
Display Page Tags.
{% Object.Tags.Transform("<a href='/TagPage?tagid=" + "{%ID%" + "}'>" + "{%DisplayName%" + "}</a>, ") #%}
Date Format / Split Date.
Example: 12/22/2024
{% Object.PublishedDate.Month #%}/{% Object.PublishedDate.Day #%}/{% Object.PublishedDate.Year #%}
Limit Text
Limit the content to certain number of characters.
This example will limit the content to 200 characters and appends “…” at the end.
{% LimitLength(Object.BlogPostSummary, 200, "...") #%}
This example will limit the content to 200 characters but preserves the last word in the string.
{% LimitLength(Object.BlogPostSummary, 200, "...", true) #%}
Check for Empty Value
This example checks if the Image field is empty, if not displays the Image.
{% if (!string.IsNullOrWhiteSpace(Image)) { return "<img class=\"img-responsive\" src=\"" + Image + "\"/>" } |(identity)GlobalAdministrator%}
References
For more info on how to use transformations please see Best Practices for Working with Transformations in Kentico