Yes, it would definitely work better with Tpl chunks. Say your array looks like this:
node1
name
childen
child1
childName
child1data1
child1data2
child2
childName
child2data1
child2data2
node2
name
children
child1
childName
child1data1
child1data2
child2
childName
child2data1
child2data2
And you want your output something like this:
<ul class="MyArray">
<li>NodeName1</li>
<ul class="child">
<li>Child1Name</li>
<ul class="childData">
<li>Child1Data1</li>
<li>Child1Data2</li>
</ul>
<ul class="childData">
<li>Child1Data1</li>
<li>Child1Data2</li>
</ul>
</ul>
<li>NodeName2</li>
<ul class="child">
<li>Child1Name</li>
<ul class="childData">
<li>Child1Data1</li>
<li>Child1Data2</li>
</ul>
<ul class="childData">
<li>Child1Data1</li>
<li>Child1Data2</li>
</ul>
</ul>
</ul>
You'd do something like this (this is off the top of my head and almost certainly not correct, but should give you the idea):
Three chunks:
OuterTpl:
<ul class="MyArray">
[[+nodes]]
</ul>
nodeTpl:
<li>[[+nodeName]]</li>
[[+children]]
childTpl:
<ul class="child">
<li>[[+childName]]</li>
<ul class="childData">
[[+childData1]]
[[+childData2]]
</ul>
</ul>
Then in your code:
<?php
$outerTpl = $modx->getChunk('outerTpl');
$nodeTpl = $modx->getChunk('nodeTpl');
$childTpl = $modx->getChunk('childTpl');
$output = "";
$nodes = "";
$children = "";
foreach (MyArrray as $node) {
$name = $node['name'];
$node = str_replace('[[+nodeName]]', $name, $nodeTpl);
foreach ($node['children'] as $child) {
$child = str_replace('[[+childName]]', $child['name'], $childTpl);
$child = str_replace('[[+childData1]]', $child['childData1'], $child);
$child = str_replace('[[+childData2]]', $child['childData2'], $child);
$children .= $child;
}
$node = str_replace('[[+children]]', $children, $node);
$nodes .= $node;
}
$output = str_replace('[[+nodes]]', $nodes, $outerTpl);
return $output;
The advantage of this it that you can then change the output formatting without touching your code.
------------------------------------------------------------------------------------------
PLEASE, PLEASE specify the version of MODX you are using.
MODX info for everyone:
http://bobsguides.com/modx.html
[ed. note: BobRay last edited this post 14 years, 3 months ago.]