Is there an easy way to get a slotted item's postion in the Slot?

I need to output the index/position of the slotted item in my snippet template.

For example, if I have two items in my slot then I want to out put something like

<rank>1</rank>
<data>slotted item 1 content</data>

<rank>2</rank>
<data>slotted item 2 content</data>

I was wondering if there was an easy way to do this other than doing some code in my snippet template to pull the data from the rx db tables.

Thanks,
Veeren

Use the $velocityCount variable.

Dave,

this works in my main text page template to give me the total count of items in the slot, but it doesn’t work in my snippet to give me the position. When I plug in the variable in my snippet template it’s not resolving.

Snippet Velocity Template Code:

<div class="list-item">				
          <div class="rank">$velocityCount</div>
          <div class="image">$imglink</div>
          <div class="title">#field("sys_title")</div>	
          <div class="body">#field("item_body")</div>
#if ($link != "")
          <div class="link">$link</div>
#end
</div>

Is there something that will give me the current item’s position in the slot that I can output in my snippet template.

Thanks,
Veeren

I found what I was looking for:

$sys.index

A loop counter for Slot contents; can only be used in a Slot. The index of the Content Items in the Slot. Indexing begins at 1. (In other words, the first Content Item in the Slot is index1, the second index 2, and so on.) This variable can be used to modify the rendering of different rows in the Slot, or to add more data to the Snippet rendering

Thanks,
Veeren

Thought someone else might be able to re-use this code, if you have any improvements please let me know.

I wanted to dynamically wrap a snippet item within a table but this posed some problems with how to also grab the table row header. The snippet item is a special template used to show departments in a directory style table listing.

The snippet template code:


html>
	table>
	body>
		tr>
			th>Department /th>
			th>Phone /th>
			th>Building /th>
		/tr>
		tr>
			td>#field_if_set("a href='${pagelink}'>" "rx:displaytitle" "/a>") /td>
			td>#field_if_set("" "rx:phone_number" "") /td>
			td>#field_if_set("" "rx:building" "") /td>
		/tr>
	/body>
	/table>
/html>

  • all “<” are removed for display.
    ** I placed the table tags outside of the body to prevent them from being included with the snippet.

The snippet passes the table header and data associated with the department within each snippet.

The auto index in this example returns multiple department snippets with the auto index template and combines them into one table element. This is where I found a use for the $velocityCount variable mentioned in the previous post. A regular expression is used to remove the table header passed in by the snippet template.

Auto Index Template Code


#set($params = "list='%'&sitepath=${sitepath}&folderpath=${folderpath}")##
#initslot($slotname $params)##
html>
	body>
		table>
    		#foreach($relresult in $sys.currentslot.relresults)##
    			#if($velocityCount == 1)##
    				$rx.doc.extractBody($relresult)##
    			#else
    				$rx.doc.extractBody($relresult).replaceFirst(' tr>(\r*\s*.*\r*\s)*?(\s* .tr>)','')##
    			#end##
    		#end##
		/table>
	/body>
/html>	

*once again all “<” are removed from the code.

If you would like to use the regular expression to remove a “tr” element, add a “<” before the “’ tr>” string and also before the “.tr>” string.