Getting info from in a slot of another item

Hi all,

I am working on a template and I need info for an item that exists in the slot of another item. I thought of a sql in an autoindex or using $rx.db.get but can’t seem to find where the info is located in the db.

Any ideas?

mikeg

We added a function to the PSO Toolkit for this: $user.psoSlotTools.getSlotContents().

This function returns a List of IPSAssemblyItem objects. You can get a field from an Assembly Item with $item.getNode().getProperty()

Let me know if you have any more questions.

Thanks - but you must pardon me for being a newbie to RX development.

I have entered the following into the source pain of the template:
$slotparams.template = “Top_Stories”
$slotcontents = $user.psoSlotTools.getSlotContents($sys.assemblyItem, “Article”, $slotparams)
$slotcontents.getNode(0).getProperty(“sys_title”)

Top_Stories being the template of the slote the item is in and Article is the content type.

What I am truly after is only the “sys_title” of the first item in the slot (there are 9 items in all).

thanks
mikeg

Mike,

Do you have a question about this? It seems correct to me (as far as it goes).

You’ll probably want to handle the case where the slot is empty (there won’t always be exactly 9 items in the slot).

Dave

me again,

Here is my complete code in the source window:

#initslot(“Top_Stories” “”)
#if($sys.currentslot.relresults.size() > 0)
$slotparams.template = “Top_Stories”
$slotcontents = $user.psoSlotTools.getSlotContents($sys.assemblyItem, “Article”, $slotparams)
$slotcontents.getNode(0).getProperty(“sys_title”)
#end

when I preview the template I receive the code text in the preveiw. In other words the code is not resolving.

???

mikeg

If you want to set a variable in Velocity code (as opposed to in the bindings tab), you must use #set

#set($slotparams.template = “Top_Stories”)

hi again,

still missing something. here is the code:

#initslot(“Top_Stories” “”)
#if($sys.currentslot.relresults.size() > 0)
#set($slotparams.template = “Top_Stories”)
#set($slotcontents = $user.psoSlotTools.getSlotContents($sys.assemblyItem, “Article”, $slotparams))
$slotcontents.getNode(0).getProperty(“rx:sys_title”).String
#end

When I run this I get '$slotcontents.getNode(0).getProperty(“rx:sys_title”).String" appearing in the preview.

But if I run this code -

#initslot(“Top_Stories” “”)
#if($sys.currentslot.relresults.size() > 0)
#foreach( $relresult in $sys.currentslot.relresults )
<p>$relresult.node.getProperty(“rx:sys_title”).String</p>
#end
#end

the code iterates through the items correctly.

??? tried a bunch of things [node(0), getNode(0), etc.] but haven’t had any luck.

???

mikeg

Change the line

$slotcontents.getNode(0).getProperty(“rx:sys_title”).String

to

$slotcontents.getNode().getProperty(“rx:sys_title”).String

or

$slotcontents.node.getProperty(“rx:sys_title”).String

The “getNode()” method has no parameters, and because you passed it a parameter, Velocity doesn’t recognize that you’re looking for a method with no parameters. It’s kind of fussy.

“.node” is short of “getNode()” not “getNode(0)”.

I usually recommend that people use the “Bindings” tab, because the debug view will show you the types. Also, you would have gotten an error that might (or might not) have helped you.

Dave

Hi,

Still not working with your suggestions - ???

I ended up going with this -

#initslot(“Top_Stories” “”)
#if($sys.currentslot.relresults.size() > 0)
#set($x = 0)
#foreach( $relresult in $sys.currentslot.relresults )
#if($x == 0)
$relresult.node.getProperty(“rx:sys_title”).String
#end
#set($x = $x + 1)
#end
#end

Seems to me there should be an easier way than this.

?? Where can I find documentation on the psotoolkit and psoslottools?

mikeg

The only documentation for the PSOToolkit is the JavaDoc that is distributed with the package. In this case, it doesn’t have a lot of detail.

I know that the PSOSlotTools approach works, but I don’t see what you’re doing wrong. Did you try this in the Binding Tab, or just in Velocity? using the Binding tab will give you more explicit errors and allow you to debug the results.

