Home  >  Article  >  Web Front-end  >  Freemarker method to determine whether an object is empty_javascript skills

Freemarker method to determine whether an object is empty_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:45:141343browse

FreeMarker has nothing to do with web containers, that is, it does not know about Servlets or HTTP when running on the web. It can not only be used as an implementation technology for the presentation layer, but can also be used to generate XML, JSP or Java, etc.

To display an object in freemarker, use ${name}.

But if name is null, freemarker will report an error. If you need to determine whether the object is empty:

<#if name&#63;&#63;>
……
</#if>

Of course, you can also avoid the error of the object being empty by setting the default value ${name!''}. If name is empty, the default value (characters after "!") will be displayed.

If the object user and name are attributes of user, both user and name may be empty, then it can be written as ${(user.name)!''}, which means that user or name is null, both will be displayed as empty.

Judged to be empty

<#if (user.name)&#63;&#63;>
……
</#if>

Method 2:

For null or miss value, freemarker will report an error

!: default value operator, the syntax structure is: unsafe_expr!default_expr, such as ${mouse!"No mouse."} When mouse does not exist, return default value;

(product.color)!"red" This method can handle the situation where product or color is a miss value;

And product.color!"red" will only handle the case where color is miss value

??: Missing value test operator, tests whether it is missing value

unsafe_expr??: product.color?? will only test if color is null

(unsafe_expr)??: (product.color)?? will test whether product and color are null

?exists:Usage of old version

For example:

<#if mouse&#63;&#63;>
 Mouse found
<#else>
 No mouse found
</#if>
Creating mouse...
<#assign mouse = "Jerry">
<#if mouse&#63;&#63;>
 Mouse found
<#else>
 No mouse found
</#if>

The above uses two methods to introduce the method of freemarker to determine whether an object is empty. I hope you like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn