> For the complete documentation index, see [llms.txt](https://quantumphp.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://quantumphp.gitbook.io/docs/packages/renderer/usage.md).

# Usage

## Render with the configured default adapter

For most application code, resolve the default renderer and render a logical view name.

```php
use Quantum\Renderer\Factories\RendererFactory;

$renderer = RendererFactory::get();

return $renderer->render('home/index', [
    'title' => 'Dashboard',
    'user' => $user,
]);
```

If the current module contains `Views/home/index.php`, that file wins.

Otherwise the package falls back to `shared/views/home/index.php`.

## Pick an adapter explicitly

Use an explicit adapter when one code path must force PHP templates or Twig templates.

```php
$phpRenderer = RendererFactory::get('html');
$twigRenderer = RendererFactory::get('twig');
```

This creates separate cached renderer instances per adapter name.

## Write a PHP view for the HTML adapter

`HtmlAdapter` exposes params as local variables inside the template.

```php
// render call
$renderer->render('posts/show', [
    'post' => $post,
]);
```

```php
<!-- modules/Blog/Views/posts/show.php -->
<h1><?= htmlspecialchars($post->title) ?></h1>
```

Use the HTML adapter when you want direct PHP templates with no extra template engine layer.

## Write a Twig view

The Twig adapter still loads a `.php` file name, but the file contents should be Twig template syntax.

```php
$renderer = RendererFactory::get('twig');

echo $renderer->render('post-show', [
    'post' => $post,
]);
```

```twig
{# modules/Blog/Views/post-show.php #}
<h1>{{ post.title }}</h1>
```

Top-level logical names are the clearest fit for the built-in Twig adapter because it renders the matched file from that file's directory.

## Use PHP functions in Twig templates

The Twig adapter registers all currently defined PHP functions as Twig functions.

That means helpers already loaded into the runtime can be called directly:

```twig
<p>{{ base_url() }}</p>
```

Use this carefully. It is convenient for framework helpers, but it also makes template behavior depend on which functions are already defined in the current process.

## Handle common failures

Wrap rendering when missing templates or unsupported adapters should become application-level errors.

```php
try {
    $html = RendererFactory::get()->render('emails/welcome', ['user' => $user]);
} catch (Throwable $e) {
    // log, fallback, or convert to an HTTP error
}
```

## Common pitfalls

### Do not include the `.php` suffix in the view name

Use `posts/show`, not `posts/show.php`.

The built-in adapters append `.php` themselves.

### Do not expect shared views to override module views

Lookup is module-first.

If a module file exists, the shared file with the same logical name is ignored.

### Know that HTML and Twig keep the same filename convention

Switching from `html` to `twig` changes the rendering engine, not the filename convention.

The built-in Twig adapter still looks for `<view>.php`.

For Twig, favor top-level logical names such as `post-show` or `dashboard`. If you prefer nested template paths, the HTML adapter fits that layout directly and a custom Twig adapter can mirror it.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://quantumphp.gitbook.io/docs/packages/renderer/usage.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
