Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Steve Brown 125 posts 290 karma points
    Sep 10, 2013 @ 15:07
    Steve Brown
    0

    convert mvc template to razor macro

    There is a mvc template in the ubootstrap project that creates a menu structure I'm looking for, from here

    http://our.umbraco.org/projects/starter-kits/ubootstrap/help-using-ubootstrap/38557-Umbraco-6-coupled-with-uBootstrap?p=0

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext

    @{  var root = Model.AncestorOrSelf(); }
       
    <ul class="nav">
       
    @{ var homeSelected = Model.Level == 1 ? " class=\"active\"" : string.Empty; }
       
    <li @Html.Raw(homeSelected)><a href="@root.Url">@root.Name</a></li>
       
    @foreach (var item in root.Children.Where("Visible"))
       
    {
           
    var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"active\"" : string.Empty;
           
    var subItems = item.Descendants("Textpage").Where("Visible");
           
    if (subItems.Count() > 0)
           
    {
               
    <li class="dropdown">
               
    <a href="@item.Url" class="dropdown-toggle" data-toggle="dropdown">@item.Name</a>
                <ul class="dropdown-menu">
                @foreach (var subItem in subItems)
                {
                    <li><a href="@subItem.Url">@subItem.Name</
    a></li>
               
    }
               
    </ul>
                </
    li>
           
    }
           
    else
           
    {
               
    <li @Html.Raw(selected)><a href="@item.Url">@item.Name</a></li>
           
    }
       
    }
       
    </ul>

    However I only want to insert the menu on certain pages in my site, so my thinking is it might be easier to convert this to a razor macro. How would I go about doing that?

  • Steve Brown 125 posts 290 karma points
    Sep 10, 2013 @ 21:09
    Steve Brown
    0

    Specifically, to create a razor macro, I think it needs @inherits Umbraco.Web.Macros.PartialViewMacroPage

    But when I get rid of the inherits DynamicNodeContext it is no longer is able to query the content nodes

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 11, 2013 @ 00:14
    Jeavon Leopold
    0

    Hi Steve,

    A little confused, the snippet you posted is from a non-Mcv Razor Macro, are you wanting to convert it to a Mvc Razor Macro Partial or just a standard Partial View or am I miss understanding?

    Jeavon

  • Steve Brown 125 posts 290 karma points
    Sep 16, 2013 @ 17:00
    Steve Brown
    0

    I want to convert it to a razor macro partial. Sorry I misspoke earlier. Thanks for the reply

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 17, 2013 @ 10:55
    Jeavon Leopold
    100

    Ok, a Partial View Macro version of this would be:

    <ul class="nav">
        @{
            var root = Model.Content.AncestorOrSelf();
            var homeSelected = Model.Content.Level == 1 ? " class=\"active\"" : string.Empty;    
        }
        <li @Html.Raw(homeSelected)><a href="@root.Url">@root.Name</a></li>
        @foreach (var item in root.Children.Where(x => x.IsVisible()))
        {
            var selected = Array.IndexOf(Model.Content.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"active\"" : string.Empty;
            var subItems = item.Descendants().Where(x => x.IsVisible() && x.DocumentTypeAlias == "Textpage");
            if (subItems.Any())
            {
                <li class="dropdown">
                <a href="@item.Url" class="dropdown-toggle" data-toggle="dropdown">@item.Name</a>
                <ul class="dropdown-menu">
                    @foreach (var subItem in subItems)
                    {
                        <li><a href="@subItem.Url">@subItem.Name</a></li>
                    }
                </ul>
                </li>
            }
            else
            {
                <li @Html.Raw(selected)><a href="@item.Url">@item.Name</a></li>
            }
        }
    </ul>
    
  • Steve Brown 125 posts 290 karma points
    Sep 17, 2013 @ 17:38
    Steve Brown
    0

    Jeavon, this is awesome, thank you. I'm new to razor/mvc and really unclear still on the differences between the syntax in templates and macro partial views. Is there a good resource with that kind of info? Thanks again for your response, it works great.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 17, 2013 @ 17:54
    Jeavon Leopold
    1

    Hi Steve,

    No worries, let me try to explain as there is some overlap which complicates things a bit.

    There are two template modes for Umbraco, WebForms or Mvc.

    Historically macros, including "Razor Macros" can be rendered within WebForms by the Macro Engine allowing you to script some logic into your output.

    When using Mvc for templating, generally you don't need Macros as you have Partial views etc... There are few exceptions though, for example inserting a Macro in the RTE, which is why the Macro Partial View exists, whereby you can still use a Macro but the syntax is Mvc.

    So in Razor Macros you have a dynamic model which you accessed like Model.property in a Mvc template the dynamic model is accessed from CurrentPage, e.g. CurrentPage.property Additionally in Mvc we have a strongly typed model which I used in the above, this is accessed from Model.Content, e.g. Model.Content.GetPropertyValue("property")

    Hope that is a helpful start, take a look at the Mvc documentation here and also the cheatsheets here

    Thanks,

    Jeavon

  • Steve Brown 125 posts 290 karma points
    Sep 17, 2013 @ 20:04
    Steve Brown
    0

    Jeavon, thanks again, that really helps clear things up.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 17, 2013 @ 21:13
    Jeavon Leopold
    0

    Great! I guess should have mentioned that you can of course now use a Partial View Macro in a WebForm so that is using the new IPublishedContent Mvc rendering in a MasterPage through the Macro Engine! I think this is the future of WebForms support in Umbraco rather than the Razor Macros, but generally using all Mvc templating is the main direction to be headed :-)

Please Sign in or register to post replies

Write your reply to:

Draft