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>