ContentBuilder.js · Guide

Using ContentBuilder.js in a PHP Project

ContentBuilder.js is a JavaScript library for creating elegant, editorial-style web content with simple drag and drop. In this guide we add it to a plain PHP site: no framework and no Composer. First we embed the editor, then we add saving, uploads and a page that shows the saved content to visitors.

ContentBuilder comes in two parts:

ContentBuilder

The editor, used on the page where content is created.

ContentBuilder Runtime

Renders the saved content on your site. It makes interactive blocks (sliders, counters, charts, accordions and more) work, both while editing and when viewing.

In this guide

  1. Set up the folder
  2. Embed the editor and the runtime
  3. Add saving and uploads
  4. Show the content to visitors
  5. Try it

Step 1

Set up the folder

Create a folder for your site. The files you need are in the ContentBuilder.js ZIP package, which you can download from your user area. From the package's public/ folder, copy these into your folder:

my-site/
├── contentbuilder/  # the editor
├── runtime/         # the runtime
├── assets/          # snippets, fonts, plugins
└── preview.html     # Live Preview page

Start PHP's built-in server in the folder:

cd my-site
php -S localhost:8000

(Or put the folder on any PHP hosting, or in XAMPP or MAMP.)

Step 2

Embed the editor and the runtime

Create edit.php. For now it is plain HTML; we add PHP in the next step:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Edit</title>
    <link href="contentbuilder/contentbuilder.css" rel="stylesheet">
    <link href="runtime/contentbuilder-runtime.css" rel="stylesheet">
</head>
<body>
    <div class="container" style="max-width: 840px; margin: 140px auto; padding: 0 20px"></div>

    <script src="contentbuilder/contentbuilder.min.js"></script>
    <script src="runtime/contentbuilder-runtime.min.js"></script>
    <script>
        // The folder this page is in, so the files are found with or without a server
        const basePath = location.href.substring(0, location.href.lastIndexOf('/'));

        // Create the editor on the .container element
        const builder = new ContentBuilder({
            container: '.container',
        });

        // Load the blocks users can drag in
        builder.loadSnippets('assets/minimalist-blocks/content-plugins.js');

        // Load the content to edit
        builder.loadHtml(`
            <div class="row">
                <div class="column">
                    <h1>Hello World</h1>
                    <p>Start editing your content here.</p>
                </div>
            </div>`);

        // Start the runtime, so interactive blocks work while editing
        const runtime = new ContentBuilderRuntime({
            pluginFolder: basePath + '/assets/plugins/',
        });
        runtime.init();

        // Make the runtime available to the editor
        window.builderRuntime = runtime;
    </script>
</body>
</html>

The runtime makes interactive blocks (sliders, counters, charts, accordions and more) work while you edit. pluginFolder tells it where the plugins are. A plugin's files are loaded only when the content uses it.

Open http://localhost:8000/edit.php. You have a working editor: click the text to edit it, and drag blocks in from the panel on the right, including interactive ones like a countdown or a chart.

The ContentBuilder.js editor in a PHP site after step 2, with Hello World

The editor after step 2. Click the text to edit it, and drag blocks in from the panel.

Step 3

Add saving and uploads

Now let's add saving and uploads. The content is saved to a file, so you can run it without a database.

Create save.php. It saves the content to data/content.html:

<?php
// Saves the content to data/content.html
$data = json_decode(file_get_contents('php://input'), true);
if (!isset($data['html'])) {
    http_response_code(400);
    exit;
}

if (!is_dir(__DIR__ . '/data')) mkdir(__DIR__ . '/data');
file_put_contents(__DIR__ . '/data/content.html', $data['html']);

header('Content-Type: application/json');
echo json_encode(['ok' => true]);

Create upload.php. It saves an uploaded file in uploads/ and returns its URL:

<?php
// Saves an uploaded file in uploads/ and returns its URL
$allowed = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'mp4', 'webm', 'mp3', 'wav', 'pdf'];

$file = $_FILES['file'] ?? null;
$ext = strtolower(pathinfo($file['name'] ?? '', PATHINFO_EXTENSION));
if (!$file || $file['error'] !== UPLOAD_ERR_OK || !in_array($ext, $allowed)) {
    http_response_code(400);
    exit;
}

// A new random name, so uploads never overwrite each other
$name = bin2hex(random_bytes(8)) . '.' . $ext;
if (!is_dir(__DIR__ . '/uploads')) mkdir(__DIR__ . '/uploads');
move_uploaded_file($file['tmp_name'], __DIR__ . '/uploads/' . $name);

header('Content-Type: application/json');
echo json_encode(['url' => 'uploads/' . $name]); // ContentBuilder needs { url }

Replace edit.php:

<?php
// The saved content, or the default the first time
$file = __DIR__ . '/data/content.html';
$content = file_exists($file) ? file_get_contents($file) : '
<div class="row">
    <div class="column">
        <h1>Hello World</h1>
        <p>Start editing your content here.</p>
    </div>
</div>';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Edit</title>
    <link href="contentbuilder/contentbuilder.css" rel="stylesheet">
    <link href="runtime/contentbuilder-runtime.css" rel="stylesheet">
</head>
<body>
    <div class="container" style="max-width: 840px; margin: 140px auto; padding: 0 20px"></div>

    <div style="position: fixed; top: 20px; right: 20px; display: flex; gap: 8px">
        <button id="btnHtml">HTML</button>
        <button id="btnView">Preview</button>
        <button id="btnSave">Save</button>
    </div>

    <script src="contentbuilder/contentbuilder.min.js"></script>
    <script src="runtime/contentbuilder-runtime.min.js"></script>
    <script>
        const basePath = location.href.substring(0, location.href.lastIndexOf('/'));

        // Sends the file to upload.php, which returns { url }
        async function upload(file) {
            const formData = new FormData();
            formData.append('file', file);
            const response = await fetch('upload.php', { method: 'POST', body: formData });
            return response.json();
        }

        // Create the editor
        const builder = new ContentBuilder({
            container: '.container',
            previewURL: 'preview.html', // enables the Live Preview button
            upload,                     // handles image, video, audio and file uploads
        });

        // Load the blocks users can drag in
        builder.loadSnippets('assets/minimalist-blocks/content-plugins.js');

        // Load the saved content
        builder.loadHtml(<?= json_encode($content, JSON_HEX_TAG | JSON_HEX_AMP) ?>);

        // Start the runtime, so interactive blocks work while editing
        const runtime = new ContentBuilderRuntime({
            pluginFolder: basePath + '/assets/plugins/',
        });
        runtime.init();
        window.builderRuntime = runtime;

        // Sends the content to save.php
        async function save() {
            await fetch('save.php', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ html: builder.html() }),
            });
        }

        document.querySelector('#btnHtml').addEventListener('click', () => builder.viewHtml());
        document.querySelector('#btnView').addEventListener('click', async () => {
            await save();
            window.open('view.php', '_blank');
        });
        document.querySelector('#btnSave').addEventListener('click', async () => {
            await save();
            alert('Content saved!');
        });
    </script>
</body>
</html>

What's new

  • Saving: html() returns the edited content, which is sent to save.php. The next time, edit.php reads the saved file and loadHtml() loads it into the editor.
  • Uploads: upload is one function for all media. It sends the file to upload.php, which returns { url }. Only the file types in $allowed are accepted, and each file gets a new random name.
  • Buttons: HTML opens the code view, Save saves, and Preview saves and opens the viewing page, which we add next.
  • Live Preview: previewURL adds a preview button to the editor, which uses preview.html.
The ContentBuilder.js editor in a PHP site, with saved content loaded back in

Saved content loads back into the editor the next time you open it.

PHP creates the data/ and uploads/ folders the first time it saves. On a server, PHP needs permission to write to your site folder.

Step 4

Show the content to visitors

The viewing page is rendered on the server, so the content is already in the HTML that browsers and search engines receive. This is good for SEO. The page needs only the runtime, not the editor.

Create view.php:

<?php
$file = __DIR__ . '/data/content.html';
$content = file_exists($file) ? file_get_contents($file) : '<p>No content available.</p>';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>View</title>
    <link href="runtime/contentbuilder-runtime.css" rel="stylesheet">
</head>
<body>
    <!-- is-container is required for content styling -->
    <div class="is-container" style="max-width: 840px; margin: 80px auto; padding: 0 20px">
        <?= $content ?>
    </div>

    <script src="runtime/contentbuilder-runtime.min.js"></script>
    <script>
        const basePath = location.href.substring(0, location.href.lastIndexOf('/'));

        // Activate interactive blocks
        const runtime = new ContentBuilderRuntime({
            pluginFolder: basePath + '/assets/plugins/',
        });
        runtime.init();
    </script>
</body>
</html>

Important: the viewing container must have the is-container class.

Step 5

Try it

Open http://localhost:8000/edit.php, drag in some blocks and click Save. Click Preview to see the saved content on the viewing page.

The viewing page showing content saved with ContentBuilder.js

The viewing page shows the saved content to visitors.

Going to production

  • Saving: replace the file in edit.php, save.php and view.php with your database.
  • Uploads: to use S3 or other storage, change upload.php to store the file there and return its URL.
  • Security: let only your editors open edit.php, save.php and upload.php, for example with your existing login check at the top of each file. view.php is the public page.

Resources

© 2026 InnovaStudio

This site is built with Site Builder Kit