Copied to clipboard

Flag this post as spam?

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


  • Laurence Gillian 600 posts 1219 karma points
    Jun 28, 2017 @ 10:54
    Laurence Gillian
    0

    Umbraco .Where("visible")

    So the idea is that this can be used to pull page the visible pages, or nodes... e.g. the ones that don't have umbracoNaviHide set.

    However, it seems to also bring back nodes which don't have a template?

    I've tried:

    x => x.TemplateId != -1
    x => x.TemplateId > 1000

    But I've got a pesky page, that once had a template, that keeps coming back, dang!

    So, what is the best way to filter out the nodes which do not have a template?

    @foreach (var item in nodes.Where("Visible").Where(x => x.TemplateId != -1))
    {
        <li class="@(item.IsAncestorOrSelf(Model.Content) ? "active" : null)">
            <a href="@item.Url">@item.Name</a>
         </li>
     }
    

    Ideas!

    Also if Umbraco is going to have this magic .Where('visible') which abstracts umbracoNaviHide perhaps it should also exclude pages with out templates (e.g. no URLs).

    Over and out! Laurie

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jun 28, 2017 @ 11:35
    Alex Skrypnyk
    0

    Hi Laurie

    Your code doesn't work because you are using dynamic types, can you use strongly typed?

    It works with strongly typed nodes collections - https://our.umbraco.org/documentation/Reference/Querying/IPublishedContent/Collections#filtering-ordering-extensions

    Thanks,

    Alex

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jun 28, 2017 @ 12:51
    Dan Diplo
    1

    If you're familiar with C# extension methods then a nice way to do this is to create one like this:

    public static bool HasTemplate(this IPublishedContent content)
    {
        return content.TemplateId > 0;
    }
    

    Then, I'd suggest using typed queries (like Alex mentions). You can then do stuff like this:

    @foreach (var item in nodes.Where(x => x.IsVisible() &&  x.HasTemplate())
    {
    }
    

    You can even add another extension method to make it neater:

    public static bool IsInListing(this IPublishedContent content)
    {
       return this.IsVisible() && this.HasTemplate();
    }
    

    Then you just go:

    @foreach (var item in nodes.Where(x => x.IsInListing())
    {
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft