Currently Available: Need a skilled Software Developer for your next project?
Categories
Laravel

Rendering Bard Content to HTML in Statamic

When working with Statamic's Bard fieldtype programmatically, you'll often need to convert its structured data to HTML. The documentation doesn't make this obvious, but here's how to do it properly.

The Problem

Bard stores content as a structured array based on ProseMirror's document format. When you access it via $entry->get('content'), you get the raw array structure. Using $entry->augmentedValue('content') gives you a Value object, but calling ->value() or casting to string often returns plain text without HTML tags or links.

The Solution

Use Statamic's CoreModifiers class with the bardHtml method:

use Statamic\Modifiers\CoreModifiers;

$bardValue = $entry->augmentedValue('article_builder');
$modifiers = new CoreModifiers();
$html = $modifiers->bardHtml($bardValue);

This properly renders:

  • Paragraph tags
  • Headings
  • Links (including internal Statamic links like statamic://entry::abc-123)
  • Lists
  • All other Bard formatting

Full Example

Here's a complete example for rendering Bard content from an entry:

use Statamic\Facades\Entry;
use Statamic\Modifiers\CoreModifiers;

$entry = Entry::find($entryId);

if ($entry->has('content')) {
    $augmented = $entry->augmentedValue('content');

    if ($augmented) {
        $modifiers = new CoreModifiers();
        $html = $modifiers->bardHtml($augmented);

        // Now $html contains properly formatted HTML
        echo $html;
    }
}

Alternative Methods

If you need more control over the rendering process, you can use the Bard Augmentor directly:

use Statamic\Fieldtypes\Bard\Augmentor;

$fieldtype = $entry->blueprint()->field('content')->fieldtype();
$augmentor = (new Augmentor($fieldtype))->withStatamicImageUrls();

$content = $entry->get('content');
$html = $augmentor->augment($content);

But for most use cases, CoreModifiers::bardHtml() is simpler and more reliable.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Subscribe to my newsletter

Get new posts when I publish them.

I respect your privacy. Unsubscribe at any time.

Leave a Reply

Your email address will not be published. Required fields are marked *