If else
用来做条件判断,判断为真显示, 否则隐藏。
k-if 与 v-if 的语法与行为是一样的
真的情况是JavaScript里面叫 Truthy 的值
参考:
示例1 简单比较
代码:
<script env="server">
var value1 = 3;
var value2 = 1;
</script>
<div k-if='value1 > 2'>value1 bigger than 2</div><div k-else>value1 smaller than 2</div>
<div k-if='value2 > 2'>value2 bigger than 2</div><div k-else>value2 smaller than 2</div>
结果:
<div>value1 bigger than 2</div>
<div>value2 smaller than 2</div>
示例2, Truthy
代码
<script env="server">
var value1 = "anyvalue"
var value2;
</script>
<div k-if="value1">value1 has value</div><div k-else>value1 no value</div>
<div k-if="value2">value2 has value</div><div k-else>value2 no value</div>
结果
<div>value1 has value</div>
<div>value2 no value</div>
示例3, JS function
<script env=server>
function abc(){return 'yes'}
</script>
<div k-if='abc()'> text</div>
示例4, repeat counter
可以用的参数包含
repeat/!first
repeat/first
repeat/last
repeat/!last
repeat/even
repeat/odd
repeat/2n+1
<div k-for="n in 10" repeat-self="true">
<span k-content="n"></span>: <span>all row</span>
<span k-if="repeat/!first">!First</span>
<span k-if="repeat/first">First</span>
<span k-if="repeat/last">Last</span>
<span k-if="repeat/!last">!Last </span>
<span k-if="repeat/even">Even</span>
<span k-if="repeat/odd">Odd</span>
<span k-if="repeat/2n+1">every 2 row</span>
<span k-if="repeat/3n+1">every 3 row</span>
</div>
运行的结果,输出 HTML的展示
1: all row First !Last Odd every 2 row every 3 row
2: all row !First !Last Even
3: all row !First !Last Odd every 2 row
4: all row !First !Last Even every 3 row
5: all row !First !Last Odd every 2 row
6: all row !First !Last Even
7: all row !First !Last Odd every 2 row every 3 row
8: all row !First !Last Even
9: all row !First !Last Odd every 2 row
10: all row !First Last Even every 3 row