Skip to content

Commit c1fe2e2

Browse files
committed
minor #16403 Frontend Encore document passing data via serialize twig filter (94noni)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- Frontend Encore document passing data via serialize twig filter I think this feature added in v5.3 is a great addition, specialy in this doc article ref https://symfony.com/blog/new-in-symfony-5-3-twig-serialize-filter + symfony/symfony#40323 Cc `@jrushlow` `@weaverryan` Commits ------- 2a1314e Frontend Encore document passing data via serialize twig filter
2 parents 89b57b9 + 2a1314e commit c1fe2e2

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

frontend/encore/server-data.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ them later in JavaScript. For example:
88

99
.. code-block:: html+twig
1010

11-
<div class="js-user-rating" data-is-authenticated="{{ app.user ? 'true' : 'false' }}">
11+
<div class="js-user-rating"
12+
data-is-authenticated="{{ app.user ? 'true' : 'false' }}"
13+
data-user="{{ app.user|serialize(format = 'json') }}"
14+
>
1215
<!-- ... -->
1316
</div>
1417

@@ -19,6 +22,7 @@ Fetch this in JavaScript:
1922
document.addEventListener('DOMContentLoaded', function() {
2023
var userRating = document.querySelector('.js-user-rating');
2124
var isAuthenticated = userRating.dataset.isAuthenticated;
25+
var user = JSON.parse(userRating.dataset.user);
2226
2327
// or with jQuery
2428
//var isAuthenticated = $('.js-user-rating').data('isAuthenticated');

0 commit comments

Comments
 (0)