SOLVED

Re: SOLVED: "if x or x" syntax in velocity?

Go to solution
Charles_Sander1
Level 4

SOLVED: "if x or x" syntax in velocity?

hopefully a quick one. I'm just trying to find syntax somewhere for a little velocity script I have. Right now it's just a series of if/else statements. I'd like to include an or inside one of these. But I'm not really sure of the proper syntax for this.

Basically, I'd like to change this:

#elseif(${lead.lastRequestedAssetType} == "infographic")

Into something like this. I'm just assuming I might be close to the syntax:

#elseif(${lead.lastRequestedAssetType} == (“infographic" || “report”))

or maybe it's like this?

#elseif(${lead.lastRequestedAssetType} == "infographic" || “report”)

EDIT: Neither one of the above seems to have worked.

EDIT2: Figured it out. Just a little longer than I'd hoped:

#elseif((${lead.lastRequestedAssetType} == "white_paper") || (${lead.lastRequestedAssetType} == "report"))

Tags (3)
1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: SOLVED: "if x or x" syntax in velocity?

You shouldn't be using ${formal} references here, but $simple (always use $simple in non-output situations, like comparisons).

There actually are shorter ways to express this condition.

One is to use a regex:

#if( $value.matches("^(apple|orange)$") )

Another is to use a collection, which can make for better code organization:

#set( $interestingValues = ["apple","orange"] )

#if( $interestingValues.contains($value) )

Finally, please remember to highlight code when posting.

https://s3.amazonaws.com/blog-images-teknkl-com/syntax_highlighter.gif

View solution in original post

2 REPLIES 2
SanfordWhiteman
Level 10 - Community Moderator

Re: SOLVED: "if x or x" syntax in velocity?

You shouldn't be using ${formal} references here, but $simple (always use $simple in non-output situations, like comparisons).

There actually are shorter ways to express this condition.

One is to use a regex:

#if( $value.matches("^(apple|orange)$") )

Another is to use a collection, which can make for better code organization:

#set( $interestingValues = ["apple","orange"] )

#if( $interestingValues.contains($value) )

Finally, please remember to highlight code when posting.

https://s3.amazonaws.com/blog-images-teknkl-com/syntax_highlighter.gif

Charles_Sander1
Level 4

Re: SOLVED: "if x or x" syntax in velocity?

Ah! Thank you! I never even noticed that that tool option. I've gone ahead and edited the first post to highlight code.

I'll try rewriting this now with the regex solution. Awesome!