This project is read-only.

Extremely simple widget

Topics: General, Writing modules
Aug 30, 2012 at 3:24 PM
Edited Aug 30, 2012 at 3:24 PM

I need to show some HTML and some general data available on the server (like the current date/time). No need for any kind of administrator edits (apart from the usual widget handling, like moving across layers). What would be the most straightforward way to implement this?


I tried to create a driver with only Display method, but how do I put my HTML in the DriverResult?

Aug 30, 2012 at 3:46 PM

No need for a driver or a part for that matter. Just create a template in the root of your views folder, say CurrentDateTime.cshtml.
Next, from your Layout.cshtml file (or any other place, like a filter), add an ad hoc shape to any zone you want, say the AsideSecond zone:


Orchard will see that you have indeed a template defined for the CurrentDateTime shape, so it will hapilly execute it.
To make it more interesting, try not to use DateTime.Now from your CurrentDateTime.cshtml, but instead create a class that implements IShapeTableProvider. In that class, describe the "CurrentDateTime" shape, and set a property on the shape called Now. Retrieve that the current date from an injected IClock.

Although this is relatively a lot of work for just displaying the current time, but next time when you need more data on your shape, you will know how to do it without resolving services from your template.

Aug 30, 2012 at 4:06 PM

Thanks, sfmskywalker.


Got this working and it will be very helpful in the future. But what I really need is something outside of my theme, so I can easily use it on multiple sites. And most importantly admins have to be able to move it around zones.

Aug 30, 2012 at 4:07 PM

In that case, I think you do need a widget :)

Aug 30, 2012 at 4:10 PM

OK, how do I make a simple widget with no persistence and no admin editing?

Aug 30, 2012 at 4:18 PM
Edited Aug 30, 2012 at 4:19 PM

Follow all the steps that are in the docs describing to create widget, with the difference that your content part does not derive from ContentPart<TRecord>; instead, just derive it from ContentPart. This also means that you don't need to write a content handler to add a StorageFilter. In your driver, the only method you need to implement is Display(), since you don't need the admin to edit anything (exept for the position and layer, but that part is managed by the WidgetPart).

Sep 2, 2012 at 6:21 AM


@sfmskywalker thank you!

Sep 3, 2012 at 5:59 PM

You're welcome.

I'd like to add that as of 1.5, you don't even have to write a single line of code to define a widget - all that is require is the following:

  1. Create a new content type, e.g. MySimpleWidget
  2. Add the WidgetPart and any other parts that you want to use.
  3. Set the StereoType to "Widget"


Sep 5, 2012 at 11:07 AM

Hm, I don't get WidgetPart, this are the available options on Add Part page:


 Admin Menu
 Publish Later


Just setting the stereotype results in exception, when trying to add a widget to a layer.

Sep 12, 2012 at 11:21 PM

Please file a bug. The widget part should be there.

Sep 13, 2012 at 11:31 AM

OK, thanks guys.


Here's the bug: