composer require bmatzner/marionette-bundle:^1.8 and update bundles in AppKernel.php as shown.php app/console assets:install web (or --symlink for development).json2, jquery, underscore, and backbone are loaded in your base template:
{# app/Resources/views/base.html.twig #}
{% block javascripts %}
{{ parent() }}
<script src="{{ asset('bundles/bmatznerjson2/js/json2.min.js') }}"></script>
<script src="{{ asset('bundles/bmatznerjquery/js/jquery.min.js') }}"></script>
<script src="{{ asset('bundles/bmatznerunderscore/js/underscore.min.js') }}"></script>
<script src="{{ asset('bundles/bmatznerbackbone/js/backbone.min.js') }}"></script>
<script src="{{ asset('bundles/bmatznermarionette/js/marionette.min.js') }}"></script>
{% endblock %}
Create a simple Marionette app in a Twig template:
{# app/Resources/views/hello_marionette.html.twig #}
{% extends 'base.html.twig' %}
{% block body %}
<div id="app-container"></div>
<script>
$(document).ready(function() {
var App = new Backbone.Marionette.Application();
App.addRegions({
mainRegion: "#app-container"
});
App.on("initialize:after", function() {
App.mainRegion.show(new Backbone.Marionette.ItemView({
template: "#hello-template"
}));
});
App.start();
});
</script>
<script type="text/template" id="hello-template">
<h1>Hello, Marionette!</h1>
</script>
{% endblock %}
bundles/bmatzner{json2,jquery,underscore,backbone,marionette}/js/.asset() with versioned URLs (e.g., {{ asset('bundles/bmatznermarionette/js/marionette.min.js?v=1.8.8') }}) to cache-bust.--symlink in development for live updates:
php app/console assets:install --symlink web
// src/AppBundle/Controller/DefaultController.php
public function indexAction()
{
return $this->render('hello_marionette.html.twig', [
'user' => ['name' => 'John'], // Pass to Twig for JSON serialization
]);
}
{# In Twig #}
<script>
var userData = {{ user|json_encode|raw }};
</script>
Region-Based Layouts: Define regions in Twig and bind them to Marionette regions:
<div id="header-region"></div>
<div id="content-region"></div>
<div id="footer-region"></div>
App.addRegions({
header: "#header-region",
content: "#content-region",
footer: "#footer-region"
});
Composite Views: Use Backbone.Marionette.Layout for complex UIs:
var LayoutView = Backbone.Marionette.Layout.extend({
template: "#layout-template",
regions: {
sidebar: "#sidebar-region",
main: "#main-region"
}
});
FOSRestBundle or NelmioApiDocBundle to expose endpoints, then fetch data in Marionette:
var UserModel = Backbone.Model.extend({
url: "{{ path('api_user_get') }}",
parse: function(response) {
return response.data; // Match Symfony's JSON structure
}
});
App.on("user:login", function(user) {
App.mainRegion.show(new UserDashboardView({ model: user }));
});
// Symfony event listener (e.g., after login)
$eventDispatcher->dispatch('user.login', new UserEvent($user));
stof/doctrine-extensions might pull newer versions).
composer.json:
"require": {
"underscore": "1.4.4",
"backbone": "1.0.0"
}
Uncaught ReferenceError (e.g., Backbone is not defined).type="text/template" for Marionette templates to avoid Twig processing:
<script type="text/template" id="my-template">
{{- raw('Hello {{ name }}!') -}} {# Raw to prevent Twig escaping #}
</script>
console by default. Enable debug mode in Symfony:
# app/config/config_dev.yml
monolog:
handlers:
main:
level: debug
channels: ["!event"]
debugger; in Marionette code to pause execution in browser dev tools.--no-dev flag for production assets:
php app/console assets:install --symlink --no-dev web
data-marionette attribute and conditional scripts).vendor/bmatzner/marionette-bundle/Resources/public/js/ to web/bundles/marionette/js/ and modify.kernel.request) to initialize Marionette:
// src/AppBundle/EventListener/MarionetteListener.php
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->isMasterRequest()) {
$this->initializeMarionetteApp();
}
}
# app/config/config.yml
nelmio_cors:
defaults:
allow_origin: ["*"]
allow_methods: ["GET", "POST", "PUT", "DELETE"]
allow_headers: ["Content-Type", "Authorization"]
expose_headers: ["Link"]
App.mainRegion.currentView.close(); // Clean up
App.mainRegion.empty();
panther or laravel-dusk for Marionette integration tests.How can I help you explore Laravel packages today?