# decorator

URL: /patitas/api/directives/decorator/
Section: directives
Description: @directive decorator for reducing directive boilerplate.

Provides a concise decorator API for creating directive handlers with
minimal boilerplate. Works with both functions and classes.

Example (function):
    >>> @directive("note", options=NoteOptions)
    >>> def render_note(node: Directive[NoteOptions], children: str, sb: StringBuilder) -> None:
    ...     sb.append(f'<div class="note">{children}</div>')

Example (class):
    >>> @directive("gallery", options=GalleryOptions, preserves_raw_content=True)
    >>> class GalleryDirective:
    ...     def render(self, node, children: str, sb: StringBuilder) -> None:
    ...         images = self._parse_images(node.raw_content)
    ...         ...

---

> For a complete page index, fetch /patitas/llms.txt.

Open LLM text
(/patitas/api/directives/decorator/index.txt)

Share with AI

Ask Claude
(https://claude.ai/new?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fpatitas%2Fapi%2Fdirectives%2Fdecorator%2Findex.txt)

Ask ChatGPT
(https://chatgpt.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fpatitas%2Fapi%2Fdirectives%2Fdecorator%2Findex.txt)

Ask Gemini
(https://gemini.google.com/app?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fpatitas%2Fapi%2Fdirectives%2Fdecorator%2Findex.txt)

Ask Copilot
(https://copilot.microsoft.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fpatitas%2Fapi%2Fdirectives%2Fdecorator%2Findex.txt)

Module

#
`directives.decorator`

@directive decorator for reducing directive boilerplate.

Provides a concise decorator API for creating directive handlers with
minimal boilerplate. Works with both functions and classes.

Example (function):
>>> @directive("note", options=NoteOptions)
>>> def render_note(node: Directive[NoteOptions], children: str, sb: StringBuilder) -> None:
... sb.append(f'
{children}
')

Example (class):
>>> @directive("gallery", options=GalleryOptions, preserves_raw_content=True)
>>> class GalleryDirective:
... def render(self, node, children: str, sb: StringBuilder) -> None:
... images = self._parse_images(node.raw_content)
... ...

1Function

## Functions

`directive`

5

`Callable[[RenderFunc | t…`

▼

Decorator to create directive handlers with minimal boilerplate.

Works with bo…

`def directive(*names: str, options: type[DirectiveOptions] | None = None, contract: DirectiveContract | None = None, preserves_raw_content: bool = False, token_type: str | None = None) -> Callable[[RenderFunc | type], type]`

Decorator to create directive handlers with minimal boilerplate.

Works with both functions (simple directives) and classes (complex directives).

##### Parameters

Name
Type
Description

`*names`
`str`

`options`
`type[DirectiveOptions (/patitas/api/directives/options/#DirectiveOptions)] | None`

Options class for typed option parsing

Default:`None`

`contract`
`DirectiveContract (/patitas/api/directives/contracts/#DirectiveContract) | None`

Optional nesting validation contract

Default:`None`

`preserves_raw_content`
`bool`

If True, parser preserves raw content string

Default:`False`

`token_type`
`str | None`

Token type identifier (defaults to first name)

Default:`None`

##### Returns

`Callable[[RenderFunc | type], type]`
