Umbraco Contour and Razor
Want to output Contour records with razor instead of xslt well I’ve created some dynamic objects you can use to do so (please note that this is just a primary version of the dynamic objects, this will be improved in a next Contour release)
Here is an example:
@using Contour.Addons.DynamicObjects
<ul id="comments">
@foreach (dynamic record in Library
.GetApprovedRecordsFromPage(@Model.Id).OrderBy("Created"))
{
<li>
@record.Created.ToString("dd MMMM yyy")
@if(string.IsNullOrEmpty(record.Website)){
<strong>@record.Name</strong>
}
else{
<strong>
<a href="@record.Website" target="_blank">@record.Name</a>
</strong>
}
<span>said</span>
<p>@record.Comment</p>
</li>
}
</ul>
This example uses the comment form created from the comment form template and then simply outputs all comments submitted on the current page. To access the field values you can simply use the dot notation (.FieldCaption like @record.Comment)
Download the assembly here: (simply unzip and drop in your bin directory)
Make sure you are running at least version 4.7 of umbraco since it won’t work on older version and of course have Contour installed.
Cool! So much cleaner than using XSLT. I’ve also written a workflow so that you can send a razor transformed email instead of using XSLT. Let me know what you think.
http://our.umbraco.org/projects/backoffice-extensions/send-razor-transformed-email-contour-workflow
@Micheal yup I’ve seen that, great work!
I’ve noticed a lot of use of dynamic objects in razor macros. Is there any specific reason for this? Strongly-typed objects work just fine in most (all?) cases.
I’m very curious as to why those in the Umbraco camp have embraced this practice so readily.
@Chris, since the field on a form will depend on how you design the form. So by using a dynamic we can support a dot notation for getting a field value like record.mycustomfield instead of record.fields[”mycustomfield”].value
Oh, record.fields[”mycustomfdield”].value would be a great addition (for field captions with non-english chars, and sometimes with spaces). Is it already in there somewhere, or is that something you could add?
Thanks
Jonas
Aha, this worked fine for me:
record.GetField[”my custom field with strange name”].Values[0]
sorry, should be record.GetField(”my custom field…”).Values[0]