【问题标题】:Variables not rendering when template is passed from database从数据库传递模板时变量不呈现
【发布时间】:2019-04-29 04:16:03
【问题描述】:

我有用于电子邮件的数据库模板,其中包含变量 ant 等。 当我从数据库变量的视图中传递该模板时,不会呈现和显示 {{ $child_fullname }}、{{ $specialist_fullname }} 等,而不是给定名称。

这是我的邮件构建函数:

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
                ]);
}

刀片模板:

<!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>

结果:

提前感谢您的帮助!

P.S 用它来发送电子邮件。

【问题讨论】:

标签: laravel laravel-5.7


【解决方案1】:

它没有渲染,因为body 是一个传递给视图的变量。

您应该首先呈现body,然后将其包含在您的电子邮件中。

一种解决方案是使用https://github.com/TerrePorter/StringBladeCompiler,它将帮助您入门。

它的伪代码是:

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
            ])
        );
}

【讨论】:

  • 不支持 Laravel 5.7 版本:/。安装时出现错误。 wpb/string-blade-compiler 3.6 需要 laravel/framework 5.6.* -> laravel/framework[5.6.x-dev] 可以满足。
【解决方案2】:

试试Blade::compileString()

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

【讨论】:

  • 不工作。现在不显示任何内容而不是变量。
猜你喜欢
  • 2021-08-17
  • 2021-09-24
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 2016-10-10
  • 2017-04-04
相关资源
最近更新 更多