Skip to content
Advertisement

How to pass variable name from controller to twig file in Symfony?

I need to display image at my twig file. I am passing filename information as below:

    $emailContent = [
        'filename' => "$filename.png"
    ];

    $this->sendMail(
        ,,,
        ...
        'Emails/content.html.twig',
        $emailContent
    );

In my twig file, when I put the filename directly it works.

<img src="{{ email.image("@images/1000.png") }}" alt="It Works!!">

But, I am not sure how to pass filename from controller to twig html file.

I already tried following lines:

<img src="{{ email.image("@images/{{ filename }}") }}" alt="test ONLY">
<img src="{{ asset("images/logo/") ~ filename }} " alt="???" />

It didn’t worked.

Can anybody please help.

Advertisement

Answer

Use concatenation for argument of email.image:

<img src="{{ email.image("@images/" ~ filename) }}" alt="test ONLY">
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement