ContentBox.js · Guide

Using ContentBox.js in a Laravel Project

ContentBox.js is a JavaScript library for designing and building complete web pages with simple drag and drop. In this guide we add it to a Laravel app, using Blade templates and Vite. First we embed the editor, then we add saving, uploads and a page that shows the saved page to visitors.

ContentBox comes in two parts:

ContentBox

The editor, used on the page where pages are built.

ContentBox Runtime

Renders the saved page 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 project
  2. Embed the editor
  3. Add saving and uploads
  4. Show the page to visitors
  5. Try it

Step 1

Set up the project

Create a Laravel project and install ContentBox and the Runtime:

composer create-project laravel/laravel my-app
cd my-app
npm install
npm install @innovastudio/contentbox @innovastudio/contentbox-runtime

The editor needs its assets (sections, snippets, icons, fonts, plugins). They are in the ContentBox.js ZIP package, which you can download from your user area. Copy these into your project's public/ folder:

public/
├── assets/      # sections, snippets, fonts, plugins
├── runtime/     # runtime files, used by blank.html and preview.html
├── blank.html   # the page you edit, shown inside the editor
└── preview.html # Page Preview

Step 2

Embed the editor

Create resources/js/editor.js:

import ContentBox from '@innovastudio/contentbox'
import '@innovastudio/contentbuilder/public/contentbuilder/contentbuilder.css'
import '@innovastudio/contentbox/public/contentbox/contentbox.css'

// Create the editor
const builder = new ContentBox({
    controlPanel: true,
    iframeSrc: '/blank.html', // the page you edit
    snippetUrl: '/assets/minimalist-blocks/content-plugins.js', // the blocks users can drag in
})

// Load the page to edit
builder.loadHtml(`
    <div class="is-section is-box type-system-ui">
        <div class="is-overlay"></div>
        <div class="is-container v2 leading-14 size-18">
            <div class="row">
                <div class="column">
                    <h1>Hello World</h1>
                    <p>Start editing your page here.</p>
                </div>
            </div>
        </div>
    </div>`)

Keep the two stylesheets in this order: contentbuilder.css first, then contentbox.css.

The page you edit is shown inside the editor from blank.html. It already starts the runtime, so interactive blocks work while you edit.

Create the page, resources/views/edit.blade.php. ContentBox adds its own interface, so the body stays empty:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Edit</title>
    @vite('resources/js/editor.js')
</head>
<body>
</body>
</html>

Add the route in routes/web.php:

Route::view('/edit', 'edit');

Tell Vite about the new script. In vite.config.js, add resources/js/editor.js to input:

input: ['resources/css/app.css', 'resources/js/app.js', 'resources/js/editor.js'],

Run it:

composer run dev

Open http://localhost:8000/edit. You have a working editor: click the text to edit it, add sections with the + button on the left, and drag in blocks from the Blocks button below it, including interactive ones like a countdown or a chart.

The ContentBox.js editor in a Laravel app after step 2, with Hello World

The editor after step 2. Click the text to edit it, add sections with + and blocks from the Blocks panel.

Step 3

Add saving and uploads

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

A page has three parts: the content (html), the styles used on the page (mainCss) and the styles of each section (sectionCss). The routes save and load all three.

At the top of routes/web.php, add:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

Then replace the /edit route with these three routes:

// The editor, with the saved page
Route::get('/edit', function () {
    return view('edit', ['page' => json_decode(Storage::get('page.json') ?? '{}', true)]);
});

// Saves the page
Route::post('/save', function (Request $request) {
    Storage::put('page.json', json_encode($request->only('html', 'mainCss', 'sectionCss')));

    return ['ok' => true];
});

// Saves an uploaded file and returns its URL
Route::post('/upload', function (Request $request) {
    $request->validate([
        'file' => 'required|file|mimes:jpg,jpeg,png,gif,webp,svg,mp4,webm,mp3,wav,pdf',
    ]);
    $path = $request->file('file')->store('uploads', 'public');

    return ['url' => '/storage/' . $path]; // ContentBox needs { url }
});

Uploaded files are saved in storage/app/public/uploads. To make them reachable from the browser, run once:

php artisan storage:link

Replace resources/views/edit.blade.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Edit</title>
    @vite('resources/js/editor.js')
</head>
<body>
    <div style="position: fixed; top: 0; left: 0; right: 0; height: 55px; z-index: 10;
                display: flex; align-items: center; justify-content: flex-end; gap: 8px;
                padding: 0 20px; background: #fff; border-bottom: 1px solid #eee">
        <button id="btnPreview">Preview</button>
        <button id="btnView">View page</button>
        <button id="btnSave">Save</button>
    </div>

    <script>
        // The saved page, for editor.js
        window.savedPage = {{ Js::from($page) }};
    </script>
</body>
</html>

Replace resources/js/editor.js:

import ContentBox from '@innovastudio/contentbox'
import '@innovastudio/contentbuilder/public/contentbuilder/contentbuilder.css'
import '@innovastudio/contentbox/public/contentbox/contentbox.css'

const DEFAULT_CONTENT = `
<div class="is-section is-box type-system-ui">
    <div class="is-overlay"></div>
    <div class="is-container v2 leading-14 size-18">
        <div class="row">
            <div class="column">
                <h1>Hello World</h1>
                <p>Start editing your page here.</p>
            </div>
        </div>
    </div>
</div>`

// Laravel's CSRF token, sent with every request
const csrfToken = document.querySelector('meta[name="csrf-token"]').content

// Sends the file to the upload route, which returns { url }
async function upload(file) {
    const formData = new FormData()
    formData.append('file', file)
    const response = await fetch('/upload', {
        method: 'POST',
        headers: { 'X-CSRF-TOKEN': csrfToken },
        body: formData,
    })
    return response.json()
}

// Create the editor
const builder = new ContentBox({
    controlPanel: true,
    iframeSrc: '/blank.html', // the page you edit
    snippetUrl: '/assets/minimalist-blocks/content-plugins.js', // the blocks users can drag in
    topSpace: true,           // leaves room at the top for our toolbar
    upload,                   // handles image, video, audio and file uploads
})

// Load the saved page, or the default the first time
const page = window.savedPage
builder.loadHtml(page.html || DEFAULT_CONTENT)
builder.loadStyles(page.mainCss || '', page.sectionCss || '')

// Sends the page to the save route
async function save() {
    await fetch('/save', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': csrfToken },
        body: JSON.stringify({
            html: builder.html(),
            mainCss: builder.mainCss(),       // styles used on the page
            sectionCss: builder.sectionCss(), // styles of each section
        }),
    })
}

// Toolbar
document.querySelector('#btnPreview').addEventListener('click', () => builder.openPagePreview())
document.querySelector('#btnView').addEventListener('click', async () => {
    await save()
    window.open('/view', '_blank')
})
document.querySelector('#btnSave').addEventListener('click', async () => {
    await save()
    alert('Page saved!')
})

What's new

  • Saving: html(), mainCss() and sectionCss() return the three parts of the page, which are sent to the save route. The next time, the /edit route passes the saved page to the script, and loadHtml() and loadStyles() load it into the editor.
  • Uploads: upload is one function for all media. It sends the file to the upload route, which returns { url }.
  • CSRF: Laravel protects POST routes with a token. The page puts it in a meta tag, and the script sends it with each request.
  • Toolbar: topSpace leaves room at the top for it. Preview shows the page at any screen width (using public/preview.html), Save saves, and View page saves and opens the viewing page, which we add next.
The ContentBox.js editor in a Laravel app, with a saved page loaded back in

A saved page, with its styles, loads back into the editor the next time you open it.

Step 4

Show the page to visitors

The viewing page is rendered on the server, so the page 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.

Add the route in routes/web.php:

Route::get('/view', function () {
    return view('view', ['page' => json_decode(Storage::get('page.json') ?? '{}', true)]);
});

Create resources/views/view.blade.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>View</title>
    @vite('resources/js/view.js')

    {{-- The saved styles: used on the page, and of each section --}}
    {!! $page['mainCss'] ?? '' !!}
    {!! $page['sectionCss'] ?? '' !!}
</head>
<body>
    <div class="is-wrapper">
        {!! $page['html'] ?? '<p>No content available.</p>' !!}
    </div>
</body>
</html>

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

Create resources/js/view.js. It makes the interactive blocks in the page work:

import ContentBoxRuntime from '@innovastudio/contentbox-runtime'
import '@innovastudio/contentbox-runtime/dist/contentbox-runtime.css'

const runtime = new ContentBoxRuntime({
    pluginFolder: window.location.origin + '/assets/plugins/',
})
runtime.init()

pluginFolder tells the runtime where the plugins are. A plugin's files are loaded only when the page uses it.

And add it to input in vite.config.js:

input: ['resources/css/app.css', 'resources/js/app.js', 'resources/js/editor.js', 'resources/js/view.js'],

Step 5

Try it

With composer run dev running, open http://localhost:8000/edit, add some sections and blocks, and click Save. Click View page to see the saved page.

The Laravel viewing page showing a page saved with ContentBox.js

The viewing page, rendered on the server by Blade, shows the saved page to visitors.

Going to production

  • Build: run npm run build, then serve the app as usual.
  • Saving: replace the file in the /edit, /save and /view routes with your database or CMS. Save all three parts: the content, mainCss and sectionCss.
  • Uploads: to use S3 or other storage, change the public disk in store() to your disk, and return the URL it gives.
  • Security: put the /edit, /save and /upload routes behind authentication (for example the auth middleware), so only your editors can edit, save and upload.

Resources

© 2026 InnovaStudio

This site is built with Site Builder Kit