Developer forum

Forum » Templates » Nested loops - Item Types

Nested loops - Item Types

Adrian Ursu
Reply

Hi all,

I need to know if there is any way to list first level of items and inside that loop to list second level of items just by using the template associated to the Item List (in Item publisher).

Basically it is very similar with building a menu based on Item Types.

 

Also, I need to know if there is any way to list the items according to the order in menu and not by name.

Thanks,

Adrian

 

 

 

 

 

 


Replies

 
Mikkel Ricky
Reply
If you're trying to create a hierarchical structure (like a menu) based on item pages, you can just use a navigation for this (e.g. by using div.dwnavigation or Navigation.Render() in your template).

 

In the Item publisher settings you can set Order by to Sort value to sort the items by the order in the page tree.

 

Best regards,
Mikkel

 

 

 

 

 

  

 
Adrian Ursu
Reply

Hi,

It doesn't seem to render the dwnavigation tag no matter what combination of navigation xslt template and settings I am using.

I am using a html template for itemsList.

Thanks,

Adrian
 

 

 

 

 

 

 
Mikkel Ricky
Reply

Can you show us the template you're using and maybe a test site?

 
Adrian Ursu
Reply

Unfortunately I cannot give you a test site since it is a client's website and they have restricted access to the frontend.
But here is the template:

 

   <!--@If(ItemPublisher:Items.Any=True)-->

   <nav id="itemList" role="navigationAccordion">

        <ul class="dwnavigation" id="accordion" data-settings="startlevel:3;endlevel:5;template:NavSubpages.xslt;expandmode:all;"></ul>  

  </nav><!-- /subnav 

  <!--@LoopStart(ItemPublisher:Items.List)-->

  <h2 id="accordion-head<!--@ItemPublisher:Items.List.LoopCounter-->" class="accordion-head active"><!--@ItemPublisher:Item.Title--></h2>

  <div id="accordion-panel<!--@ItemPublisher:Items.List.LoopCounter-->" class="accordion-panel active">

     <div class="accordion-inner">

       <!--@If Not Defined(ItemPublisher:Items.CanEdit)-->

          <!--@ItemPublisher:Item.Description-->

       <!--@EndIf(ItemPublisher:Items.CanEdit)-->

       <ul class="items-list">

          <!--@LoopStart(ItemPublisher:Items.List)-->

          <!--@ItemPublisher:Items.List.LoopCounter--><!--@ItemPublisher:Item.Title-->

          <!--@LoopEnd(ItemPublisher:Items.List)-->         

                  <li class="items-list-row items-clear" onclick="location.href = '<!--@ItemPublisher:Item.DetailsUrl-->';">

                      <!--@If Not Defined(ItemPublisher:Items.CanEdit)-->

                      <!--@LoopStart(ItemPublisher:Item.Fields)-->

                      yyy<span class="items-list-cell<!--@If(ItemPublisher:Item.Field.First=True)--> items-list-cell-first<!--@EndIf--><!--@If(ItemPublisher:Item.Field.Last=True)--> items-list-cell-last<!--@EndIf-->"><!--@If Defined(ItemPublisher:Item.Field.Value)--><!--@ItemPublisher:Item.Field.Value--><!--@EndIf(ItemPublisher:Item.Field.Value)--><!--@If Not Defined(ItemPublisher:Item.Field.Value)-->&nbsp;<!--@EndIf(ItemPublisher:Item.Field.Value)--></span>

                      <!--@LoopEnd(ItemPublisher:Item.Fields)-->

                      <!--@EndIf(ItemPublisher:Items.CanEdit)-->

          

                      <!--@If Defined(ItemPublisher:Items.CanEdit)-->

                      <!--@LoopStart(ItemPublisher:Item.Fields)-->

                      <span class="items-list-cell<!--@If(ItemPublisher:Item.Field.First=True)--> items-list-cell-first<!--@EndIf-->"><!--@If Defined(ItemPublisher:Item.Field.Value)--><!--@ItemPublisher:Item.Field.Value--><!--@EndIf(ItemPublisher:Item.Field.Value)--><!--@If Not Defined(ItemPublisher:Item.Field.Value)-->&nbsp;<!--@EndIf(ItemPublisher:Item.Field.Value)--></span>

                      <!--@LoopEnd(ItemPublisher:Item.Fields)-->

                      <span class="items-list-cell items-list-cell-last"><a class="item-edit-link" href="<!--@ItemPublisher:Item.EditUrl-->"><!--@Translate(ItemPublisher:Item.Edit, "Edit")--></a></span>

                      <!--@EndIf(ItemPublisher:Items.CanEdit)-->

                  </li>

                  

          </ul>

       </div>

   </div>

   <!--@LoopEnd(ItemPublisher:Items.List)-->

 

    <!--@If(ItemPublisher:Items.Paging.TotalPages>1)-->

    <ul class="items-list-paging items-clear">

        <!--@LoopStart(ItemPublisher:Items.Paging.Pages5)-->

        

        <li class="items-list-page">

            <!--@If(ItemPublisher:Items.Paging.Pages5.Page.IsCurrent=True)-->

            <strong><!--@ItemPublisher:Items.Paging.Pages5.Page.Number--></strong>

            <!--@EndIf-->

            <!--@If(ItemPublisher:Items.Paging.Pages5.Page.IsCurrent<>True)-->

            <a href="<!--@ItemPublisher:Items.Paging.Pages5.Page.UrlHtmlEncoded-->"><!--@ItemPublisher:Items.Paging.Pages5.Page.Number--></a>

            <!--@EndIf-->

        </li>

        <!--@LoopEnd(ItemPublisher:Items.Paging.Pages5)-->

    </ul>

    <!--@EndIf-->

    <!--@EndIf-->

 

