Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
Pass an array to a twig macro in Symfony

I have a controller, a twig macro and the main template where the macro is imported. I would like to send an array in my macro and for each row of my table I want to display its data.

A line is composed of : title, description, img, type

The controller :

/**
 * @Route("/realisations", name="realisations")
 * @Method({"GET"})
 *
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function realisationsAction()
{
    return $this->render('MyBundle:page:realisations.html.twig', $datas = array('type' => 'The type',
                                                                                 'img' => 'test.jpg',
                                                                                 'titre' => 'Test',
                                                                                 'description' => 'The description'));
}

The main template :

{% import "MyBundle:macros:realisationsMacros.html.twig" as macros %}

{{ macros.realisations(datas) }}

The macro :

    {% macro realisations(datas) %}
    {% for data in datas %}
        <div class="project {{ data.type }} isotope-item" style="position: absolute; left: 0; top: 0; transform: translate3d(0px, 0px, 0px);">
            <div class="content">
                <div class="image">
                    <img src="{{ asset('bundles/jsmetallerie/img/realisations/'~ data.type ~'/13.png') }}" alt="{{ data.titre }}">
                    <div class="item-hover">
                        <ul class="item-icons">
                            <li><a href="{{ asset('bundles/jsmetallerie/img/realisations/'~ data.type ~'/'~ img) }}" data-fancybox><i class="fa fa-search"></i></a></li>
                            <li><a href="{{ path('jsm_realisation_projet') }}"><i class="fa fa-link"></i></a></li>
                        </ul>
                    </div>
                </div>
                <span class="h6">{{ data.titre }}</span>
                <p>{{ data.description }}</p>
            </div>
        </div>
    {% endfor %}
{% endmacro %}

The Symfony error is : "Variable "datas" does not exist."

I think the error comes from creating my table in the controller but I do not know how to create it correctly

Thanks in advance

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

simply pass an array with a key named 'datas' as example:

    $params = array(
        'datas' => array('type' => 'The type',
                         'img' => 'test.jpg',
                         'titre' => 'Test',
                         'description' => 'The description')
    );

    return $this->render('MyBundle:page:realisations.html.twig', $params);

Hope this help

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda