by Keyvan Nayyeri via Keyvan Nayyeri on 12/8/2006 6:10:07 PM
TextToken is another feature in Community Server and has special implementations in some applications. For example TextParts (which are now renamed to Snippets in Community Server 2.1) are their implementation in weblog application. I can remember this feature in .Text, too.
If you want to know what they do, they let you to use some text keys and corresponding values in your application or specific sections. When you put one of these key texts in your post bodies, they will be replaced with their corresponding value. This saves you from typing a long texts and helps you to automatically put a link for a text without typing it for each insertion.
In this CS Dev Guide post I want to talk about TextTokens in Community Server APIs.
CommunityServer.Components.TextToken class and static methods in CommunityServer.Components.TextTokens are main APIs to work with TextTokens.
TextToken class provides six properties:
This class also provides a FormatText() function which lets you to format any string value (body of your posts) based on TextToken values. It returns a formatted text.
Static methods in CommunityServer.Components.TextTokens namespace let you to get TextTokens for a section as well as update, add or delete a TextToken object. There is also a Format() method which gets an ITextToken and a string value and formats this text based on TextToken then returns the formatted text.
Let's discover some examples.
GetFormattedBody() function in below code returns the formatted body of current post. It gets all TextTokens for current section and formats post body using these TextTokens.
String GetFormattedBody()
{
CSContext context = CSContext.Current;
String formattedBody = context.Post.Body;
ArrayList tokens = TextTokens.GetTokens(context.SectionID);
foreach (TextToken token in tokens)
formattedBody = token.FormatText(formattedBody);
}
return formattedBody;
In second example UpdateAllTokens() method updates all tokens for current section and adds a constant link to all tokens.
void UpdateAllTokens()
token.Link = "http://nayyeri.net";
TextTokens.Update(token);
And finally in the last example AddNewToken() method adds a new token to current section.
void AddNewToken()
TextToken textToken = new TextToken();
textToken.Token = "CS";
textToken.Text = "Community Server";
textToken.SectionID = context.SectionID;
textToken.Link = "http://communityserver.org";
TextTokens.Add(textToken);
In near future I'll write about one of common implementations of TextTokens in weblog application, named TextPart (Snippet).
Original Post: CS Dev Guide: TextTokens
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.