composer require eltharin/twigfilesgetter
This bundle will help you to include css and js files once in your template.
Per example, when you make a Custom FormType who need a script, you can put in the buildView function the lines :
use Eltharin\TwigFilesGetterBundle\Service\FileManager;
...
public function buildView(FormView $view, FormInterface $form, array $options)
{
FileManager::registerJsFile('/bundles/eltharinreloadablefield/js/reloader.js');
...
}
or in a twig template :
{% do addJs('/bundles/eltharinreloadablefield/js/reloader.js') %}
or
{% do addCss('fichier.css') %}
or with a group :
{% do addJs('fichier.js', 'group Name') %}
and in your template :
{{ get_required_js_files() }}
will write
<script src="/bundles/eltharinreloadablefield/js/scripts.js"/></script>
if you want have js in head and after the page you can specify a 'group'
public function buildView(FormView $view, FormInterface $form, array $options)
{
FileManager::registerJsFile('/bundles/eltharinreloadablefield/js/file_in_head.js', 'head');
FileManager::registerJsFile('/bundles/eltharinreloadablefield/js/file_bottom.js', 'bottom');
...
}
and in your template :
in head :
{{ get_required_js_files('head') }}
and
{{ get_required_js_files('bottom') }}
How can I help you explore Laravel packages today?