30

速度では、その値がnullである変数があります。その場合は何も表示したくありません。

現在、テンプレートエンジンは ""をnullに変換するので、実行する必要があります。

#set ( $a = "")
#if ($a) 
   assert("never prints a neither gets here: " + $a)
#end

それを直接行う方法はありますか?次のようなものを作成できるようにしたいと思います。

This is the variable $a. ## in case that $a is null i don't want 'dollar a' to be displayed
4

3 に答える 3

24

あなたは静かな参照記法が欲しい:$!a

これがあなたの例です:

This is the variable $!a.

$anullまたはの場合""、Velocity は以下をレンダリングします。

This is the variable .

公式ガイド セクション: https://velocity.apache.org/engine/devel/user-guide.html#quietreferencenotation

于 2014-02-14T12:14:20.733 に答える
1

もう 1 つの方法は、Checking for Nullifごとにステートメントを変更することです(リンク @xavi-lópez に感謝します)。

アプローチ 2: 静かな参照では null が空の文字列として評価されるという事実を利用します。(参照 http://velocity.apache.org/engine/devel/user-guide.html#quietreferencenotation )

したがって、コードは次のようになります。

#set ( $a = "")
#if ("$a" != "") 
   assert("never prints a neither gets here: " + $a)
#end
于 2018-07-31T22:07:00.663 に答える