If, else, and unless expressions in Template7

Source: Internet
Author: User


Template7 provides abundant expression syntax (Expressions syntax) for our use. This article introduces the {# if }}... {else }}... {{/ if }}, {{# unless }}... {else }}... the two expressions: {/unless.

 

1. {# if}... {else}... {/if }}
{# If} determines whether the data is not false (or not "undefined" or "null" or "" or "0 "). If it is not false, go to {# if}; otherwise, go to the {else} branch.

1. Use if
(1) assume that context data

{
Active: true,
Title: 'link ',
}
(2) template example

<A href = "#" {{# if active} class = "active" {{/ if }}>{{ title }}</a>
(3) output results

<A href = "#" class = "active"> Link </a>

2. if used with else
(1) assume that context data

{
Name: 'John Doe ',
Holobby: false
}
(2) template example

<P> Hello, my name is {name }}. </p>
{{# If holobby }}
<P> I have holobby </p>
{Else }}
<P> I don't have holobby </p>
{/If }}
(3) output results

<P> Hello, my name is John Doe. </p>
<P> I don't have holobby </p>

II. {# unless}... {else}... {/unless }}
The {# unless} expression above is the opposite. Determines whether the data is false (or "undefined" or "null" or "" or "0 "). If it is false, go to {# unless}; otherwise, go to the {else} branch.
1. Use unless
(1) assume that context data

{
Active: true,
Title: 'link ',
}

(2) template example

<A href = "#" {{# unless active }}class = "active" {{/ unless }>{{ title }}</a>
(3) output results

<A href = "#"> Link </a>

2. Use unless with else
(1) assume that context data

{
Name: 'John Doe ',
Holobby: false
}

(2) template example

<P> Hello, my name is {name }}. </p>
{{# Unless holobby }}
<P> I have holobby </p>
{Else }}
<P> I don't have holobby </p>
{{/ Unless }}

(3) output results

<P> Hello, my name is John Doe. </p>
<P> I have holobby </p>

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.