Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
simple iteration question
Ottawa_IWOV
Hi All-
I am sure this would seem easy to advanced templaters, but can't find anything in the documentation
I have a DCR that contains navigation elements:
Title
About Us
References
Corporate
I was to use a TPL to iterate over this list, but produce a different style for the first and last elements. Seems like a combination of iw_iterate and something else ...
Example
Titlle (class = top)
About Us (class = normal)
References (class = normal)
Corporate (class= bottom)
Any hints?
Find more posts tagged with
Comments
Migrateduser
Use the iteration attribute of iw_iterate to track which iteration you're own and compare it to the length of the list. Probably the best way to get the length is to do a call to iwpt__dcr_list in an iw_perl and "cast" the result to a scalar using the scalar function (or you could save it to an array and ref $#arrayname to get the maximum index).
Adam Stoller
I'm not sure what you're referring to in terms of iteration. Are you talking about iterating over 4 different fields which contain information regarding [respectively]: Title, About Us, References, and Corporate -- or are you talking about iterating over N-sets of fields in which each set contains the four fields (/Title, /AboutUs, /References, and /Corporate)?
The latter makes more sense to me - but then the rest of the question doesn't really make sense because just be refereincing the field you know which one it is, and you can easily customize the output accordingly -- I mean if you're accessing 'dcr.Container[1].Title' then you put '<p class="top">' and '</p>' before and after (respectively) the value of that field - right?
Maybe I'm just not understanding what you're asking - perhaps you can refine the question?
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com
Ottawa_IWOV
Ok ...
I have a DCR, with a replicant, that is storing four items that will make up a left hand menu. I want to iterate over that list, and apply a unique style to the first item, and the last item. Thing is, we will not know how many items are there, so we cannot rely on dcr.element and expect accurate results. I basically want to be able to output something for the first and last iteration.
Does this make more sense?
Thanks,
Adam Stoller
Closer, but I think there's still some miscommunication going on here.
If you have a replicant with 4 items in it - then those 4 items will appear in each and every instance of the replicant - e.g.:
<item name="Title">...</item>
<item name="About Us">...</item>
<item name="References">...</item>
<item name="Corporate">...</item>
For the sake of simplicity, let's call the above a "set" - and you could have anywhere from the
min
to
max
amount of these sets in your replicant - for the sake of simplicity let's assume values of
1
and
5
for min and max values - which means anywhere from:
<item name="replicant_container">
<item name="replicant_instance">
<!-- 0 -->
...set...
</item>
<item name="replicant_instance">
<!-- 1 -->
...set...
</item>
<item name="replicant_instance">
<!-- 2 -->
...set...
</item>
<item name="replicant_instance">
<!-- 3 -->
...set...
</item>
<item name="replicant_instance">
<!-- 4 -->
...set...
</item>
</item>
(I believe the instances are zero-based, if not, change the numbers above to be from 1 to 5 rather than 0 to 4)
You can find out how many instances exist in your replicant with either the XML or Perl methodology for handling replicants - and unless it really matters to you how many there are
going to be
, you can just loop through whatever is there until you get to the end.
In each instance of your loop, however, you know the names of the fields that exist within the set for the replicant, and you know what style you wish to apply to the content of each field in the set - so, like I said before - this should be pretty trivial to implement. (pseudo code below):
foreach my $node (...loop through replicant instances...){
iwpt_output("<p class='top'>$node.Title</p>\n")
if ($node.Title ne '');
iwpt_output("<p class='normal'>$node.AboutUs</p>\n")
if ($node.AboutUs ne '');
iwpt_output("<p class='normal'>$node.References</p>\n")
if ($node.References ne '');
iwpt_output("<p class='bottom'>$node.Corporate</p>\n")
if ($node.Corporate ne '');
}
To output something for the first and last iteration, just do so *outside* the loop - something before, and something after. If you want to treat the values in the first instance differently - use a counter within the loop as a boolean/toggle switch to do what you want for the first instance. If you want to treat the values in the last instance differntly there are a number of approaches but probably the simplest is to get a count of how many instances there are to begin with and then check the counter against that value each time through the loop.
Does that make more sense now?
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com
Migrateduser
I understood him to mean that Title, About Us, References, and Corporate are the values of one field, probably something with a name like "LinkText", so that each replicant instance would have a LinkText item and an item (something like LinkURL) for the target of the link.
Adam Stoller
Well - he'll need to clarify whether that's the case and I misunderstood - or whether I was correct and perhaps he misunderstood ... ;-)
--fish
Senior Consultant, Quotient Inc.
http://www.quotient-inc.com