Code Razor cshtml Widget

Topics: Customizing Orchard, General, Writing modules
Jul 19, 2011 at 10:08 AM

I'm thinking or creating a widget similar to the Html widget, only instead of Html its accepts cshtml / Razor

The reason for this is that occasionally I need to create a link on a page that is dynamic i.e includes the users name in the query string.

Is this going to be possible? any tips on how to get the cshtml to load / compile on the fly?


<a href="@string.Format("{0}", HttpContext.Current.User.Identity.Name)">This is a example link</a>


Or... is their a better way to acheive what I'm trying to do?

Jul 20, 2011 at 12:24 AM

Piotr has a Razor thing that can do that. You might want to talk to him (it wasn't easy from what I understand).

Jul 20, 2011 at 1:22 PM
Edited Jul 20, 2011 at 1:23 PM

Hi, yes, I built a module exactly for that scenario (Razor templates):) It uses RazorEngine under the hood and allows you to do pretty much the same things (access model, helpers etc.) as in an ordinary .cshtml file. Bertrand's right - it was a really nasty thing to do (not looking like that at the beginning...). Most of the RazorEngine library had to be rewritten (leaving only the core compiler things) to be able to utilize Orchard goodies and mimic the behavior of Orchard .cshtml View files.

The first version of the module features one thing - TemplatePart, which looks like a BodyPart, but allows you to write Razor code. Current content item is available via Model property inside.

I will publish it in the Gallery in a day or two as I have to tweak some things yet. Unfortunately, I had to delay the publishing because of work (and time shortage...).

BTW - The best part is that after first-time compilation, templates work blazingly fast (~2ms to render even for pretty complex ones).

Jul 20, 2011 at 1:50 PM

Piotr, thanks for replying.

It looks like just what I'm after i just need set the part as a widget if you havent done it already.

Jul 20, 2011 at 2:49 PM

No problem. Sure, I'll add a templated widget to the module - it's around 4 lines of code:)

Jul 26, 2011 at 4:51 PM

When is this module coming out and what will it be called?

Jul 27, 2011 at 4:12 PM

I guess it'll be called Razor Templates or something similar. I'll try to publish it in a few days. It depends on whether the next Orchard release is coming soon or not. There are some breaking changes in the dev branch (namespace naming and some widely-used method signatures), which I work with, so in the current shape the module won't work ootb with Orchard 1.2.

If new Orchard release isn't going to arrive soon - I'll do some changes which will make the module 1.2-compatible and publish it. Otherwise - I'd like to wait until the official release, so the compatibility changes won't be necessary.

@Bertrand: When (if it's known by now, ofc) is the new release planned to be published?

Jul 27, 2011 at 8:16 PM

The dev branch is 2.0, don't worry about that. If there is a 1.3, it won't break you, I think.

Nov 4, 2011 at 7:49 AM

Hi to All,

Just wondering is this functionality implemented? I did a search in gallery with keyword Razor but found nothing similar.

What I need to use to get the real Media path in one of my Html widget in Azure without hard coding the blob storage path. I figured out that some code needed for this, so the Razor widget idea would be fit here.

Thx in advance

Nov 4, 2011 at 1:07 PM
Edited Nov 4, 2011 at 1:07 PM

Yes it is, but I have to add some finishing touches to it and write a short tutorial. I'll probably push it to the Gallery somewhere around the beginning of next week.

Have you tried leveraging Orchard.Tokens functionality? I guess it'd fit nicely in your scenario.

Nov 4, 2011 at 4:02 PM
Edited Nov 4, 2011 at 4:02 PM

Thx to answer, I will definitely will look for it.

About tokens: I read "Future Orchard Part 1: Introducing Tokens - Tales from the Evil Empire" part1, part2.

I just can not get how can I refer to an implemented token in a Html Widget? As I see all code were in csharp, and find no documentation for the integration link...

Nov 4, 2011 at 5:03 PM

You can't. HtmlWidget does not support tokens.