This service allows for registering stylesheets and javascripts in a dependency aware fasion to be used in the current template.
In order to register a stylesheet or javascript in a controller
// register stylesheet
$this->get('avanzu_admin_theme.theme_manager')
->registerStyle('my-style-id', 'relative/path/to/style.css');
// register javascript
$this->get('avanzu_admin_theme.theme_manager')
->registerScript('my-script-id', 'relative/path/to/script.js');
Linking to stylesheets in your templates
{# stylesheets #}
{% for style in admin_theme.styles %}
<link rel="stylesheet" href="{{ asset(style) }}" />
{% endfor %}
Linking to scripts in your templates
{% for script in admin_theme.scripts %}
<script src="{{ asset(script) }}" ></script>
{% endfor %}
Assets can be registered with an array of ids as dependency requirements.
$manager = $this->get('avanzu_admin_theme.theme_manager');
// no dependencies
$manager->registerScript('my-script-id', 'relative/path/to/script.js');
// define dependency on script with id "my-script-id"
$manager->registerScript('my-other-id', 'relative/path/to/other.js', ['my-script-id']);
The Theme manager will try to resolve the defined dependencies in order to return the asset urls in the right order.
Please note: circular dependencies will cause infinite recursion
Scripts can be assigned to different groups to account for different locations within the template, since some
javascripts need to be loaded in the <head>
section instead of just before the </body>
.
Group names are not following any convention, feel free to name your groups how you like.
$manager = $this->get('avanzu_admin_theme.theme_manager');
// use the "head" group instead of the default one
$manager->registerScript('my-script-id', 'relative/path/to/script.js', [], 'head');
Linking to groups other than the default one inside your templates looks a bit different:
{% for script in admin_theme.getScripts('head') %}
<script src="{{ asset(script) }}" ></script>
{% endfor %}