I am working on a project that has many common elements, for example. footer, header, navigation, etc. I have few layouts that I use in my presentations. So far, I'm loading common elements inside my view, but I need to provide all the assets (dependencies) needed for each common element inside the view into which I load it. I would like to skip this step and load an element that would be actually prepared for use immediately (so I would not need to remember all the dependent javascript and css files, as some of them may have several),
I was thinking about specifying all the assets needed for the common item inside the item view, so when I include the item I need, it will load the assets “automatically”, without any definitions of all of them inside my view. So my question is, can this be done or how to do it correctly?
Hope this will be explained even better using the code:
Structure:
views/ - /layout/ -> layout.twig - /homepage/ -> index.twig - /shared/ -> navigation.twig
Markup:
{% block assets %} <link rel="stylesheet" href="xxx" /> {% endblock %} {% block content %} {% endblock %}
View:
{% extends "layout/layout.twig" %} {% block assets %} {{ parent() }} {% endblock %} {% block content %} {% include "shared/navigation.twig" %} {% endblock %}
General element navigation.twig:
// It not working, of course - just for better explanation of what I'm trying to approach {% block assets %} {{ parent() }} {% endblock %}
I assume that, as a rule, you do not load all the assets in the views, as some common elements may have many of them, especially in a larger project. In addition, I am pleased to note that I really do not want to display styles for them inside the <body> , so creating <links> inside the view is not a way to go for me. This is intended to work as a common element from which you can control where your assets will be loaded and which assets will be loaded, without even knowing which assets are needed for each common element. Thank you in advance.