Velocity template If/elseif/else Syntax

Source: Internet
Author: User
12.3.1 If/elseif/else

The #if instruction in velocity allows text to be included when a page is generated, if the IF condition is true. For example:

#if ($foo)
   <strong>Velocity!</strong>
#end

The variable $foo evaluated first to determine whether it is true. True in either case: (i) $foo is a logical variable with a true value, or (ii) a value that is not empty. Remember that the velocity context includes only objects, so when we say "Boolean" Boolean, he is represented as "Boolean" (Boolean Class). This is true even for methods that return Boolean types-introspection schemas will return a Boolean class with the same logical value.

If the evaluation is true, the content between the #if and the #end statement is output. In this case, if the $foo is true, the output will be "velocity!". Conversely, if the $foo has a null value or is logically false and the statement evaluation is false, there is no output.

A #elseif or #else item can be used in a #if statement. Note that the Velocity template engine will stop when the first expression is true. In the following example, suppose $foo has a value of 15 and $bar equals 6.

#if ($foo <)
    <strong>go north</strong>
#elseif ($foo = a)
    <strong>go east</ Strong>
#elseif ($bar = 6)
    <strong>go south</strong>
#else
    <strong>go West </strong>
#end

In this example, the $foo is greater than 10, so the previous two comparisons fail. Next compares $bar and 6, the result is true, so the output is go South.

Note that at this time velocity's numeric comparison constraint is integral-all other types will be evaluated to false. There is only one exception equals ' = = ', at which point velocity requires the same type of object on both sides of the equal sign.


From:https://wizardforcel.gitbooks.io/velocity-doc/content/22.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.