Sprunkas
Sprunkas

Reputation: 433

Variables not rendering when template is passed from database

I have in database template for email with variables ant etc.. When I pass that template in my view from database variable are not rendered and displayed {{ $child_fullname }}, {{ $specialist_fullname }} and etc. instead of given name.

Here is my Mail build function:

public function build()
{
    return $this->subject('Pranešimas apie sėkmingą registraciją')
                ->view('email-template')
                ->with([
                    'body'                => EmailTemplate::find(1)->text,
                    'child_fullname'      => $this->child->name . ' ' . $this->child->surname,
                    'specialist_fullname' => $this->card->specialist->name,
                    'date_time'           => $this->visits->workSchedule->work_date . 
                                             ' ' . $this->visits->workSchedule->work_time
                ]);
}

Blade template:

<!doctype html>
<html>
    <head>
        <meta name="viewport" content="width=device-width">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    {!! $body !!}
</html>

Result: enter image description here

Thanks in advance for help!

P.S Using this to send email.

Upvotes: 0

Views: 313

Answers (2)

Leena Patel
Leena Patel

Reputation: 2453

Try Blade::compileString()

{!! Blade::compileString($body) !!}

Upvotes: 0

Unamata Sanatarai
Unamata Sanatarai

Reputation: 6637

It's not rendering because body is a variable passed to the view.

You should first render the body, then include it into your e-mail.

One solution is to use https://github.com/TerrePorter/StringBladeCompiler it will get you started.

The pseudocode for it is:

public function build()
{
    return $this->subject('Pranešimas apie sėkmingą registraciją')
        ->view('email-template')->with(
            [
                'body'                  => renderFromString(EmailTemplate::find(1)->text, 
                [
                    'child_fullname'        => $this->child->name . ' ' . $this->child->surname,
                    'specialist_fullname'   => $this->card->specialist->name,
                    'date_time'             => $this->visits->workSchedule->work_date . ' ' . $this->visits->workSchedule->work_time
            ])
        );
}

Upvotes: 2

Related Questions