G
G
gog692016-08-08 12:50:08
symfony
gog69, 2016-08-08 12:50:08

Sub request or service?

the layout consists of 4 parts: header, content, sidebar, footer
data for the middle (content, sidebar) I plan to return the controller

public function indexAction() 
{
   ...
   return $this->render($tpl, [
     "content" => $this->get('service')->getDataForCurrentPage(),
     "sidebarContent" => $this->get('service')->getSidebar3()
  ])
}

for dynamic header and footer data, I see the following options:
1. through Sub-request {{ render(controller( 'controller:headerAction' )) }}
2. through service and twig extension
<div class="wrapper">
    {% for item in getDataForHeader() %}
         <div class=" ..  ." >
              {{  item.render() }}
         </div>
    {% endfor %}
</div>

3. get data for everything with one call
{% set layoutData = getLayoutData() %}
.....
{% for item in layoutData.header %}
...
{% endfor %}
.......
.....
{% for item in layoutData.footer %}
   some rendering
{% endfor %}
{ endfor %}

4. just like 3, only when loading the bundle - drive the data directly into the global twig variable instead of calling {% set layoutData = getLayoutData() %}
5. just like 4, only mark several services with tags, each of which will drive the data into the layout
What is the best and most flexible way to do this?
PS. this task is relevant for absolutely any project and it will be very strange if there are 100 views of this topic and 5 replies. how the other 95 people do it is also interesting

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Skobkin, 2016-08-08
@skobkin

The first option is quite normal.
The second one is pretty pointless. Fence an extension for something it's not needed for.
The third one is not very good, because in this case, the far-left controllers will have to know something about the header, although they are doing something completely different.
The fourth one is also not the best idea.
Fifth - perhaps, too.
I would choose the first option, when a separate action is responsible for displaying data for the header or sidebar.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question