contao-components/ace
Contao integration for the Ace code editor. Provides backend-ready assets and configuration to embed a powerful in-browser editor with syntax highlighting, themes, and advanced editing features in Contao projects.
Installation Add the package via Composer:
composer require contao-components/ace
Publish the assets (if needed):
php artisan vendor:publish --tag=ace-assets
Basic Usage Include the editor in a Blade view:
{!! Ace::editor('my_editor', $content, ['theme' => 'monokai', 'mode' => 'php'])->render() !!}
$content: Initial editor content (optional).theme: Syntax theme (e.g., monokai, github).mode: Language mode (e.g., php, javascript, html).First Use Case Integrate with a Laravel form for rich-text editing:
// Controller
public function edit(Post $post) {
return view('posts.edit', ['content' => $post->content]);
}
<!-- posts/edit.blade.php -->
<form method="POST" action="/posts">
@csrf
{!! Ace::editor('content', old('content', $content), ['mode' => 'markdown'])->render() !!}
<button type="submit">Save</button>
</form>
Dynamic Editor Initialization Use JavaScript to initialize editors conditionally:
$(document).ready(function() {
if ($('#dynamic-editor').length) {
Ace.edit('dynamic-editor').setTheme('chrome');
}
});
Integration with Laravel Forms
Extend Illuminate\Support\Facades\Form for Ace-compatible fields:
// app/Providers/AppServiceProvider.php
use ContaoComponents\Ace\Facades\Ace;
public function boot() {
Form::macro('ace', function ($name, $value = null, $options = []) {
return Ace::editor($name, $value, $options)->render();
});
}
Usage:
{!! Form::ace('description', $post->description, ['mode' => 'html']) !!}
Saving Editor Content Handle form submissions with AJAX for seamless updates:
$('#editor-form').submit(function(e) {
e.preventDefault();
const content = Ace.edit('my_editor').getValue();
$.post('/posts', { content, _token: '{{ csrf_token() }}' }, function() {
alert('Saved!');
});
});
Multi-Editor Pages Manage multiple editors with unique IDs:
@foreach($post->sections as $section)
{!! Ace::editor("section_{$section->id}", $section->content, ['mode' => 'javascript'])->render() !!}
@endforeach
Custom Themes/Plugins Load external ACE themes/plugins via CDN or local assets:
{!! Ace::editor('code', $content, [
'lib' => ['theme' => '//cdn.example.com/ace/themes/custom'],
'plugins' => ['beautify']
])->render() !!}
Real-Time Collaboration
Use ACE’s ace.require('ace/ext/beautify') with Laravel Echo/Pusher for live editing.
Server-Side Processing Sanitize/process editor content before saving:
$cleanContent = purify($request->input('content')); // Use HTML Purifier or similar
Asset Loading Conflicts
@stack('scripts')
@push('scripts')
{!! Ace::assets()->render() !!}
@endpush
CSRF Token Mismatch
_token in AJAX calls (as shown in workflows).Editor Not Initializing
<textarea>.Ace is not defined. Ensure:
php artisan vendor:publish).Performance with Large Files
setValue() after DOM ready.console.log(Ace.edit('editor_id').getSession().getDocument().getValue());
console.log(Ace.version);
plugins: [] to isolate issues.Custom Commands Extend ACE’s behavior via JavaScript:
Ace.edit('editor').commands.addCommand({
name: 'save',
bindKey: {win: 'Ctrl-S', mac: 'Command-S'},
exec: function() { /* Custom logic */ }
});
Server-Side Validation Validate editor content in Laravel:
$validator = Validator::make($request->all(), [
'content' => 'required|max:10000|mimes:text/plain,text/html',
]);
Theme/Mode Overrides Dynamically set themes/modes via Laravel config:
config(['ace.default_theme' => env('ACE_THEME', 'monokai')]);
Then use:
{!! Ace::editor('code')->render() !!} <!-- Uses config default -->
Laravel Mix Integration Process ACE assets with Mix for caching:
// resources/js/app.js
require('ace-builds/src-min-noconflict/ace');
require('ace-builds/src-min-noconflict/theme-monokai');
How can I help you explore Laravel packages today?