View
8/8/25Less than 1 minute
Description:
Register Service tại ViewServiceProvider.php
Mặc định sử dụng twig template engine.
public $bindings = [ ViewFactory::class => TwigFactory::class, ];
→ Nếu muốn thay đổi engine chỉ cần binding ViewFactory cho 1 class khác vd: BladeFactory, SmartyFactory,..
Config:
# app.yml
view:
dir_loader: src/templates
Usage:
Type-hinting tại controller contructor:
<?php namespace Nin\Controllers; use Nin\Libs\View\ViewFactory; class FooController { protected ViewFactory $view; public function __construct(ViewFactory $view) { $this->view = $view; } public function indexAction() { $foo = [ ['name' => 'A'], ['name' => 'B'], ['name' => 'C'], ]; return $this->view->make('index', ['foo' => $foo]); } }
// src/templates/index.html.twig <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Twig Example</title> </head> <body> <ul> {% for item in foo %} <li>{{ item.name }}</li> {% endfor %} </ul> </body> </html>
Facade:
use Nin\Libs\Facades\View; $foo = [ ['name' => 'A'], ['name' => 'B'], ['name' => 'C'] ]; return View::make('index', ['foo' => $foo]);
Packagist: https://packagist.org/packages/nin/nin