> 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/view/usage.md).

# Usage

## Render a page with a layout

The normal page flow is:

1. set a layout
2. add any shared params
3. call `render()` for the page body
4. send the returned HTML in the response

```php
view()->setLayout('layouts/main', [
    'css' => ['/css/app.css'],
    'js' => ['/js/app.js'],
]);

view()->setParams([
    'title' => 'Settings',
    'user' => $user,
]);

$html = view()->render('pages/settings');
```

Use this path when the page should inherit the application's main layout and asset output.

## Render a fragment

Use `renderPartial()` or `partial()` for reusable fragments:

```php
$html = partial('partials/flash', [
    'message' => 'Profile saved.',
]);
```

This does not require `setLayout()`.

## Pass trusted HTML intentionally

By default, string params are escaped before rendering.

If you already trust a value and want to output it as HTML, mark it explicitly:

```php
view()->setParam('content', raw_param($trustedHtml));
```

Do this sparingly. Normal user input should stay in the default escaped path.

## Read data back inside layout or shared code

The shared param bag is readable:

```php
$title = view_param('title');
```

`getContent()` is useful in a layout wrapper after a full page render has already happened:

```php
$body = view()->getContent();
```

## Reset shared state when needed

Because `view()` returns a shared instance, params and layout asset definitions can carry over between calls.

Use `flushParams()` when you intentionally want to clear previously assigned values before another render path. When you are switching from a full page flow to a different layout flow, pair that with a fresh `setLayout()` call.

```php
view()->flushParams();
view()->setLayout('layouts/account', [
    'css' => ['/css/account.css'],
]);
```

## Common pitfalls

### Forgetting to set a layout

`render()` throws immediately when no layout is set.

If you only need a fragment, use `renderPartial()` instead.

### Expecting `getContent()` after a partial render

`renderPartial()` does not store page content for later retrieval.

### Passing mutable objects with strings you still need elsewhere

The escaping step updates object string properties before rendering. Pass arrays, DTO copies, or raw values if you need the original object unchanged.


---

# 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/view/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.