There is another way to approach this: set the “max_results” parameter on the slot finder to 1, which should only give you the first item in the slot. You set this parameter the same way that you set the “template” parameter, usually by adding it to a string of values: “name1=value1&name2=value2” etc .

Dave

Hello again,

I have this working on our development server perfectly. Unfortunately when I moved this to production the template is unable to return shared or local field values. That is I have done tests trying to return fields of type local, sys and shared to the $webtitle variable and only sys fields don’t error. Here is the code in the template -

<pre>
#initslot(“Top_Stories” “”)
#if($sys.currentslot.relresults.size() > 0)
#set($x = 0)
#foreach( $relresult in $sys.currentslot.relresults )
#if($x == 0)
#set($pagename = $relresult.node.getProperty(“rx:sys_title”).String)
#set($webtitle = $relresult.node.getProperty(“rx:headline”).String)
#set($dte = $relresult.node.getProperty(“rx:sys_contentstartdate”).Date)
#set($dteyear = $tools.date.getYear($dte))
<a href="/news/${dteyear}/${pagename}.html" target="_blank">${webtitle}</a>
#end
#set($x = $x + 1)
#end
#end
</pre>
Any thoughts why I cannot return local or shared fields for the nodes?

mikeg

My only thought is that if the Node properties are different between servers, there are 2 main possibilities:
[ol]
[li]The item in the slot doesn’t have the same content type it had in development</snip>
[/li][li]The content type doesn’t reference those fields on the production server.[/ol]You need to eliminate these 2 possibilities before we move on to something more subtle.
[/li]
Dave

me again - is there a way to determine what content type the template is linked to? I can’t seem to find anything in the template interface.

mikeg

I don’t see why you’d want to get the content type associated with the Template in this case.

Much more useful would be the content type associated with the content item.

$sys.item.primaryNodeType.name 

You can get the templates associated with a content type from the Assembly manager service, but I don’t see why that would help you.

Dave

Hi,
Is there a way to get the URL of the item in the slot ?

Thanks,

Guillaume

Guillaume,

Sure. In the example above, $relresult is an IPSAssemblyResult object, which also extends IPSAssemblyItem.

To get the URL of any Assembly Item, use:

$rx.location.generate($relresult,$relresult.template)

This will produce the URL appropriate for the context you are assembling in. If this is the “preview” context, the URL will be a preview URL, not the result of computing the location scheme for the item and template.

Note that if you use the 1 argument form of the generate() function, you will get a link to the default template for the content type.

I hope I answered the question

Dave

Thank you.
It work like I wanted.

Guillaume

Mike,

Really helpful thread here. Thanks for posting this and Dave for your response!

I share the same requirement of wanting to just grab the first item in the slot results. You were close. You had:

$slotcontents.getNode(0).getProperty(“rx:sys_title”).String

What you really needed was to get element zero from the slotcontents result set, rather than the node object. It appears to implement (or at least follow) Java’s List interface, so you can do that by calling the “get” method as follows:

#set($contentItem = $sys.currentslot.relresults.get(0).node)

From there, you can grab any property from your new variable:

#set($pagename = $contentItem.getProperty(“rx:sys_title”).String</p>

This frees you from having to loop through all the slot elements just to get the first one.

Steve

Just to both hijack and resurrect this thread in one swoop…

I’m finding this problem too, in that I if I call:

$relresult.getNode().getProperty(“rx:sys_title”).String

It works fine for a sys property such as that above, but it fails for any local property I call. This is still in dev, which means that it is related to any difference in how dev/production are configured as suggested below, so I’m wondering what the “something more subtle” could be that might answer this?

thanks.

[QUOTE=dbenua;1153]My only thought is that if the Node properties are different between servers, there are 2 main possibilities:
[ol]
[li]The item in the slot doesn’t have the same content type it had in development</snip>
[/li][li]The content type doesn’t reference those fields on the production server.[/ol]You need to eliminate these 2 possibilities before we move on to something more subtle.
[/li]
Dave[/QUOTE]

Just to add to the mystery, calling hasProperty() on the local field returns true!