Conditional Statements

Pengenalan

NexaDom menyediakan sintaks kondisional yang powerful untuk logika template, dengan dukungan untuk kondisional dasar hingga yang kompleks. Sistem ini mendukung tiga mode: mode standar, mode enhanced, dan mode stable yang dapat diaktifkan secara eksplisit.

Info:
  • Untuk menggunakan fitur kondisional yang lebih advanced, aktifkan enhanced conditionals dengan memanggil enable_enhanced_conditionals()
  • Untuk stabilitas yang lebih baik, gunakan stable conditionals yang tersedia di versi terbaru

Basic Conditionals

1. If Statement



{if user.is_logged_in}
    Dashboard
{endif}


{if cart.items > 0}
    {cart.items} items
{else}
    Cart empty
{endif}


{if user.role == 'admin'}
    Admin Panel
{elseif user.role == 'moderator'}
    Mod Panel
{else}
    User Panel
{endif}
    

2. Comparison Operators



{if status === 'active'} 
    Active User
{endif}

{if role !== 'guest'} 
    Member Content
{endif}


{if user.age >= 18}
    Adult Content
{endif}

{if stock < 10}
    Low Stock Warning
{endif}
    

Enhanced Conditionals

Enhanced conditionals menyediakan fitur yang lebih powerful dan fleksibel. Aktifkan dengan enable_enhanced_conditionals().

1. Complex Logical Operators



{if (user.age >= 18 && user.verified) || (user.has_parent_consent && user.parent_verified)}
    Content Available
{endif}


{if user.subscription.status === 'active' && (user.credits > 100 || user.is_premium)}
    Premium Features Available
{endif}
    

2. Advanced Value Comparison



{if user.points === 100 && typeof(user.rank) === 'string'}
    Rank: {user.rank}
{endif}


{if in_array(user.role, ['admin', 'moderator', 'supervisor'])}
    Staff Access Granted
{endif}
    

Stable Conditionals

Stable conditionals menyediakan evaluasi yang lebih reliable dan konsisten.

1. Value Resolution



{if isset(user.preferences) && user.preferences.theme !== null}
    Theme: {user.preferences.theme}
{endif}


{if user.id === '123'} 
    Exact Match
{endif}
    

2. Helper Functions



{if is_numeric(user.id) && is_string(user.name)}
    Valid User Data
{endif}


{if can_access_feature('premium_content')}
    Premium Content Here
{endif}
    

Debugging Tools

NexaDom menyediakan tools untuk debugging kondisional:

1. Condition Debugging



{debug_condition("user.role === 'admin' && user.is_active")}


{debug_condition("(user.age >= 18 && user.verified) || user.has_parent_consent")}
    

2. Switch Statement Debugging



{debug_switch user.status}
    {case 'active'}
        Active User
    {endcase}
    {case 'pending'}
        Pending User
    {endcase}
{endswitch}
    

Best Practices

  • Gunakan enhanced conditionals untuk logika yang kompleks
  • Manfaatkan stable conditionals untuk evaluasi yang lebih reliable
  • Gunakan helper functions untuk logika yang sering digunakan
  • Manfaatkan debugging tools untuk troubleshooting
  • Hindari nested conditions yang terlalu dalam
  • Gunakan type-safe comparisons (===, !==)
  • Validasi nilai sebelum comparison dengan isset()
Warning:
  • Pastikan mode yang sesuai telah diaktifkan (enhanced/stable)
  • Perhatikan performa saat menggunakan kondisi kompleks
  • Gunakan debugging tools dengan bijak di production