Umbraco MVC: Is the macro as useful in V6 with Partials?

Following on from my previous blog post on Umbraco MVC, where we got MVC setup in Umbraco and created nested templates. In this next post in this series, I will talk about adding dynamic functionality to your website such as a simple navigation and discuss that perhaps that the Umbraco Macro is no longer needed as much as we have used it before. So carry on reading to find out why that the Umbraco macro is perhaps on it last legs in my opinion.

What is a Partial?

A partial you can think of as a small piece of functionality, such as navigations, blog post comments for example. If you are familiar with Umbraco macros then mostly all of these would be applicable as a partial. This video on the ASP.NET MVC website covers the basics of Partial Views

Why use Partials?

The purpose of using partials is to allow you to create functionality in contained files away from your main template views. It is possible to write Razor and logic directly into your templates, however I would not recommend it as it is not the correct approach to MVC. As the MVC approach is about separation of concerns, by separating your logic away from your markup for example. If you have worked with Umbraco macros before by creating, XSLT, .NET Usercontrols or the older style of Razor MacroScripts then these would all be valid examples of when to use a partial view.

Show me the code!

In this example I will show you how to create a very simple Partial View for a navigation for your site. In your Umbraco website create a new file called navi.cshtml in the /Views/Partials folder. In here we can write our code that will display our navigation for us.

@model IPublishedContent

@{
    //Model is our current page in this example
    
    //From the currentpage - get the root node (Select the node at level 1)
    var rootNode = Model.AncestorsOrSelf(1).FirstOrDefault();
    
    //Get the top level children for our navi (only one level under homepage)
    //Select pages where they are visible (umbracoNaviHide = false) AND the document type alias is NOT CarouselFolder
    var pages = rootNode.Children.Where(x => x.IsVisible() && x.DocumentTypeAlias != "CarouselFolder");
}

@if (pages.Any())
{
    <ul>
        <!-- Loop over the pages for our navigation -->
        @foreach (var page in pages)
        {
            <li>
                <a href="@page.Url">@page.Name</a>
            </li>
        }
    </ul>
}

The next part is to reference our partial view in our main master template so the navigation is visible on all pages of our website.

//Insert our navigation into our master template,
//by passing the Current Page as the model to our partial view
@Html.Partial("navi", Model.Content)

The wonderful guys of the Umbraco core team have written some great documentation on Partial Views in Umbraco when using MVC.

So why are Macros on the way out?

OK as before this is my opinion and you may or may not agree with me, but keep in mind it is my opinion. Now it’s possible to insert macro type functionality such as Navigations as in our example without the need of a macro, there is no real need for macros except in a certain few cases. But I may hear you cry what about caching, surely I need to use a macro if I want the contents to be cached and the answer is no you don’t as there is a handy helper method available to us, to render out Cached Partial Views.

//Cache MyPartialName passing through pageModel and caching the result for an hour (3600 secs)
@Html.CachedPartial("MyPartialName", pageModel, 3600)

//Here are all the options available on the extension method CachedPartial
IHtmlString CachedPartial(
    string partialViewName, 
    object model, 
    int cachedSeconds,
    bool cacheByPage = false,
    bool cacheByMember = false,
    ViewDataDictionary viewData = null)

The only real reason that I will see you using Umbraco macros is that you need to have the partial to be inserted inside the WYWISYG Rich Text Editor. Apart from that in my eyes the Umbraco Macro has no other use, but I may be wrong.

Next time we start to look at controllers & surface controllers and what they are used for.

Cheers,
Warren 🙂

Advertisements

10 thoughts on “Umbraco MVC: Is the macro as useful in V6 with Partials?

  1. Don’t think macro’s will go away anytime soon as otherwise you don’t have options to cache based on params.
    Yes, you now have a helper CachedPartial, but that will cache the first ever rendering of the partial, which may our result in undesirable behaviour.

    For example, you may want to add a css class “active” to the current page in your navigation, but still make use of caching.

    If you’d use a CachedPartial, you’d run into troubles as first rendering of the partial will be rendered during caching time.
    So, you’re active page would remain the same!

    Cheers,
    /Dirk

  2. Macros still have legitimate use in Umbraco especially as you mention from an editors perspective. In particular there are certain activities that an editor cannot do without a Macro(-partial) such as inserting an image gallery in the flow of the content, or inserting pull quote style content into the RTE.

    Macros will still be a massive part of Umbraco especially from a non technical web designer/developer perspective where the concepts of MVC etc are not initially as intuitive, macros are easy to grasp from a wireup perspective as it is all point click and caching is all just inbox settings and there are templates to help them get started. There just isn’t quite the same level of UI for partials at this stage.

  3. Some good points there @dirk & @peter and it’s nice to have a discussion and see the flip side of this. I personally don’t develop as many sites that tend to use Macros in the RTE.

    Dirk the same could be said if I was to wrap this navigation in a macro as well. So to cache a navigation is probably a bad example on my part.

    1. Yes, agree, navigation maybe wasn’t the best example. But than again, if you didn’t use this example, we probably wouldn’t have this discussion.
      That being said, I still disagree. Sure, you’ll have to think ahead of what can or should be cached. And most importantly, choose the correct paramaters based on that discussion. It can make a huge difference if you choose parameters wisely. And disastrous if you don’t (Don’t tell, been there done that)

  4. You can still cache Navigation macros without loss of selected classes if ‘cache by page’ is checked. Partials are great, but from a website-administrator perspective its great to be able to set or tweak macro caching in the back office without having to open up any templates and start wading through code.

  5. We allow content editors to construct a page using the MacroContainer datatype and allowed macros, and params… not sure if there’s any out of the box functionality in mvc/umbraco that would allow that to be accomplished with partials?

Leave a Reply

Fill in your details below or click an icon to log in:

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