Copied to clipboard

Flag this post as spam?

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


  • Murray Roke 467 posts 875 karma points c-trib
    Oct 19, 2011 @ 06:02
    Murray Roke
    0

    In need of Razor syntactic sugar to help with 'is published' checking.

    Hi all

    Here's a pretty common scenario we face, and I'm wanting a way to make this DRYer. 

    How can I separate out all the logic above the 'HTML HERE' line into something re-usable?

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
        if (Model.HasProperty("relatedPages") && Model.HasValue("relatedPages")) 
        {
            List publishedRelatedPages = new List();
            foreach (var nodeId in @Model.RelatedPages)
            {
                var page = Model.NodeById(nodeId.InnerText);
                if (page.Id != 0)
                {
                    publishedRelatedPages.Add(page);
                }
            }
            if(publishedRelatedPages.Count > 0)
            {
                @* ---------- HTML HERE ---------- *@
                <h2>Related Pages</h2>
                <ul>
                    @foreach (var page in publishedRelatedPages)
                    {
                        <li>
                            <a href="http://mce_host/forum/developers/razor/@page.NiceUrl">@page.Position(). @page.Name</a>
                        </li>
                    }
                </ul>
            }
        }
    }
    
  • Dirk De Grave 4537 posts 6006 karma points MVP 3x admin c-trib
    Oct 19, 2011 @ 09:35
    Dirk De Grave
    0

    Hi Murray,

    How about creating a @helper method, which can be put in App_code folder and be used on any of your Razor files? Or an extension method on the Model that returns a DynamicNodeList, probably taking your propery alias as additional parameter?

     

    Hope this helps.

    Regards,

    /Dirk

  • Murray Roke 467 posts 875 karma points c-trib
    Oct 19, 2011 @ 23:58
    Murray Roke
    0

    BTW: related pages is uComponents MNTP saving in XML format.

    @Dirk unfortunately you can't have extension methods on dynamic objects, I tried that.

    However I just tried the obvious solution again and this time it works, I don't know If I failed last time or because we updated to latest macroEngine, but here it is, moving it into a static helper class:

    using System.Collections.Generic;
    using System.Web;
    using umbraco.MacroEngines.Library;
    
    namespace Terabyte.Umbraco.Extensions
    {
        public static class DynamicNodeExtensions
        {
            public static IEnumerable<dynamic> GetPublishedNodes(dynamic property)
            {
                foreach (var nodeId in property)
                {
                    var page = (new RazorLibraryCore(null)).NodeById(nodeId.InnerText);
                    if (page.Id != 0)
                    {
                        yield return page;
                    }
                }
            }
        }
    }
    
    

    and the razor:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using Terabyte.Umbraco.Extensions;
    @{
        IEnumerable<dynamic> pages = DynamicNodeExtensions.GetPublishedNodes(@Model.RelatedPages);
        if (pages.Any())
        {
            <h2>Related Pages</h2>
            <ul>
                @foreach (var page in pages)
                {
                    <li>
                        <a href="@page.NiceUrl">@page.Name
                    </li>
                }
            </ul>
        }
    }
    

    Improvements welcome

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies