I'm working on a project that has a lot of shared elements, e.g. footer, header, navigation, etc. I have few layouts which I extend in my views. For now, I'm loading the shared elements inside my view, but I need to provide all the assets (Dependencies) needed for each shared element inside the view I'm loading it into. I'd like to be able to skip that step and load the element which would be actually prepared for usage right away (so I wouldn't need to remember all the dependent javascript and css files, as some of them might have a few).
I was thinking about specifying all the assets needed for shared element inside the element's view, so when I include the element I need, it would load the assets "automatically", without me specifying all of them inside my view. So my question is if it's possible to do this or what's the right way to accomplish this?
Hope it will be explained even better using code:
Structure:
views/
- /layout/
-> layout.twig
- /homepage/
-> index.twig
- /shared/
-> navigation.twig
Layout:
<!-- HTML headers, etc. -->
{% block assets %}
<link rel="stylesheet" href="xxx" />
{% endblock %}
{% block content %}
{% endblock %}
View:
{% extends "layout/layout.twig" %}
{% block assets %}
{{ parent() }}
<!-- some assets for view -->
{% endblock %}
{% block content %}
{% include "shared/navigation.twig" %}
<!-- content -->
{% endblock %}
Shared element navigation.twig:
// It's not working, of course - just for better explanation of what I'm trying to approach
{% block assets %}
{{ parent() }}
<!-- assets needed for shared element -->
{% endblock %}
<!-- rest of shared element -->
I guess, normally, you don't load all the assets in the views as some shared elements may have a lot of them, especially in bigger project. Also, I think it's nice to point out, that I don't really want to render the styles for them inside the <body>, so creating <links> inside the view is not a way to go for me. This is meant to work like a shared element from which you can control where your assets will be loaded and what assets will be loaded without even knowing what assets are required for each shared element. Thank you in advance.