And it returns this:

 

        <ul class="dwnavigation" id="accordion" data-settings="startlevel:3;endlevel:5;template:NavSubpages.xslt;expandmode:all;"></ul>  

 

 

 
Mikkel Ricky
Reply
This post has been marked as an answer

You can only use <ul class="dwnavigation" …/> in layout templates. Sorry for not being clear on that.

In module templates you have to use @Navigation.Render() (see http://templates.dynamicweb-cms.com/Default.aspx?ID=14202).

 

 

Votes for this answer: 1
 
Adrian Ursu
Reply

Thanks a lot for your answer. It works like a charm :)

However, I cannot find the Order by Sort option in the Item publisher. I only have the fields of the Item type. Could this be version related?

 

 
Adrian Ursu
Reply

One more thing. Is it possible to list the fields of an item type inside navigation xslt?

  

 
Mikkel Ricky
Reply

You need 8.3 or later to use Order by Sort value (see http://developer.dynamicweb-cms.com/releases/dynamicweb-8-3/bug-fixes-8-3.aspx#item10636).

Currently you cannot use the item type fields in xslt navigation, but we have this on our list of future improvements. No fixed release date yet, though.

 
Adrian Ursu
Reply

Ok. Sort is not an issue if I use navigation. But displaying content is.

If I use navigation, I can display a hierarchy of item types but I cannot display their content.

If I use ItemPublisher, I cannot display them nested.

How can I mix these two in order to achive my desired result?

http://biogencobc.sherri-alexander.com/values-team.html

 

 

 

 
Mikkel Ricky
Reply

You can render a hierarchical structure of items using recursive Razor helpers, e.g.

<!DOCTYPE html>

@functions {
	Dynamicweb.Content.Items.ItemCollection GetItems(int pageID) {
		var items = Dynamicweb.Content.Items.ItemManager.Storage.GetByParentPageId("SomeItem", pageID);
		// Hmm ... GetByParentPageId returns the item with page id pageID?!
		// @TODO Use linq for this
		for (var i = items.Count-1; i >= 0; i--) {
			if ((int)items[i]["PageId"] == pageID) {
				items.RemoveAt(i);
			}
		}
		return items;
	}
}

@helper RenderItems() {
	var items = GetItems(Pageview.Page.ID);
	foreach (var item in items) {
		@RenderItem(item)
	}
}

@helper RenderItem(Dynamicweb.Content.Items.Item item, int level = 0) {
	<fieldset>
		<legend>@item["Name"]</legend>

		<div class="text">@item["Text"]</div>

		@{
			var items = GetItems((int)item["PageId"]);
		}
		@foreach (var i in items) {
			@RenderItem(i, level+1)
		}
	</fieldset>
}

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta charset="utf-8"/>
		<title>Andrian Ursu</title>
	</head>
	<body>
		@RenderItems()
	</body>
</html>

 

  

 
Adrian Ursu
Reply

This should be used as ItemPublisher List Template or as Page template?

 
Adrian Ursu
Reply

I got it. It's exactly what I needed.

Razor Rulz!

 

 
Mikkel Ricky
Reply

As you've figured out it's a page template set as default page template for the item type.

 
Lars Larsen
Reply

Hi

If you want to sort the items in the object "items" returned in line 5, how could that be done?

 
Mikkel Ricky
Reply

Use OrderBy (or OrderByDescending):

var items = Dynamicweb.Content.Items.ItemManager.Storage.GetByParentPageId("SomeItem", pageID).OrderBy(item => item["SomeFieldName"]);

Best regards,
Mikkel

 

 
Lars Larsen
Reply

What version of DW have you made the example for, Mikkel? I am using DW 8.4.1.2 and on that version it doesn't work because the method "OrderBy" does not exist on Dynamicweb.Content.Items.ItemCollection()!

 
Mikkel Ricky
Reply

I've tried it on 8.4.1.2, 8.4.1.8 and 8.4.1.16 and it works everywhere. OrderBy is not a part of Dynamicweb, but a part of the .NET framework.

Try adding

@using System.Linq;

in your Razor template to see if it works then.

 
Lars Larsen
Reply

oh I see, thanks Mikkel :-)

 

You must be logged in to post in the forum