nixphp/view

NixPHP View Plugin with simple templating.

Maintainers

Details

github.com/nixphp/view

Source

Issues

Installs: 3

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:nixphp-plugin

dev-main 2025-05-24 20:44 UTC

This package is auto-updated.

Last update: 2025-05-24 20:45:03 UTC


README

Logo

NixPHP View Plugin

← Back to NixPHP

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

composer require nixphp/view

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

🚀 Usage

🧱 Rendering views

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

$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):

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

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

🧩 Layouts & Blocks

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

app/views/page.phtml

<?php $this->setLayout('layout') ?>

<?php $this->block('content') ?>
    <h1>Hello <?= s($name) ?>!</h1>
<?php $this->endblock('content') ?>

app/views/layout.phtml

<!doctype html>
<html>
<head>
    <title>My App</title>
</head>
<body>
    <?= $this->renderBlock('content') ?>
</body>
</html>

🛡️ Escape output

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

<p><?= s($userInput) ?></p>

🔁 Helper Comparison

Helper Returns Use case
view() string For manual output or further processing
render() ResponseInterface Ideal for controller return values

🔍 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.