Code
A labelled, scrollable code panel with optional copy enhancement.
Try it
Named Python snippet
A source panel with a language label.
Use it
Working invocation
This code creates the configuration shown above. Copy the template and the required Python context together.
{% include "brickwork/components/_code.html" with code=example_source filename=filename language=language id="component-example-view" %}
context = {'example_source': 'def component_view(request):\n return render(request, "example.html")', 'filename': 'views.py', 'language': 'Python'}
Guide
Add it to your project
- Include with plain source text or trusted, already-highlighted markup.
- Supply `filename` or `label` so the scrollable region is named.
- Supply a stable `id` if the filename repeats on a page.
Options
Public API
| Option | Type | Default | Required | Description |
|---|---|---|---|---|
| code | str|SafeString | Not set | Yes | Source text, or trusted highlighted markup. Constraint: No additional package-level validation is documented beyond this option's stated type and its role in the component. |
| language | str | Not set | No | Visible descriptive language label. Constraint: No additional package-level validation is documented beyond this option's stated type and its role in the component. |
| filename | str | Not set | No | Visible filename and default region name. Constraint: No additional package-level validation is documented beyond this option's stated type and its role in the component. |
| id | str | Not set | No | Stable id-safe identifier for repeated filenames. Constraint: Use a stable id-safe token of letters, digits, hyphens, or underscores; it must be unique on the page. |
| label | str | Not set | No | Region name when filename is absent. Constraint: Required if filename is omitted. |
| copyable | bool | False | No | Adds hidden-until-init copy control. Constraint: No additional package-level validation is documented beyond this option's stated type and its role in the component. |
Behaviour
Accessibility and responsive behaviour
- Accessibility
- The overflow region is focusable and named; language is text rather than a colour signal; copy feedback is a polite live status.
- Responsive
- Long lines scroll horizontally at every viewport.
- Without JavaScript
- Source is selectable without JavaScript; the copy button only appears after enhancement.
Need help?
Troubleshooting
Contract check: supply ``label`` instead so the region is never nameless.
When `filename` is omitted, provide `label` so the code region and its copy control are not nameless.
Explore next