Trying to get a String out of Velocity

I’m having some trouble with Velocity.

I have a String for a content ID in a loop, I want to compare it with the current page’s content ID. Both are meant to be of type java.lang.String, but I can’t get anything more useful than “[Ljava.lang.String;@cb9cfa” out of the latter.

$sys.params is supposedly a Map<String, String>, but that is what I get out of $sys.params.get(“sys_contentid”).toString().

I’m trying

#if ($sys.item.getProperty(“sys_contentid”).String == $currentPage.get(“sys_contentid”))

But it always returns false. Inspecting the two values, $sys.item.getProperty(“sys_contentid”).String gives me exactly what I’d expect it to, but $sys.params.get(“sys_contentid”).toString() only ever gives me a nonsense value like “[Ljava.lang.String;@cb9cfa”.

If it makes a difference, this is in rffSnTitleLink, called from uswSnUKNavRightNews (a snippet of ours) as

#slot(“uswSlRightNavAuto” “” “<p>” “</p>” “” “category=$category&max_results=10”)

Does anyone have any ideas on how to get a string out of this, erm, String? Or is there a better way, within a snippet, to identify whether a page in a loop is the current page?

Thanks!

Owen

The problem is the $sys.params returns an Array of Strings not a String. In the “debug” view:

sys_contentid [java.lang.String[]]

The [] indicates an Array. So you can reference the content id as

$sys.params.get(“sys_contentid”)[0]

[QUOTE=dbenua;378]The problem is the $sys.params returns an Array of Strings not a String. In the “debug” view:

sys_contentid [java.lang.String[]]

The [] indicates an Array. So you can reference the content id as

$sys.params.get(“sys_contentid”)[0][/QUOTE]

Nah, I tried that and it didn’t work. Any attempt to use an indexer to access items of that array just don’t work.

Following another suggestion, adding

#foreach( $id in $sys.params.get("sys_contentid") )
   #set($pageID = $id)
#end

almost works (there being only one item in that array, it just not letting me use indexers to get to it, for some reason).

Except that ID is always the same as the value of $sys.item.getProperty(“sys_contentid”).String, so I still can’t find out what the value is for my calling template.

I’m getting very frustrated with this now. The almost complete lack of documentation for Velocity is ridiculous, for a start.

Ok, you’re right. That doesn’t work.

What works in JEXL (that is, in the template bindings)

$content_id = $sys.params.get("sys_contentid").0
$content_id2 = $sys.item.guid.UUID  ##Note that this is an integer not a String 

In Velocity, you can do the following:


    <p>$content_id</p> <!-- a string from JEXL -->
    <p>$sys.item.guid.UUID</p> <!-- a number -->
    <p>$tools.list.get($sys.params.get("sys_contentid"),0)</p> <!-- the list tool -->

But reading back over your original post, I’m not sure what you’re trying to accomplish here. Where are you setting $currentpage ?

BTW, I assume you’re reading the VTL User Guide:
http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html

I’m trying to determine whether or not a snippet inside a for loop is referencing the page containing that snippet or not.

We’re building a news archive. The right-hand side of the page contains a list of the latest articles. I need to be able to tell, in the context of this list, whether each item is the same article as the page containing the list, so we can highlight the current article in that list.

I just can’t find a way to do this. I can’t believe there isn’t a way, I just don’t seem to be able to work out how to get Velocity and Rhythmyx to get me access to the right variables. My code reads:


#foreach( $id in $sys.params.get("sys_contentid") )
   #set($pageID = $id)
#end
	
#if ($sys.item.getProperty("sys_contentid").String == $pageID)
   <strong>#field("displaytitle")</strong>
#else
   <a href="$pagelink" class="titlelink">#field("displaytitle")</a>
#end

But $pageID (which should be the calling page’s ID) and $sys.item.getProperty(“sys_contentid”).String are always coming out with the same value — the ID of the page inside the loop, not the containing page.

BTW, I assume you’re reading the VTL User Guide:
http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html

Yes, it’s about as useful as a chocolate teapot. It is one of the least helpful user guides I’ve ever seen.

It seems to me that you need to set $pageID in the PARENT (or Page) template and then compare this value with the content Id of the snippet.

Values that you declare in the bindings of the page template will still be there in the bindings of the snippet. Here’s what I recommend you try (note that I have not tested this myself)

In the bindings (NOT the Velocity) of the page template:

$pageID = $sys.item.guid.UUID

in the Velocity of the snippet

#if($pageID && $pageID == $sys.item.guid.UUID)

Remember that when you preview the snippet without the page, $pageID will be null, so you need to account for this in your #if statement.

Let me know if that works any better.

Dave

[QUOTE=dbenua;386]It seems to me that you need to set $pageID in the PARENT (or Page) template and then compare this value with the content Id of the snippet.

Values that you declare in the bindings of the page template will still be there in the bindings of the snippet. Here’s what I recommend you try (note that I have not tested this myself)

In the bindings (NOT the Velocity) of the page template:

$pageID = $sys.item.guid.UUID[/QUOTE]

Ah, now this is looking more hopeful. Except $sys.item.guid.UUID in the article template is giving me the UUID of that folder’s navon. Any thoughts?

$sys.item always points to the current item you are assemblying. Are you doing this in a navigation template? If so, then you’ll get the GUID of the Navon. If you’re doing it directly on a page template, I’d expect you’d get the GUID of the page itself.