7 YEARS AGO

Sneak peek at K#

With the 6.0 beta being out for a couple of days, I thought it was time to take a better look at some of the new features. One feature that I am really looking forward to use is the new K# engine. In this post I will cover a couple of examples using K#.

With the 6.0 beta being out for a couple of days, I thought it was time to take a better look at some of the new features. One feature that I am really looking forward to use is the new K# engine. In this post I will cover a couple of examples using K#.

1. Methods

The newly improved Macro editor not only displays a list of available Macros, but it also shows you a list of methods that you can use. Take for example the GetUserFullName method that takes a User's ID and returns the User's Full Name:

{% GetUserFullName(CurrentUser.UserID) %} 

The macro and it's result are illustrated in the following screenshot:

ksharp_example_1.png

2. foreach loop

K# supports some well known commands like if statements, for loops, while loops and foreach loops. In the example below I loop through all the children of the document root and output every documents name (MenuItemName).

{% ret = ""; foreach(x in RootDocument.Children) { ret += x.MenuItemName} ret %}

ksharp_example_2.png

3. Comments

You can add some comments to your code similar to C#:

{%
   ret = "";

   // Loop through the children
   foreach(x in RootDocument.Children)
   { 
      // Add the MenuItemName
      ret += x.MenuItemName
   } 

   ret
%}


ksharp_example_3.png

4. Lorem Ipsum

Version 6.0 brings you the "Lorem Ipsum" macro! Simply use the following syntax to generate the Lorem ipsum text:

{%LoremIpsum%}

ksharp_example_4.png

5. Transformations

It will be possible to use Macros in transformations if you choose a Macro supporting transformation type like HTML. Known syntaxes like the following will be possible without additional code (CMS.CMSHelper.CMSContext.CurrentResolver.ResolveMacros(string inputText)):

{%NewsTitle%} 

ksharp_example_5.png

That concludes the sneak peek at K#. I hope these examples will give you an idea of what's to come and I am looking forward to provide you with more examples once I can use K# in practise. K# is definitely in my top 3 list of best 6.0 features!