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:
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 %}
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
%}
4. Lorem Ipsum
Version 6.0 brings you the "Lorem Ipsum" macro! Simply use the following syntax to generate the Lorem ipsum text:
{%LoremIpsum%}
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%}
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!
Here is a quick tip that is useful if you work a lot with widgets in Kentico.
You might know that macros are not allowed in the widgets editing form because of security reasons. Luckily for us developers there is a nice little workaround to enable macro resolving in widgets:
Add the macro parameter as default value for the field and make sure to uncheck "Display attribute in the editing form".
In the screenshot below you can see an example of this where the title querystring is used in the news lists where condition.
Sometimes we get the request to create a menu item that works as a signout button. There are of course several ways to do this. For instance you could create a web part/widget and place it on the page which automatically signs the user out. In this post I will show you a different approach using a custom signout macro.