[init]
The [init]
block is a special system block that runs once when your app first loads, before any other blocks execute.
Purpose
Use [init]
to set up your app’s initial state, configure settings, and prepare data that will be used throughout your app. It’s like the backstage preparation before the show begins.
Key Characteristics
- Runs automatically when the app loads
- Executes before
[start]
or any other block - Runs only once per app session
- No user interface actions allowed
- Perfect for initialization tasks
Common Uses
Setting Default Values
[init]
set $app-version = "2.1.0"
set $theme = "light"
set $language = "en"
set $items = []
Configuration
[init]
// API endpoints
set $api-base = "https://api.myapp.com"
set $image-cdn = "https://images.myapp.com"
// Feature flags
set $enable-chat = true
set $enable-notifications = false
Loading Saved Data
[init]
// Load user preferences
if $STORAGE.theme
set $theme = $STORAGE.theme
end
// Load saved user session
if $STORAGE.user-token
set $user-token = $STORAGE.user-token
set $logged-in = true
end
What You Can’t Do in [init]
The [init]
block is for setup only. You cannot:
- Display UI elements (
.text
,.button
, etc.) - Navigate to other blocks (
goto
,call
) - Interact with users
Best Practices
- Keep
[init]
focused on setup tasks - Set all default values your app needs
- Load any saved preferences or data
- Prepare empty lists or data structures
- Don’t perform time-consuming operations
Example App Structure
[init]
// Setup runs first
set $user-list = []
set $current-page = 1
set $items-per-page = 10
[start]
// Then the app begins
.title "Welcome!"
.text "App version: $app-version"
.button "Continue" [main]