> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-framework/interceptors/core-interception-points/template-invocations.md).

# Template Invocations

These events fire during the invocation of BoxLang templates. They are announced on the **global interceptor pool**.

> **Template scope:** `preTemplateInvoke` and `postTemplateInvoke` fire for every template invocation, including `include` calls and nested templates — not just the top-level request template.

| Event Name           | Cancellable | Description                                                                          |
| -------------------- | :---------: | ------------------------------------------------------------------------------------ |
| `preTemplateInvoke`  |      No     | Fired before every template executes.                                                |
| `postTemplateInvoke` |      No     | Fired after every template finishes — always fires, even if an exception was thrown. |

* [`preTemplateInvoke`](#pretemplateinvoke)
* [`postTemplateInvoke`](#posttemplateinvoke)

## preTemplateInvoke

Fired immediately before a BoxLang template (`.bx` or `.bxm` file) begins executing. The template has been pushed onto the template stack at this point.

### Data Structure

| Data Key       | Type               | Description                                     |
| -------------- | ------------------ | ----------------------------------------------- |
| `context`      | `IBoxContext`      | The context in which the template is executing. |
| `template`     | `BoxTemplate`      | The template runnable about to be invoked.      |
| `templatePath` | `ResolvedFilePath` | The resolved file path of the template.         |

### Example

```groovy
class myListener{
	function preTemplateInvoke( struct data ){
		// Log execution, inject variables, enforce access control, etc.
		var templatePath = data.templatePath;
	}
}
```

## postTemplateInvoke

Fired after a BoxLang template finishes executing. This is announced inside a `finally` block, so it **always fires** regardless of whether the template threw an exception. The template is still on the stack when this fires and is popped immediately after.

### Data Structure

| Data Key       | Type               | Description                                 |
| -------------- | ------------------ | ------------------------------------------- |
| `context`      | `IBoxContext`      | The context in which the template executed. |
| `template`     | `BoxTemplate`      | The template runnable that was invoked.     |
| `templatePath` | `ResolvedFilePath` | The resolved file path of the template.     |

### Example

```groovy
class myListener{
	function postTemplateInvoke( struct data ){
		// Collect timing metrics, clean up injected variables, etc.
		var templatePath = data.templatePath;
	}
}
```


---

# 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://boxlang.ortusbooks.com/boxlang-framework/interceptors/core-interception-points/template-invocations.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.
