36

I want to include one template nested into others cont1, cont2, cont3. And nested template should be hide one specific control for cont1 only. Before inclusion into cont1 I would like to assign value to some flag variable $hideMyControl.

And inside nested template I would like to check if $hideMyControl is assigned value.

How to perform such check?

sergtk
  • 10,714
  • 15
  • 75
  • 130

6 Answers6

37
#if($hideMyControl)
    // your code
#end

If $hideMyControl is defined, your code will execute

Yves M.
  • 29,855
  • 23
  • 108
  • 144
onlinehood
  • 370
  • 1
  • 3
  • 4
23

You can do this using

  #if($!{$articleLeader})
      // Perform your operation or the template part you want to show.
  #end

For more info, see the 'formal reference' section of the Apache Velocity Reference Manual.

sagar27
  • 3,121
  • 3
  • 27
  • 37
  • 12
    There's no point to using formal and silent notation in an #if, which you didn't quite get right anyway. Just do #if( $article ) ## perform op here #end – Nathan Bubna Jan 28 '11 at 18:13
  • 2
    So what about checking if it's NOT defined? – Snekse Jun 21 '13 at 18:30
  • This answer users [Quiet Reference Notation](http://velocity.apache.org/engine/2.0/user-guide.html#quiet-reference-notation) that may lead to exception being thrown on undefined reference in [Strict Reference Mode](http://velocity.apache.org/engine/2.0/user-guide.html#strict-reference-mode). See [VELOCITY-739](https://issues.apache.org/jira/browse/VELOCITY-739). – Vadzim Feb 15 '19 at 12:16
11
#if($!{hideMyControl} != "")
## do something if $hideMyControl is defined
#end

This works for me in AWS API Gateway Body Mapping Templates. Please refer to Quiet Reference Notation in Velocity User Guide for more information.

erabbit
  • 111
  • 1
  • 2
1

I was using

#if ($hideMyControl) 
    //do something 
#end 

since a few months ago, however today its not working anymore.

I came here to find help, and noticed a new way of writing it :

#if($!{$hideMyControl})
   // do something
#end

this code works!

Max Alexander Hanna
  • 3,388
  • 1
  • 25
  • 35
  • `since a few months ago, however today its not working anymore.` Is this maybe a Velocity 2.0 migration issue? - see "method arguments are now converted as needed between all main basic Java standard types (booleans, numbers and strings)." – Patrick Feb 19 '19 at 12:43
  • wow im glad i left the job that hired me to do that a few years ago, no one ever told me they migrated versions as i was coding up the EDI part of their ERP systems using that language. glad i left that company it was a shitshow lol. – Max Alexander Hanna Feb 19 '19 at 15:10
1

According to the docs for Strict Reference Mode it is possible to several constructions to check if variable is defined.

#if ($foo)#end                  ## False
#if ( ! $foo)#end               ## True
#if ($foo && $foo.bar)#end      ## False and $foo.bar will not be evaluated
#if ($foo && $foo == "bar")#end ## False and $foo == "bar" wil not be evaluated
#if ($foo1 || $foo2)#end        ## False $foo1 and $foo2 are not defined

So this code works in my case.

#if( !$value )
  // Perform your operation or the template part you want to show.
#end
Roman Kotov
  • 1,039
  • 1
  • 15
  • 13
0

To check if $hideMyControl is in Velocity context and IS NOT boolean 'true' value (or 'false' as well):

#if ($hideMyControl && $hideMyControl != true)
    ##do stuff
#end

Sure, if you really use your $hideMyControl variable as boolean type, you don't need second part of condition.

user07
  • 319
  • 2
  • 8