fousky / jsblock
Collect & render javascripts at the end of your template.
Installs: 580
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=7.2
- twig/twig: ^2.12|^3.0
README
The Symfony JSBlockBundle gives you a chance to collect javascript across all TWIG templates (e.g. forms) and render them at the end of your layout (or anywhere you want them).
1. Install it by composer
composer require fousky/jsblock
2. Register bundle in Kernel
public function registerBundles() { $bundles = array( new Fousky\JSBlockBundle\FouskyJSBlockBundle(), // ... your custom bundles ); // ... your custom dev bundles return $bundles; }
3. Render collected javascripts to your main template
I assume you have the main template layout.html.twig
, so add {% jsblock 'render' %}
before closing </body>
tag.
Of course you can put this tag anywhere you want to render collected javascripts.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>my page</title> </head> <body> <!-- APPLICATION LOGIC --> <!-- render collected javascripts --> {% jsblock 'render' %} </body> </html>
4. Collect javascripts anywhere
Now you can collect javascripts anywhere you want.
You have to start collecting by tag: {% jsblock 'start' %}
and close it by tag {% jsblock 'stop' %}
.
<!-- CUSTOM LOGIC --> <!-- need some javascript? Start rendering --> {% jsblock 'start' %} <script> $(document).ready(function() { console.log('Hello from collected JSBlockBundle!'); }); </script> {% jsblock 'stop' %} <!-- and close collecting -->