Razor Inline helpers in Umbraco Juno (Full sitemap sample)

Now inline helpers works in Umbraco Juno

Edit : There’s more discussions and samples of Razor in http://our.umbraco.org/forum Like for example in this thread. with more approaches to the sitemap and filtering of nodes.

Edit: Umbraco 4.7 syntax
Here’s one way of creating a Razor sitemap sample:

<h1>Full sitemap</h1>
@traverse(Model.AncestorOrSelf(1))

@helper traverse(dynamic node)
{
 var cc = node.Children.Where("umbracoNaviHide!=true");                        
 if (cc.Count()>0)
 {
  <ul>
  @foreach (var c in cc)
  {
  <li>
   <a href="@c.Url">@c.Name</a>
   @traverse(c)
  </li>
  }
  </ul>
 }
}

Result

Simple website 
  Installing modules 
  Go further 
  Getting started 

The Razor renderer makes a good job dealing with the mix of C# code and HTML. It’s however not always so easy to know exactly what’s considered as either one and the compiler might complain because of that. In those cases it’s often good to use @{ } to make it clear you’re doing C#. That’s what I needed in my sample – even though the code is in a helper function and should not need it (I thought).

Test your Razor macros

Macro engine macros does not have the nice Visualizer as Xslt ones has. One way to test your macro is to simply call it from a template. That can be a bit tedious to go in and change that template for each new macro you like to test. I have previously made a combo of a template with just a call to a generic macro that gets a macro name from the querystring and renders it.

It’s packaged as “Rndr url macro renderer” and it’s now using a Razor macro (instead of a Python one) as the controlling macro. With it you can easy test your macros by calling them this way:

http://www.mysite.com/rndr?macroalias=mymacro&pageid=1063

http://our.umbraco.org/projects/website-utilities/rndr-url-macro-renderer

Advertisements

2 thoughts on “Razor Inline helpers in Umbraco Juno (Full sitemap sample)

  1. What about security? Can anyone use this link, or it must be logged to backend?

    And is it possible to test xslt macros too?

    Thanks

    • Hi!

      Yes, as it’s using Umbraco macro renderer. No built in security – but easy to add some basic protection as it’s razor c# code. For me – I’m only using it to test my macros on dev sites.

      Regards
      Jonas

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s