Now let's learn how to access this data cleanly in StarHTML templates.
💡 Two Approaches: Messy vs Clean
There are two ways to access nested properties. See the difference:
# ❌ Hard to read, error-prone, and unmaintainable
Span(data_text=user_data.profile.preferences.theme)
Button(data_on_click=user_data.profile.preferences.notifications.email.set(~user_data.profile.preferences.notifications.email))
Span(data_text=user_data.profile.preferences.notifications['push'])
Input(data_bind=user_data.profile.preferences.theme)
# ✅ Clean, readable, and maintainable
profile = user_data.profile
preferences = profile.preferences
notifications = preferences.notifications
# Now use clean references:
Span(data_text=preferences.theme)
Button(data_on_click=notifications.email.set(~notifications.email))
Input(data_bind=preferences.theme)
📍 Start Simple: Top-Level Properties
Begin with straightforward, top-level properties from our data structure:
👤 User ID:
📛 Name:
# Direct access to top-level properties
user_data.id # Simple and clean
user_data.name # No intermediate variables needed
🔗 Add One Level: Intermediate Variables
When you need nested data, extract logical sections:
📝 Bio:
🎨 Theme:
# Extract logical sections first
profile = user_data.profile
preferences = profile.preferences
# Then use clean references
profile.bio # Much cleaner!
preferences.theme # Easy to read
📦 Group Related Data
Extract common parents for related properties:
📧 Email notifications:
🔔 Push notifications:
# Group related properties under common parent
notifications = preferences.notifications
# All notification properties are now clean
notifications.email
notifications['push'] # Bracket notation for reserved words