Skip to content

Conditional Rendering

k-if

The directive k-if is used to conditionally render a block. The block will only be rendered if the directive's expression returns a truthy value.

<h1 k-if="awesome">Kdu is awesome!</h1>

k-else

You can use the k-else directive to indicate an "else block" for k-if:

<button @click="awesome = !awesome">Toggle</button>

<h1 k-if="awesome">Kdu is awesome!</h1>
<h1 k-else>Oh no 😢</h1>

Kdu is awesome!

A k-else element must immediately follow a k-if or a k-else-if element - otherwise it will not be recognized.

k-else-if

The k-else-if, as the name suggests, serves as an "else if block" for k-if. It can also be chained multiple times:

<div k-if="type === 'A'">
  A
</div>
<div k-else-if="type === 'B'">
  B
</div>
<div k-else-if="type === 'C'">
  C
</div>
<div k-else>
  Not A/B/C
</div>

Similar to k-else, a k-else-if element must immediately follow a k-if or a k-else-if element.

k-if on <template>

Because k-if is a directive, it has to be attached to a single element. But what if we want to toggle more than one element? In this case we can use k-if on a <template> element, which serves as an invisible wrapper. The final rendered result will not include the <template> element.

<template k-if="ok">
  <h1>Title</h1>
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
</template>

k-else and k-else-if can also be used on <template>.

k-show

Another option for conditionally displaying an element is the k-show directive. The usage is largely the same:

<h1 k-show="ok">Hello!</h1>

The difference is that an element with k-show will always be rendered and remain in the DOM; k-show only toggles the display CSS property of the element.

k-show doesn't support the <template> element, nor does it work with k-else.

k-if vs k-show

k-if is "real" conditional rendering because it ensures that event listeners and child components inside the conditional block are properly destroyed and re-created during toggles.

k-if is also lazy: if the condition is false on initial render, it will not do anything - the conditional block won't be rendered until the condition becomes true for the first time.

In comparison, k-show is much simpler - the element is always rendered regardless of initial condition, with CSS-based toggling.

Generally speaking, k-if has higher toggle costs while k-show has higher initial render costs. So prefer k-show if you need to toggle something very often, and prefer k-if if the condition is unlikely to change at runtime.

k-if with k-for

Note

It's not recommended to use k-if and k-for on the same element due to implicit precedence. Refer to style guide for details.

When k-if and k-for are both used on the same element, k-if will be evaluated first. See the list rendering guide for details.

Conditional Rendering has loaded