Copied to clipboard

Flag this post as spam?

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


  • adam 14 posts 74 karma points
    Jun 05, 2013 @ 16:04
    adam
    0

    Razor - For each loop

    @{var feed = Model != null ? Model : null;}
    
    <dl id="accordion">
        @foreach (var item in feed.DescendantsOrSelf("SubSubPage")){
    
        <dt>@item.Name</dt>
            <dd>@item.description</dd>
    }
    
    </dl>

    I am trying to show a list of accordions on a page that im doing in umbraco

    The setup is something like

     

    main page

    sub
    page sub sub page sub sub page sub page sub sub page sub sub page

    But with the code above its just listing them out in one big accordion

    What im trying to do is list them out in seperate accordions for each 'sub page' if this is possible?

  • Ian Black 54 posts 230 karma points
    Jun 05, 2013 @ 16:32
    Ian Black
    0

    Hi adam,

    You might need to tell it which level of decendants you want it to return. Something like this maybe so it looks through the sub pages and then loops through any children of those sub pages:

    <dl id="accordion">
       
    @foreach(var item in feed.DescendantsOrSelf("SubPage").Where("level == 2")){

    <dt>@item.Name</dt>
    <dd>@item.description</dd>

    @foreach (var subitem in item.DescendantsOrSelf("SubSubPage").Where("level == 3")){

    <
    dt>@subitem.Name</dt>
    <dd>@subitem.description</dd>
    }

    }

    </dl>
  • adam 14 posts 74 karma points
    Jun 05, 2013 @ 16:51
    adam
    0

    Hey Ian

    thanks a lot for the reply I couldnt get the above to work (it didnt render anything out)

    Im not sure if the above would list them out in seperate accordions for example

    at the moment it lists them out like:

    <dl id="accordion">
     <dt>sub sub page 1 stuff</dt>
     <dt>sub sub page 2 stuff</dt>
    </dl>

    Im trying to list each sub page's content in its own accordion for example:

    <dl id="accordion">
     <dt>sub sub page 1 stuff</dt>
     <dt>sub sub page 1 stuff</dt>
    </dl>

    <dl id="accordion">
     <dt>sub sub page 2 stuff</dt>
    </dl>

  • Ian Black 54 posts 230 karma points
    Jun 05, 2013 @ 17:16
    Ian Black
    0

    If the <dl id="accordion"> needs to be repeated you'll definitely need to have it inside of a foreach loop:

    @{var feed =Model!=null?Model:null;}

    @foreach(var item in feed.DescendantsOrSelf("SubSubPage")){
    <dl id="accordion">
    <dt>@item.Name</dt>
    </dl>
    }
    }

    Also, you might want to give the <dl> a class of accordion rather than an ID as the ID won't be unique if there are more than one of them.

  • adam 14 posts 74 karma points
    Jun 05, 2013 @ 17:25
    adam
    0
    @{var feed =Model!=null?Model:null;}

    @foreach(var item in feed.DescendantsOrSelf("SubSubPage")){
    <dl id="accordion">
    <dt>@item.Name</dt>
    </dl>
    }
    }

    Hey thanks for the reply, but would this not just list every item out in a dl with a single dt?

    Rather than spliting each sub page into a dl with its multiple dt's

     

     

  • Ian Black 54 posts 230 karma points
    Jun 05, 2013 @ 17:49
    Ian Black
    100

    It's tricky without knowing the structure of your pages in the content tree but how about something like this:

    @foreach(var item in feed.DescendantsOrSelf("SubPage")){
    <dl id="accordion">

    <dt>@item.Name</dt>

    @foreach(var itemSub in item.DescendantsOrSelf("SubSubPage")){

    <dt>@itemSub.Name</dt>

    }

    </dl>
    }

    Is there a method to formatting code correctly in these forums - I can't seem to get it right - especially indenting lines!?

  • adam 14 posts 74 karma points
    Jun 05, 2013 @ 18:00
    adam
    0

    Thanks a lot Ian!! It works &  thats exactly what I was trying to do :D

     

  • Ian Black 54 posts 230 karma points
    Jun 05, 2013 @ 18:01
    Ian Black
    0

    Awesome, glad you got it working!

Please Sign in or register to post replies

Write your reply to:

Draft