New features Please use http://kentico.uservoice.com as the primary place to submit your suggestions and allow others to vote for your ideas!
Version 4.x > New features > LimitLength transformation View modes: 
User avatar
Guest
Gabriëlle - 2/8/2009 11:51:10 AM
   
LimitLength transformation
Hi there,

Just discovered the new transformationexample LimitLength.

I'm very happy with this new feature...but it would be nice if this functions doesn't break up the words. So, instead of maxcharacters..is it possible to set the maximum of words and how do I accomplish that?

Thanks in advance...regards,

Gabriëlle

User avatar
Guest
Gabriëlle - 2/8/2009 12:07:18 PM
   
RE:LimitLength transformation
Sorry for the extra post, something went wrong in my web browser.

User avatar
Kentico Developer
Kentico Developer
kentico_ondrejv - 2/11/2009 9:55:54 AM
   
RE:LimitLength transformation
Hello Gabrielle,

You may need to develop your custom function which you will use in transformation. Please place it to: '~\App_Code\Functions.cs'

You can find more info in our Developer's Guide:
http://devnet.kentico.com/docs/devguide/adding_custom_functions_to_tra.htm

Best regards
Ondrej Vasil

User avatar
Guest
Gabriëlle - 2/11/2009 2:28:51 PM
   
RE:LimitLength transformation
Hi Ondrej,

OK thank you, I've searched the entire internet and finally found a solution. For other users:

Make a new function called TrimWords.cs

Enter this code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.GlobalHelper;
using CMS.FileManager;
using CMS.CMSHelper;

/// <summary>
/// Summary description for TrimWords
/// </summary>
public static class TrimWords
{
public static String LimitWords(String Input)
{
// split input string into words (max 21...last words go in last element)
String[] Words = Input.Split(new char[] { ' ' }, 21);

// if we reach maximum words, replace last words with elipse
if (Words.Length == 21)
Words[20] = "...";
else
return Input; // nothing to do

// build new output string
String Output = String.Join(" ", Words);
return Output;
}
}

Enter this in your transformation:

<%# TrimWords.LimitWords(Eval("ColumnName").ToString())%>

Et Voila.

User avatar
Kentico Developer
Kentico Developer
kentico_ondrejv - 2/12/2009 4:57:11 AM
   
RE:LimitLength transformation
Hello Viola,

Thank you for your solution. Since it's very useful function I'm sure that other users will appreciate it.

Best regards
Ondrej Vasil

User avatar
Member
Member
sasdaman - 5/4/2009 12:35:55 PM
   
RE:LimitLength transformation
Voila,

Will this work with editable text web part? So the syntax would be:

<%# TrimWords.LimitWords(Eval("EditableText3").ToString())%>


Any feedback would be greatly appreciated.

Many thanks,

Sahus Pilwal




User avatar
Member
Member
Gabriëlle - 5/4/2009 2:09:40 PM
   
RE:LimitLength transformation
Hi Sahus,

If I understand your question correctly, then I think this is not possible. The transformation needs a field from a document type and a webpart doesn't have fields like this.

However I'm not fully sure about this.

Also please note that if you want to use this transformation for a HTML encoded field and you have some HTML codes in your text, the transformation will destroy your layout if the last word is between a start and an endtag. For example <b>blabla</b>.

Regards,

Gabriëlle

User avatar
Kentico Developer
Kentico Developer
kentico_martind - 5/18/2009 5:01:09 AM
   
RE:LimitLength transformation
Hi Sahus,

you can use following sample code to get content of editable region in transformation:

<%# EditableItems["IdOfEditableTextWebPart"] %>

So you can trim the content using following code:

<%# TrimWords.LimitWords(EditableItems["IdOfEditableTextWebPart"]) %>

But as Gabriëlle already mentioned, it will trim the whole HTML code, not just actual text.

Best Regards,

Martin Dobsicek