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

# Contracts

This page covers the behaviors you can rely on when integrating with the View package.

## Layout contract

### `setLayout(?string $layoutFile, array $assets = []): void`

Use this before `render()`.

Contract:

* stores the layout file name for later full renders
* appends any provided assets to the current layout asset list
* accepts `null`, which clears the current layout selection while leaving the accumulated asset list available for later full renders

`render()` reads whatever layout is currently stored. It does not accept a layout argument of its own.

## Parameter contract

### `setParam()` and `setParams()`

These methods add data into the shared param bag.

Contract:

* later values overwrite earlier values for the same key
* `render()` and `renderPartial()` both merge their `$params` argument into the same shared bag
* params remain available after rendering until `flushParams()` is called or a key is overwritten
* `getParams()` and `getParam()` return raw values, so `RawParam` wrappers stay an internal escape-hatch detail

### `setRawParam()`

Stores one param as a `RawParam` wrapper.

Use it only for trusted HTML or content you intentionally do not want escaped.

### `getParam()` and `getParams()`

These methods always return the unwrapped value.

If a param was stored as `RawParam`, callers receive the original value rather than the wrapper object.

## Render contract

### `render(string $viewFile, array $params = []): ?string`

Contract:

* requires a layout to be set first
* renders the page template first, then the layout
* returns the final layout HTML
* stores the rendered page body so `getContent()` can return it afterward
* registers any assets collected through `setLayout()` before the layout render begins

Failure behavior:

* throws `ViewException` when no layout is set
* can also surface renderer, asset, config, DI, session, database, or app-level exceptions from its dependencies

### `renderPartial(string $viewFile, array $params = []): string`

Contract:

* does not require a layout
* returns the requested fragment HTML
* keeps the existing full-render page body available through `getContent()`
* merges the provided params into the shared param bag before rendering

Use it for partials, snippets, or error fragments that should not go through the full page pipeline.

## Content contract

### `getContent(): ?string`

Returns the last body view generated by `render()`, including after later partial renders.

Failure behavior:

* throws `ViewException` if no full render has happened yet

If your code only used `renderPartial()`, `getContent()` is still considered unavailable.

## Cache contract

`render()` renders body and layout, then writes the final HTML using the current request URI as the cache key when view cache is enabled.

Important boundary:

* caching happens after both the body and layout are rendered
* this path does not short-circuit rendering before layout generation
* only full `render()` participates
* no request URI means no cache write/read path


---

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