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 }
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:
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:
Is there an equivalent example in Umbraco 7.1.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:
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
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
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)
Hi pronto,
I think one way that you could do it is by filter by the Template id.
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
Incase this helps anyone else...
You can get the template alias (and other template details) of a node using the FileService service eg.
is working on a reply...