File Templates

Pengenalan

Sistem template NexaDom menyediakan mekanisme render yang fleksibel dengan dukungan multiple layouts dan device-specific templates. Sistem ini memungkinkan pengembang untuk mengelola tampilan berdasarkan device dan kebutuhan spesifik.

Metode Utama

1. Basic Rendering


// Render template dasar
$this->render('index');

// Render dengan data tambahan
$this->render('dashboard', [
    'user_name' => 'John Doe',
    'role' => 'admin'
]);

// Render dengan return value
$content = $this->render('partial/header', [], true);
    

2. Layout Management


// Set layout spesifik
$this->setLayout('admin');

// Set device type untuk responsive template
$this->setDeviceType('mobile');

// Mendapatkan default template
$defaultTemplate = $this->getDefaultTemplate();
    

3. Device-Specific Templates


class YourController extends NexaController {
    protected function init(): void {
        parent::init();
        
        // Setup device layouts
        $this->setDeviceType('mobile');
        // atau
        $this->setDeviceType('tablet');
        // atau
        $this->setDeviceType('desktop');
    }
}
    

Struktur Template


templates/
  ├── desktop/
  │   ├── layout.html
  │   └── pages/
  ├── mobile/
  │   ├── layout.html
  │   └── pages/
  ├── tablet/
  │   ├── layout.html
  │   └── pages/
  └── shared/
      └── components/
    

Fitur Utama

  • Device Detection: Otomatis deteksi dan load template sesuai device
  • Layout System: Mendukung multiple layouts untuk berbagai kebutuhan
  • Template Inheritance: Sistem template yang hierarkis
  • Flexible Rendering: Opsi render yang dapat dikustomisasi

Template Inheritance






    file_templates


    
    {content}
    





Welcome, {user_name}!

Best Practices

  • Selalu set device type di init() method
  • Gunakan shared components untuk kode yang reusable
  • Manfaatkan template inheritance untuk konsistensi layout
  • Pisahkan logic dari template files

Device-Specific Configuration


// Dalam Controller
protected function init(): void {
    parent::init();
    
    // Auto detect device
    if ($this->isMobile()) {
        $this->setDeviceType('mobile');
    } elseif ($this->isTablet()) {
        $this->setDeviceType('tablet');
    } else {
        $this->setDeviceType('desktop');
    }
    
    // Set default layout
    $this->setLayout('main');
}
    
Note: Sistem template mendukung nested layouts dan device-specific rendering. Pastikan struktur folder sesuai dengan device types yang didukung.

Debugging Templates


// Debug template resolution
$this->debugRequest();

// Check template existence
if ($this->isExists('dashboard', 'admin')) {
    $this->render('dashboard');
}