Kody for Craft CMS

Free · Craft CMS 5

The code editor field Craft 5 does not have

The last code editor field for Craft, luwes/craft3-codemirror, stopped being updated in August 2022. Since then, code in an entry has gone in a plain textarea, with no colours, no bracket matching and a Tab key that jumps out of the field, or in a rich-text field that mangles it. Kody gives you the field, plus a library for code the whole site reuses and highlighting that needs no JavaScript.

Kody

One line in the template

A Code field gives your templates an object, and render() turns it into highlighted HTML. Each option has a plugin default and a field default, and you can override either one in a single render call.

twig
{% if entry.snippet.isNotEmpty %}
    {{ entry.snippet.render({
        lineNumbers: true,
        highlightLines: '4, 7-9',
        filename: 'templates/news/index.twig',
        copyButton: true,
    }) }}
{% endif %}

{# Entries whose field holds Twig #}
{% set twigEntries = craft.entries().snippet({ language: 'twig' }).all() %}

Features

The field, the library and the highlighter are designed together, because in practice you need all three.

Monaco in the control panel

This is the editor from VS Code, loaded through nystudio107's CodeEditor library. Authors get Twig and Craft API autocomplete, bracket matching, and a Tab key that indents.

  • Per-field language, or let the author pick one
  • The editor grows with its content, up to a limit
  • Syntax is checked when the entry is saved

A snippet library

Some code belongs to the whole site rather than to one entry. A snippet is an element with a handle, so you fix it once and every page that uses it updates.

  • {{ kodySnippet('deploy') }} in templates
  • {kody:deploy:render} inside rich text
  • A Snippets relation field for authors

Highlighted on the server

PHP tokenises the code, caches the result by content and outputs plain HTML and CSS. Visitors download no highlighter, nothing runs in their browser, and code never appears uncoloured first.

  • Works with JavaScript off and in RSS readers
  • 46 of 49 languages coloured on the front end

Copying gives you the exact source

Line numbers are drawn with CSS counters and real newlines stay in the markup. Copying a block, or having a screen reader read it, gives you the source character for character.

Language you can query

The code and its language are stored as separate keys, and Craft indexes each one. You can filter or sort entries by language without a LIKE search through escaped JSON.

Safe to evaluate

When a field holds Twig, .eval() uses Craft's Twig sandbox if it is switched on. If the sandbox is off, it only runs content an admin saved last. Nothing else is evaluated.

Kody in Craft

These are real screens from a Craft 5 install: the field on an entry, the snippet library, and a front-end page where the code was highlighted on the server.

A Kody code field on a Craft entry, showing Monaco with Twig highlighting, line numbers and a language selector set to Twig
The Kody snippets index in the Craft control panel, listing nine snippets with handle, language and line count, and a sidebar grouped by language
A front-end page showing a Twig template highlighted by Kody with a filename caption, line numbers, three highlighted lines and a copy button

Screenshots from a live install, not mockups.

Frequently Asked Questions

The questions worth answering before you install it.

Stop putting code in textareas

Kody is free, with no editions and no licence key. Install it, add a Code field to a layout, and your next code sample gets a proper editor.

More in Development

Pairs well with Kody

For the developer on the project. Migrations in and between sites, a code editor field, schema docs, content inventories, reports and email testing.