Nibble

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)

Contour.Addons.DynamicObjects.dll.zip

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.

7 Comments so far

  1. Michael Lawrence on April 20th, 2011

    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

  2. Tim Geyssens on April 21st, 2011

    @Micheal yup I’ve seen that, great work!

  3. Chris on August 25th, 2011

    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.

  4. Tim Geyssens on August 26th, 2011

    @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

  5. Jonas Eriksson on September 9th, 2011

    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

  6. Jonas Eriksson on September 9th, 2011

    Aha, this worked fine for me:

    record.GetField[”my custom field with strange name”].Values[0]

  7. Jonas Eriksson on September 9th, 2011

    sorry, should be record.GetField(”my custom field…”).Values[0]

Leave a Reply