> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mellea.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# mellea.formatters.template_formatter

> `TemplateFormatter`: Jinja2-template-based formatter for legacy backends.

export const SidebarFix = () => <script dangerouslySetInnerHTML={{
  __html: `
        (function () {
          const INTERVAL_MS = 500;

          const upgradeSidebar = () => {
            const links = document.querySelectorAll('a[href^="#"]');

            links.forEach((link) => {
              if (link.dataset.badged === "true") return;

              const rawText = (link.textContent || "").trim();

              // ========== FUNC ==========
              if (rawText.startsWith("FUNC ")) {
                const label = rawText.replace(/^FUNC\\s+/, "").trim();

                while (link.firstChild) link.removeChild(link.firstChild);

                // 👉 Make the whole link a single flex row & prevent wrapping
                link.style.display = "flex";
                link.style.alignItems = "center";
                link.style.whiteSpace = "nowrap";
                link.style.columnGap = "0.5rem";

                const badge = document.createElement("span");
                badge.style.marginRight = "0.5rem";
                badge.style.display = "inline-flex";
                badge.style.alignItems = "center";
                badge.style.borderRadius = "9999px";
                badge.style.padding = "0rem 0.6rem";
                badge.style.fontSize = "0.5rem";
                badge.style.fontWeight = "700";
                badge.style.letterSpacing = "0.05em";
                badge.style.backgroundColor = "rgba(48, 100, 227, 0.20)";
                badge.style.color = "#1D4ED8";

                badge.textContent = "FUNC";

                link.appendChild(badge);
                link.appendChild(document.createTextNode(label));
                link.dataset.badged = "true";
                return;
              }

              // ========== CLASS ==========
              if (rawText.startsWith("CLASS ")) {
                const label = rawText.replace(/^CLASS\\s+/, "").trim();

                while (link.firstChild) link.removeChild(link.firstChild);

                // 👉 Same flex / nowrap treatment for class links
                link.style.display = "flex";
                link.style.alignItems = "center";
                link.style.whiteSpace = "nowrap";
                link.style.columnGap = "0.5rem";

                const badge = document.createElement("span");
                badge.style.marginRight = "0.5rem";
                badge.style.display = "inline-flex";
                badge.style.alignItems = "center";
                badge.style.borderRadius = "9999px";
                badge.style.padding = "0rem 0.6rem";
                badge.style.fontSize = "0.5rem";
                badge.style.fontWeight = "700";
                badge.style.letterSpacing = "0.05em";
                badge.style.backgroundColor = "rgba(74, 222, 128, 0.20)";
                badge.style.color = "#15803D";

                badge.textContent = "CLASS";

                link.appendChild(badge);
                link.appendChild(document.createTextNode(label));
                link.dataset.badged = "true";
                return;
              }
            });
          };

          upgradeSidebar();
          setInterval(upgradeSidebar, INTERVAL_MS);
        })();
      `
}} />;

<SidebarFix />

`TemplateFormatter`: Jinja2-template-based formatter for legacy backends.

`TemplateFormatter` extends [`ChatFormatter`](chat_formatter#class-chatformatter) to look up a per-component Jinja2
template at rendering time, allowing each [`Component`](../core/base#class-component) type to control its own
prompt representation. Template discovery walks a configurable `template_path` and
the built-in templates directory; results are cached in a [`SimpleLRUCache`](../backends/cache#class-simplelrucache) for
performance. Use this formatter when your backend requires hand-crafted prompts
rather than a generic chat-message rendering.

## Classes

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#4ADE8033]/20 text-[#15803D]">CLASS</span> `TemplateFormatter` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/formatters/template_formatter.py#L27" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Formatter that uses Jinja2 templates to render components into prompt strings.

Template discovery walks a configurable `template_path` and the built-in
templates directory. Results are optionally cached in a [`SimpleLRUCache`](../backends/cache#class-simplelrucache)
for performance. Use this formatter when your backend requires hand-crafted
prompts rather than generic chat-message rendering.

**Args:**

* `model_id`: Describes the model for which templates will be looked up.
  Should match the template directory structure.
* `template_path`: An alternate location where templates can be found.
  Will be preferred over all other template directories even if a less exact match is found.
  Defaults to `""`.
* `use_template_cache`: When `True`, caches template lookup results. Set to `False`
  if you plan to change `model_id` or `template_path` after construction.
  Defaults to `True`.

Example::

formatter = TemplateFormatter(model\_id="my-model", template\_path="/path/to/templates")
text = formatter.print(my\_component)

<div className="h-8" />

**Methods:**

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

#### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#3064E3]/20 text-[#1D4ED8]">FUNC</span> `print` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/formatters/template_formatter.py#L155" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
print(self, c: Component | CBlock) -> str
```

Render a component or code block to a string using a Jinja2 template.

**Args:**

* `c`: The component or code block to render.

**Returns:**

* The rendered string representation of the component.

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />
