---
title: code
description: "Format inline code or a fenced code block."
---

# code

Format inline code or a fenced code block.

## Syntax

```knap
{{ value | code }}
{{ value | code:"typescript" }}
```

## Usage

- An optional language creates a fenced code block and adds the language after the opening fence.
- Multiline data automatically becomes a fenced block. Backtick delimiters grow when needed to contain backticks safely.
- For arrays and objects, including serialized collections from another filter, string values are formatted recursively while keys and non-string values are preserved.

## Examples

### Inline

```json title="Data"
{
  "source": "const answer = 42"
}
```

```knap title="Template"
{{ source | code }}
```

```md title="Output"
`const answer = 42`
```

### Language block

```json title="Data"
{
  "source": "const answer = 42"
}
```

```knap title="Template"
{{ source | code:"typescript" }}
```

````md title="Output"
```typescript
const answer = 42
```
````

## Related filters

- [code_block](/filters/code-block)
