Guide
Essentials
- Installation
- Introduction
- The Kdu Instance
- Template Syntax
- Computed Properties and Watchers
- Class and Style Bindings
- Conditional Rendering
- List Rendering
- Event Handling
- Form Input Bindings
- Components Basics
Components In-Depth
- Component Registration
- Props
- Custom Events
- Slots
- Dynamic & Async Components
- Handling Edge Cases
Transitions & Animation
- Enter/Leave & List Transitions
- State Transitions
Reusability & Composition
- Mixins
- Custom Directives
- Render Functions & JSX
- Plugins
- Filters
Tooling
- Single File Components
- TypeScript Support
- Production Deployment
Scaling Up
- Routing
- State Management
- Server-Side Rendering
- Security
Internals
- Reactivity in Depth
Migrating
- Migration to Kdu 2.7
Meta
- Meet the Team
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.
|
It is also possible to add an “else block” with k-else
:
|
Conditional Groups with 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.
|
k-else
You can use the k-else
directive to indicate an “else block” for k-if
:
|
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:
|
Similar to k-else
, a k-else-if
element must immediately follow a k-if
or a k-else-if
element.
Controlling Reusable Elements with key
Kdu tries to render elements as efficiently as possible, often re-using them instead of rendering from scratch. Beyond helping make Kdu very fast, this can have some useful advantages. For example, if you allow users to toggle between multiple login types:
|
Then switching the loginType
in the code above will not erase what the user has already entered. Since both templates use the same elements, the <input>
is not replaced - just its placeholder
.
Check it out for yourself by entering some text in the input, then pressing the toggle button:
This isn’t always desirable though, so Kdu offers a way for you to say, “These two elements are completely separate - don’t re-use them.” Add a key
attribute with unique values:
|
Now those inputs will be rendered from scratch each time you toggle. See for yourself:
Note that the <label>
elements are still efficiently re-used, because they don’t have key
attributes.
k-show
Another option for conditionally displaying an element is the k-show
directive. The usage is largely the same:
|
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.
Note that 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
Using k-if
and k-for
together is not recommended. See the style guide for further information.
When used together with k-if
, k-for
has a higher priority than k-if
. See the list rendering guide for details.