> 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://boxlang.ortusbooks.com/boxlang-framework/interceptors/core-interception-points/template-invocations.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
