Is it possible to number the items in a list slot? If so, would it be possible to make a template that produces something like this:
theImages[0] = '/images/featuredContent/FCnews.jpg';
altText[0] = 'The News';
theImages[1] = '/images/featuredContent/FCstulife.jpg';
altText[1] = 'Student Life';
theImages[2] = '/images/featuredContent/FCimagine.jpg';
altText[2] = 'Imagine Student Artists';
If I could publish this as a Javascript file, users could update rotating images and alt attributes on the home page. As it is, the URLs and alt attributes are hard-coded in the Javascript.
Is it possible to call a slot using variables, like this:
#slot("sList" "<div id=$var>" "theImages[$var]" "" "</div>" "")##
I think you could use something like this:
#initslot("sList")
#set( $i = 0 )
#if($sys.currentslot.relresults.size() > 0)
#foreach( $relresult in $sys.currentslot.relresults )
theImages[i] = '/images/featuredContent/FCnews.jpg';
altText[i] = 'The News';
#set( $i = $i + 1 )
#end
#end
#end
Nice, except you’re hard-coding the slot contents. How about something more like
altText[i] = $sys.item.getProperty(“img_alt”).String);
Right on, it was more of an example. That should work.
To anyone searching the forums, you should use $relresult.getNode() in the foreach loop. Using sys.item.getProperty() will return the current item you are viewing the slot from and is probably not what you want. Thus the final code may look like:
#initslot("sList")
#set( $i = 0 )
#if($sys.currentslot.relresults.size() > 0)
#foreach( $relresult in $sys.currentslot.relresults )
theImages[i] = $relresult.getNode().getProperty('displaytitle').String;
altText[i] = $relresult.getNode().getProperty('img_alt').String; ;
#set( $i = $i + 1 )
#end
#end
#end
Another Velocity tip: You can use $velocityCount within the #foreach loop - it’s the default name for the loop counter. It starts at 1 by default. I understand that you can rename the counter, and set it to start at 0 in the velocity.properties file, but I can’t confirm that, since I haven’t tried it.
In this case, you’d use ($velocityCount - 1) in place of i in your code example.
-Kathleen
It turns out this was all I needed for my SnRotate snippet template that formats my list items:
theImages[$sys.index] = '$rxs_navbase/images/featuredContent/$sys.item.getProperty('sys_title').String$sys.item.getProperty('sys_suffix').String';
altText[$sys.index] = '$sys.item.getProperty('img_alt').String';
PgRotatingImages is a lot of Javascript and one slot:
#slot("sList" "" "" "" "" "template=SnRotate")##
The resulting Javascript works, but I’m still hard-coding too much. Can’t I use $sys.pub_path instead of “/images/featuredContent”? I must have been doing it wrong.
Also, the file is publishing as siteFunctions.html instead of siteFunctions.js, even though I set the Location Suffix in the template to js.
Here’s my Location Scheme:
$baseUrl = '';
$filename =$sys.item.getProperty('rx:sys_title').String;
if ($user.psoRelationships.isLandingPage($sys.item.getProperty('sys_contentid').String)) {
$filename = 'index';
}
$baseUrl + $sys.pub_path + $filename + $rx.location.getFirstDefined($sys.item,'rx:activeimg_ext,rx:sys_suffix', '.html');
Since numbering now works fine, I started two new threads asking about hidden content fields and $sys.pub.path.