Embedding in rich text

One mechanism, every editor. Rendering is Craft's own reference-tag parsing, so an embed does not depend on the editor that produced it.

The tag

{legs:price-list:render}

Paste that into a CKEditor field, a Redactor field, a plain HTML field, or a Twig template, and you get the table. It works because craft\htmlfield\HtmlFieldData runs Craft's reference-tag parser over every rich-text value, and Legs tables are elements with a legs reference handle.

Three things follow from that, all of them useful:

  • Content can move between a Redactor field and a CKEditor field with nothing to migrate.
  • Uninstalling an editor plugin does not break a single existing embed.
  • An unresolvable tag is left visible as text rather than vanishing, so a broken handle looks broken instead of looking like an empty page.

You can also refer to a table by ID, which survives a handle change: {legs:14:render}.

Options for one embed

A tag can carry presentation options that apply to that appearance only:

{legs:price-list:render(compact,!striped,perPage=10)}
  • name — turn it on
  • !name — turn it off
  • name=value — set it

Anything you leave out follows the table's own settings, so an override says only what differs here. Names that are not real options are ignored rather than passed on — a typo gets you the table's own behaviour rather than a table that looks right and behaves wrong.

No spaces. Craft's reference-tag pattern ends at the first space, so render(compact, perPage=10) would stop being a reference tag altogether.

The CKEditor button

Open the field's settings and drag the Table button into the toolbar. Clicking it opens the table picker; clicking a table inserts it immediately, and the gear beside each table opens the options pane if this appearance should differ.

What lands in the content is a plain block:

<div class="legs-embed" data-legs-handle="price-list">{legs:price-list:render}</div>

In the editor that block is drawn as a card naming the table, so an author never looks at raw braces. In the stored content it is the tag that matters; the wrapper and its data attributes exist only so the editor can recognise its own work when the content comes back.

Requires craftcms/ckeditor 5.0 or later. Older, DLL-based versions of that plugin are left alone rather than half-supported — embeds still render there, they just have to be typed or pasted.

The Redactor button

Added automatically to every Redactor config that does not already declare a plugins list of its own — if yours does, add legs to it. The button opens the same picker and inserts the same block.

What about HTML Purifier?

Legs registers data-legs-handle and data-legs-options with the purifier used by Craft's HTML fields, so its own attributes survive a save. The reference tag inside is plain text and was never at risk.