Copied to clipboard

Flag this post as spam?

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


  • pronto 61 posts 172 karma points
    May 09, 2014 @ 16:06
    pronto
    0

    Filter nodes by template alias in Umbraco 7

    Hi All,

    I'm trying to filter some nodes by their template and I'm having trouble getting this to work with Umbraco 7.1.1 (or in general, a Partial View Macro).

    Usually I would iterate over my nodes using something like this:

    @foreach(var item in Model.Children.Where("Template > 0")){
        @item.Name
    }

    Is there an equivalent example in Umbraco 7.1.1?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    May 09, 2014 @ 17:56
    Dennis Aaen
    1

    Hi pronto,

    When you´re in a Partial View Macro or a Partial View the currentpage that you're a on is represented by CurrentPage, and not by Model. If you´re usig strongly typed razor the currentpage is represented by Model.Content.

    The documentation for CurrentPage in a Partial View Macro or a Partial View can you find here:

    http://our.umbraco.org/documentation/Reference/Templating/Mvc/views

    Strongly typed version

    http://our.umbraco.org/documentation/Reference/Templating/Mvc/partial-views

    So to get back to your case, try sometthing like this:

    @foreach(var item in CurrentPage.Children.Where(NodeTypeAlias == "TemplateAlias")){
       
    @item.Name
    }

    Where you need to change the "TemplateAlias" to the alias of document types that you want to filter on in your collection of pages.

    Hope this helps,

    /Dennis

  • pronto 61 posts 172 karma points
    May 09, 2014 @ 18:02
    pronto
    0

    Hi Dennis,

    Thanks for the info, I have been looking for something like this!

    Is there a way to filter by the Template and not the NodeTypeAlias? For example, some of my nodes dont have templates!

    pronto

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    May 09, 2014 @ 18:20
    Ali Sheikh Taheri
    0

    Hi Pronto

    If you just want to check if there is any template or not :

    CurrentPage.Children.Where(x => x.TemplateId > 0)

    and if you are looking for specific template then you could do this

    CurrentPage.Children.Where(x => x.GetTemplateAlias() == "TemplateAlias"> 0)

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    May 09, 2014 @ 18:27
    Dennis Aaen
    0

    Hi pronto,

    I think one way that you could do it is by filter by the Template id.

    @foreach(var item in CurrentPage.Children.Where(Template == "1054")){
       
    @item.Name
    }

    You can find the template id in the umbraco.config file, that holds the XML of the content. The file is located in: /umbraco/App_Data/umbraco.config

    Hope this could be a solution to you.

    /Dennis

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Sep 03, 2014 @ 11:32
    Dan Diplo
    1

    Incase this helps anyone else...

    You can get the template alias (and other template details) of a node using the FileService service eg.

    var template = ApplicationContext.Current.Services.FileService.GetTemplate(Model.Content.TemplateId);
    string alias = template.Alias;

     

     

     

     

Please Sign in or register to post replies

Write your reply to:

Draft