2013-07-07 31 views
0

我通過列出具有umbraco 4.11.9(子可變源)中包含的示例的特定頁面的子節點來製作頁腳。剃刀宏:將子節點劃分爲三個列表

頁腳中元素的數量預計會發生變化,因此宏必須將子元素列表分成三列,並將它們放在三個UL中。 所有的孩子都是源的直接後裔

當前代碼:

@inherits umbraco.MacroEngines.DynamicNodeContext 

@{ 
    var startNodeID = "1089"; 
} 

@if (startNodeID != null) 
{ 
    @* Get the start node as a dynamic node *@ 
    var startNode = Library.NodeById(startNodeID); 

    if (startNode.Children.Where("Visible").Any()) 
    { 
     <ul> 
      @foreach (var page in startNode.Children.Where("Visible")) 
      { 
       <li><a href="@page.Url">@page.Name</a></li> 
      } 
     </ul> 
    }  
} 

回答

1

我在使用模運算符之前做過類似的事情。看下面的例子:

var col1Count = 1; 
var col2Count = 1; 
var col3Count = 1; 

<div class="col col-3 hide-mobile"> 
    @foreach (var nodeId in links) 
    { 
     if (col1Count % 3 == 1) 
     { 
      // Output column 1 content 
     } 
     col1Count++; 
    } 
</div> 

<div class="col col-3 hide-mobile"> 
    @foreach (var nodeId in links) 
    { 
     if (col2Count % 3 == 2) 
     { 
      // Output column 2 content 
     } 
     col2Count++; 
    } 
</div> 

<div class="col col-3 hide-mobile"> 
    @foreach (var nodeId in links) 
    { 
     if (col3Count % 3 == 0) 
     { 
      // Output column 3 content 
     } 
     col3Count++; 
    } 
</div> 
0

這可能是一個有點直接的方法,但爲什麼不嘗試(在你如果/。任何()):

注意 - 這種方法使用LINQ

var childNodes = startNode.Children.Where("Visible").ToList(); 
    int perListCount = childNodes.Count; 
    for (int i = 0; i < 3; i++){ 
     var activeNodeList = childNodes.Skip(i * perListCount).Take(perListCount);   
     if (i == 2){ 
     activeNodeList = childNodes.Skip(i * perListCount); 
     } 
     <ul> 
     @foreach(var node in activeNodeList){ 
     <li><a href="@node.Url">@node.Name</a></li> 
     } 
     </ul> 
    } 
+0

ProNotion的例子比這個好得多:) – Sidheag