2011-01-28 71 views
27

我想包括一个模板nested到其他cont1,cont2,cont3。 嵌套模板应该只隐藏cont1的一个特定控件。 在纳入cont1之前,我想给一些标志变量$hideMyControl赋值。速度:是否有任何方法来检查是否定义变量

而内嵌套模板我想检查是否为$hideMyControl赋值。

如何执行此类检查?

回答

13

你可以做到这一点使用

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

欲了解更多信息,请参阅Apache Velocity Reference Manual的“正式引用”部分。

+11

无论如何,在#if中使用正式和无声的符号是没有意义的。只要做#if($ article)##在这里执行操作#end – 2011-01-28 18:13:57

+2

那么检查它是否没有定义呢? – Snekse 2013-06-21 18:30:24

+0

#if($!{$ articleLeader})没有工作, #if(!$ {articleLeader})did ... – 2014-12-01 08:41:01

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

如果$ hideMyControl定义,你的代码将执行

0

要检查是否$ hideMyControl是在速度方面和IS NOT布尔 '真' 值(或 '假' 也一样):

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

当然,如果你确实使用$ hideMyControl变量作为布尔类型,你不需要第二部分条件。

3
#if($!{hideMyControl} != "") 
## do something if $hideMyControl is defined 
#end 

这适用于AWS API网关正文映射模板中的我。有关更多信息,请参阅Velocity用户指南中的Quiet Reference Notation

1

我用

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

,因为几个月前, 但今天它不再工作。

我来到这里寻求帮助,并注意到写它的一个新的方式:

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

此代码的工作!

0

根据docs for Strict Reference Mode可以通过几种结构来检查变量是否被定义。

#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 

所以这个代码在我的情况。

#if(!$value) 
    // Perform your operation or the template part you want to show. 
#end 
相关问题