how to use $rx.cond.choose

I’m having difficulty using a conditional statement in my velocity template.

For example:

#set($test = ‘hello’)
test: $test
<br/>
conditional output:
$rx.cond.choose("$test == ‘hello’",‘valuetrue’,‘valuefalse’)

output i get from this is

valuefalse

which is the same for

#set($test = ‘hello’)
test: $test
<br/>
conditional output:
$rx.cond.choose("$test != ‘hello’",‘valuetrue’,‘valuefalse’)

I also tried the following syntax but hit an error during assembly

$rx.cond.choose($test != ‘hello’,‘valuetrue’,‘valuefalse’)

I would appreciate any help on this.

Thanks,
Veeren

Hi Veerendra

Try

$rx.cond.choose($test == “hello”,‘valuetrue’,‘valuefalse’)

As you had the whole condition in quotes it was evaluate the string and not the value.

Cheers
James

Veerendra,

Since you’re using Rhythmxy Version 6.5.2, you might also want to consider using the JEXL if operator, which was introduced when we upgraded JEXL in Rhythmyx Version 6.5. The $rx.cond.choose function is deprecated as of Version 6.5.

RLJII

The if operator works in Velocity as well as JEXL, but I’m having a problem with the condition. For example, this Velocity code displays a heading in my Callout snippet:

 #if (1==1) 
 <h2>#field("displaytitle")</h2>
 #end

But this more complex condition gets the error, “Problem assembling output for item: 4-101-881 with template: SnCallout exception: Property rx:usage not found see log for stack trace”:

 #if ($sys.item.getProperty("rx:usage").String == 'L') 
 <h2>#field("displaytitle")</h2>
 #end

Any suggestions? Basically I’m trying to avoid using a dispatch template just to deal with one-line differences between pages.

I usually set it equal to a variable before using it in a conditional…


#set ($prop =$sys.item.getProperty("rx:usage").String)
#if ($prop == 'L') 
< h2>#field("displaytitle")< /h2>
#end


Everyone else will probably tell you to set that line in the bindings of the page…

<strike>But of course to really answer your question, just use the toString() method on it…

#if ($sys.item.getProperty(“rx:usage”).String.toString() == ‘L’)
< h2>#field(“displaytitle”)< /h2>
#end
</strike>

I just verified that your code seems to work on our system (but I do know there was a point where it did not so I had to do the first way i suggested). We are on 6.5.2.13420.

A better way is:


#if ($sys.item.hasProperty("rx:usage") && getProperty("rx:usage").String.equals('L')) 
 <h2>#field("displaytitle")</h2>
#end

This, of course, is the syntax for Velocity (in the template). The syntax for JEXL is slightly different.

Yes…that would be the better way…:slight_smile:

Eh…i didn’t look at your error…because you are apparently putting this code in SnCallout (which i assume is a snippet) is it possible that another content type using this snippet does not have the rx:usage field?

Using the code dbenua has makes sure the property/field exists, so you probably will not see it get to that point (where velocity fails to find the undefined field)

Hmm. Using Dave’s code, I get:

Problem assembling output for item: 2-101-952 with template: SnCallout exception: Encountered "getProperty" at line 13, column 59.
Was expecting one of:
    "[" ...
    "(" ...
    <STRING_LITERAL> ...
    "true" ...
    "false" ...
    <NUMBER_LITERAL> ...
    <IDENTIFIER> ...
    "{" ...
    "!" ...
     see log for stack trace

eh…you did check that you put $sys.item. in front of the second condition right?

so it would be:


#if ($sys.item.hasProperty("rx:usage") && $sys.item.getProperty("rx:usage").String.equals('L')) 
 <h2>#field("displaytitle")</h2>
#end

Sorry about that, I guess I answered too quickly.

Jit is correct, you need the $sys.item.getProperty() not just getProperty() or you will get a syntax error.

Dave

I made that change and it works great. Or, it works great from Generic pages which actually have the Usage field - which of course SnCallout does not. Maybe I need $sys.the.other.item.getProperty(“rx:usage”).String.equals(‘L’))!

I don’t understand this statement. The #if statement will be “false” whenever you assemble an item which does not contain the “usage” field. You need to handle this in the #else condition.

Do you want to clarify exactly what your goal is here?

Dave

I was sort of making a joke about my coding abilities or lack of them. Goal: if a callout snippet is contained in a slot on a Landing page (a page for which usage==L), I want to display a header above the callout body. Is there a function that can be included in a snippet that checks a condition in the content type that contains the snippet, not a condition in the snippet itself?

Not directly. You cannot reference the “properties” of the containing node. However, you can set a binding variable (in the bindings tab) of the containing (page) template, and reference this binding variable in the snippet code.

You need to make sure that the variable has a name that will not cause it to be overwritten (e.g. don’t use one of the standard $sys variables).

You also need to set the variable in the binding tab and not in the Velocity source. Otherwise, it will not be visible to the snippet.

Lastly, you need to be prepared for the variable to be null, as it will not be defined when you preview the snippet, or include the snippet in another slot.

Dave

Thanks, Dave. That seems to work well for me.

I simply included this binding for $usage in my Generic page template:

$sys.item.getProperty("rx:usage").String

and this code in the SnCallout snippet which is included in that Generic page:

        #if ($usage == L') 
        <h2 class="calloutTitle">#field("displaytitle")</h2>
	#end

The FF Generic content type includes a value for rx:usage, and I’m using it in my dispatch template as well.

Agreed, but I believe the concern is over what happens when the $usage variable is undefined? I’m not sure if velocity throws an error of whether it is just a warning. In any case, if you check to make sure that the field exists (as per dbenua’s code) using $sys.item.hasProperty(“rx:usage”) you would get by any null declarations (so you could use the SnCallout snippet with content types that don’t necessarily have the usage field defined…just makes for a reusable snippet template).

Duly noted. Just because my template didn’t choke this time doesn’t mean I did it right.