Razor support in Umbraco 4.7
With Umbraco 4.6.1 in Jan,2011, they brought in one of the great feature to Umbraco CMS - Razor. And with Umbraco 4.7 in March, 2011 they have matured the made it more usable, reliable and stable.
Razor is the view engine introduced by Microsoft team with ASP.Net MVC 3.0. It is optimized around HTML generation using a code-focused templating approach. And now Umbraco 4.7 provides excellent support for writing Razor scripts to create macros in umbraco. For those guys, including me, who dislike XSLTs, this is a great alternate for them. Now we can write the logic in C# or VB rather then in XSLT. Thanks Umbraco Team!!!.
In one of my pervious post, I mentioned about using Razor scripts in Umbraco 4.6.1. You can consider this as an extension to that. In this post I have tried to describe what has changed from 4.6.1 to 4.7 and how to use Razors in Umbraco 4.7.
Upgrading from 4.6.1 to 4.7
(Please note that 4.7 requires .Net Frameowork 4.0)
- Copy following folders from 4.7RC download: /bin, /umbraco and /umbraco_client.
- Take care of web.config changes: Mainly there are 4 changes and they're clearly marked in the web.config in 4.7 as <!-- Added in Umbraco 4.6.2 –>.
- Delete following .dll files from old installation under /bin folder
IronPython.dll
IronPython.Modules.dll
IronRuby.dll
IronRuby.Libraries.dll
IronRuby.Libraries.Yaml.dll
Microsoft.Dynamic.dll
Microsoft.Scripting.dll
Microsoft.Scripting.Debugging.dll
Also delete following files if they exist. (in case if you have 4.7 beta)
RazorEngine.Core.dll
RazorEngine.Templates.dll
umbraco.MacroEngines.Legacy.dll - In 4.6.1, by default Razor scripts were saved in ~/python folder, where as in 4.7 default folder has changed to ~/macroScripts. So if you have any .razor files under ~/python, you need to move them to ~/macroScripts folder.
For general upgrade information follow the guide found at http://our.umbraco.org/wiki/install-and-setup/upgrading-an-umbraco-installation
And here is one important thread from Umbraco Installation Forum which might be helpful: http://our.umbraco.org/forum/getting-started/installing-umbraco/17890-XSLT-macros-not-getting-rendered-after-upgrading-to-47-RC
Changes in Umbraco 4.7
- As mentioned above, now with 4.7 all razor scripts are to be saved in ~/macroScripts folder.
- Now the new razor script files will have extension of .cshtml or .vbhtml. In 4.6.1 razor script files were having .razor extension and only C# was supported. Old .razor files will still work in 4.7 as far as they are in ~/macroScripts. But now new script files created will have extension .cshtml (for C#) and .vbhtml (for VB)
- Now you cannot use single-statement in control-flow statements without {} block. ie. Any thing under control flow statements like if, for, while, foreach need to be in between {} block. For example, the following is not allowed:
@if(isLoggedIn)
Hello, @user
Instead, wrap the contents of the block in "{}":
@if(isLoggedIn) {
Hello, @user
}
New Features in Umbraco 4.7
-
One of the Major new feature in 4.7 is support for Macro Parameters in razor scripts. Yes now razor script can read parameters passed by macros using @Parameter.nameOfParameter syntax.
-
For proper randering, document type properties with html contents in them needs to be wrapped in Html.Raw() ie. @Html.Raw(Model.bodyText). Leaving this wrapper off will cause HTML to be encoded and render to the screen.
-
IronPython and IronRuby is now not a part of the distribution and needs to be installed by unzipping the contents of the separate macroengine download. So if you are not using these Iron* scripts, you can remove ~/python folder.
-
For technical changes (Programmers perspective) please refer to the serise of blogs at umbraco.com: