eduardoledo/symfony-jquery-load-fragment-renderer
Installation:
composer require eduardoledo/symfony-jquery-load-fragment-renderer
Add the bundle to config/bundles.php (Symfony) or register the service provider in config/app.php (Laravel via Symfony bridge).
Basic Usage:
hinclude with:
{{ render_jquery_load_fragment('path/to/partial', ['param1' => 'value1']) }}
First Use Case: Dynamically load a partial (e.g., a comment section) without full page reload:
<div id="comments-section">
{{ render_jquery_load_fragment('partials.comments', ['post_id' => $post->id]) }}
</div>
Partial Rendering:
{{ render_jquery_load_fragment('partials.user-sidebar', ['user_id' => auth()->id()]) }}
Integration with Laravel:
render_jquery_load_fragment directive for custom logic:
// In AppServiceProvider@boot()
Blade::directive('dynamicLoad', function ($expression) {
return "<?php echo render_jquery_load_fragment($expression); ?>";
});
Usage: @dynamicLoad('partials.'.$dynamicPartial)AJAX Call Customization:
$.load() behavior by extending the package’s JavaScript:
// public/js/custom-load.js
$.fn.loadWithOptions = function(url, options) {
// Custom logic (e.g., add CSRF token)
return this.load(url, $.extend(options, { beforeSend: function() {
$(this).addClass('loading');
}}));
};
loadFragment function to use your custom method.Route-Based Loading:
Route::get('/partials/comments/{id}', [CommentController::class, 'renderPartial']);
Template:
{{ render_jquery_load_fragment(route('partials.comments', $post->id)) }}
// mix.js
require('eduardoledo/symfony-jquery-load-fragment-renderer/dist/jquery-load-fragment-renderer');
{{ render_jquery_load_fragment('partials.'.$partial, [], ['cacheBust' => true]) }}
error callback to show user-friendly messages:
$.loadFragment('url', {
error: function() {
$('#fragment-container').html('<p class="text-danger">Failed to load content.</p>');
}
});
CSRF Protection:
$.load() may fail due to missing CSRF tokens for POST requests.<meta name="csrf-token" content="{{ csrf_token() }}">
JS:
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }
});
Route Caching:
php artisan route:clear and regenerate cached routes:
php artisan route:cache
Partial Not Found:
AppServiceProvider:
public function boot()
{
app()->error(function (\Throwable $e) {
if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
abort_if(request()->ajax(), 500, 'Partial not found.');
}
});
}
JavaScript Conflicts:
jQuery.noConflict().$(document).ajaxError(function(e, xhr, settings) {
console.error('AJAX Error:', settings.url, xhr.status, xhr.responseText);
});
{{ render_jquery_load_fragment('partials.'.$partial, [], ['noCache' => true]) }}
Custom Renderer:
$.fn.loadFragment:
$.fn.loadFragment = function(url, options) {
return this.load(url, $.extend({
data: { _token: '{{ csrf_token() }}' }
}, options));
};
Server-Side Logic:
PartialRenderer (if available) to pre-process data:
// app/Providers/AppServiceProvider.php
public function boot()
{
$this->app->extend('partial.renderer', function ($renderer) {
$renderer->addModifier('trim', function ($content) {
return trim($content);
});
return $renderer;
});
}
Usage: {{ render_jquery_load_fragment('partials.'.$partial, [], ['modifiers' => ['trim']]) }}Event Listeners:
$(document).on('fragmentLoaded', '#comments-section', function() {
// Initialize tooltips, etc.
$(this).find('[data-toggle="tooltip"]').tooltip();
});
loadFragment success callback:
{{ render_jquery_load_fragment('partials.'.$partial, [], [
'onSuccess' => 'function() { $(this).trigger("fragmentLoaded"); }'
]) }}
Fallback Content:
<div id="fallback-comments">
Loading comments...
</div>
{{ render_jquery_load_fragment('partials.comments', [], [
'fallback' => '#fallback-comments'
]) }}
How can I help you explore Laravel packages today?