简体   繁体   中英

twig variable won't pass - symfony sonata

5 days have gone by, still no success!

I dumped the results of my card variable and it returns results.

But when I try to pass it trough varibale on my twig tamplate it throws:

Variable "card" does not exist.

It's defined in SonataAdmin.

protected function configureShowFields(ShowMapper $showMapper)
{

    $card = $this->getCardTransactions(); // on dump(), it works

    $showMapper->tab('Cards')
                    ->add('Data', 'string', array(
                        'template' => "@AdminTemplates/sonata/details.html.twig",
                        'card' => $card
                    ))
                    ->end()
                ->end();
}

and in my twig;

 {% for c in card %}
      {{ c.id }}
  {% endfor %}

I think it has to do with SonataAdmin and how it handles this type of calls but I have read the documentation and searched online but still no luck.

You have to use the field_description.options object in your template to access your variable.

protected function configureShowFields(ShowMapper $showMapper)
{

    $showMapper
        ->tab('Cards')
            ->add('Data', 'string', [
                'template' => "@AdminTemplates/sonata/details.html.twig",
                'card' => $this->getCardTransactions(),
            ])
        ->end();
}
{# @AdminTemplates/sonata/details.html.twig #}
{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %}

{%- block field -%}
    {% spaceless %}
        {% for card in field_description.options.cards %}
            {{ card.id }}
        {% else %}
            <p>No card</p>
        {% endfor %}
    {% endspaceless %}
{%- endblock -%}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM