An open API service indexing awesome lists of open source software.

https://github.com/nixphp/view

NixPHP View Plugin for simple templating.
https://github.com/nixphp/view

framework nixphp php php8 plugin templating view

Last synced: 3 months ago
JSON representation

NixPHP View Plugin for simple templating.

Awesome Lists containing this project

README

          

![Logo](https://nixphp.github.io/docs/assets/nixphp-logo-small-square.png)

[![NixPHP View Plugin](https://github.com/nixphp/view/actions/workflows/php.yml/badge.svg)](https://github.com/nixphp/view/actions/workflows/php.yml)

[← Back to NixPHP](https://github.com/nixphp/framework)

---

# nixphp/view

> **A lightweight, native PHP templating system — with layout inheritance and block support.**

This plugin brings a clean, minimal templating system to your NixPHP application.
It lets you define base layouts, use content blocks, and safely output user data — all with pure PHP.

> 🧩 Part of the official NixPHP plugin collection.
> Install it when you need structured HTML rendering — without external engines like Twig or Blade.

---

## 📦 Features

* ✅ Define layouts and reuse views via `setLayout()` and `block()/endblock()`
* ✅ Render views with `view('template', [...])`
* ✅ Return response objects with `render('template', [...])`
* ✅ Safe output via `s()` (escape helper)
* ✅ Fully native PHP – no new syntax or templating engine required

---

## 📥 Installation

```bash
composer require nixphp/view
```

The plugin auto-registers itself and adds the `view()`, `render()`, `assets()` and `s()` helpers globally.

---

## 🚀 Usage

### 🧱 Rendering views

Use the `view()` helper to render a template and return the result as a **string**:

```php
$content = view('hello', ['name' => 'World']);
```

This is useful if you want to process or wrap the HTML manually.

Use the `render()` helper to return a **response object** instead (e.g. in your controller):

```php
return render('hello', ['name' => 'World']);
```

This renders the view **and wraps it in a proper response**, ready to be returned from any route handler.

To load a template file in another folder, you can use the dot notation:
```php
return render('pages.hello', ['name' => 'World']);
```

Or even multiple levels:

```php
return render('pages.elements.hello', ['name' => 'World']);
```

This works for both `view()` and `render()`.

---

### 🧩 Layouts & Blocks

Use `setLayout()` to define a parent layout, and `block()/endblock()` to inject content:

#### `app/views/page.phtml`

```php
setLayout('layout') ?>

block('content') ?>

Hello = s($name) ?>!


endblock('content') ?>
```

#### `app/views/layout.phtml`

```php

My App

= $this->renderBlock('content') ?>

```

---

### 🛡️ Escape output

Use the `s()` helper to sanitize output (HTML-escaped):

```php

= s($userInput) ?>


```

---

## 🎨 Asset Management (CSS & JS)

The plugin includes a small, flexible asset collector used inside layouts to include CSS and JavaScript files.

Assets are added inside views or controllers using the `assets()` helper:

```php
assets()->add('/assets/style.css'); // CSS
assets()->add('/assets/app.js'); // JavaScript (classic)
assets()->add('/assets/main.js', 'module'); // JavaScript ES module
```

### Output in layout files

Use `assets()->render('css')` or `assets()->render('js')` inside your layout:

```php

= assets()->render('css') ?>

= $this->renderBlock('content') ?>
= assets()->render('js') ?>

```

### What gets generated?

**CSS:**

```html

```

**Classic JS:**

```html

```

**Module JS:**

```html

```

### Internals

**All paths are automatically HTML-escaped via `s()`.**

---

## 🔁 Helper Comparison

| Helper | Returns | Use case |
| ---------- | ------------------- | --------------------------------------- |
| `render()` | `ResponseInterface` | Ideal for controller return values |
| `view()` | `string` | For manual output or further processing |
| `assets()` | `string` | Include CSS & JS files in layouts |
| `s()` | `string` | Escape output |

---

## 🔍 Internals

* `view()` resolves and loads `.phtml` templates from `/app/views/` or any registered view path.
* `setLayout()` nests the rendered content into a wrapper view.
* Blocks are buffered and stored internally until rendered.

---

## ✅ Requirements

* `nixphp/framework` >= 1.0

---

## 📄 License

MIT License.