DLight\Application\View

Mô tả: render view theo view path và view engine. Mặc định dùng PHP include; có thể bật engine (BladeOne/Twig…) qua .envconfig/view.php.

Namespace / đăng ký

  • Namespace: DLight\Application
  • File: src/Application/View.php

Public API

  • __construct(?string $viewPath = null)
  • View::render(string $view, array $data = [], string $viewPath = null): string
  • $view->view(string $view, array $data = []): string
  • View::include(string $view, array $data = []): string
  • View::abort(int $statusCode): bool|int

Cách dùng (PHP view mặc định)

use DLight\Application\View;

// Render view trong ROOT_DIR/resource/view/
echo View::render('client.home', [
    'title' => '<title>Home</title>',
    'name'  => 'DLight',
]);

Với engine mặc định (php), View sẽ thử nhiều extension: .php, .blade.php, .twig, .tpl, .html, .htm. Chuỗi client.home sẽ được chuyển thành client/home.

Include partial

use DLight\Application\View;

echo View::include('admin.dashboard', ['title' => '<title>Dashboard</title>']);

Bật view engine (BladeOne/Twig…)

.env:

SUPPORT_VIEW_ENGINE=enable

config/view.php (ví dụ BladeOne):

<?php
return [
  'view_path' => ROOT_DIR . 'resource/view/',
  'engine' => 'bladeone',
  'drives' => [
    'bladeone' => [
      'class' => DLight\\Application\\Drive\\View\\BladeOneDrive::class,
      'options' => [
        'cache' => true,
        'cache_path' => INDEX_DIR . 'cache/',
      ],
    ],
  ],
];

View engine phải có method render(string $template, array $data = []): string (xem Interfaces\ViewEngine).

Abort HTTP status

use DLight\Application\View;

View::abort(404);