【问题标题】:Use variable inside another variable in a mail blade view在邮件刀片视图的另一个变量中使用变量
【发布时间】:2019-07-30 15:46:05
【问题描述】:

我正在使用 Laravel 中的 Mail 库来发送带有自定义数据的 html 电子邮件,并传递给刀片视图。

当邮件必须呈现从数据库中的一行获取的 html 时产生的问题,其中包含我通过视图传递的变量。

这是我的可邮寄类中的构建函数

public function build()
{
   return $this->from('hello@test.it')
     ->view('view')
     ->with([
       'url'     => 'https://google.com',
       'text' => $this->parameters->text,
      ]);
}

然后在刀片视图中:

<div>
  {!! $text !!}
</div>

这是 $text 变量的样子:

<p>
  <span>This is my text for the mail</span>
  <a href="{{ $url }}">Click here to compile</a>
</p>

链接href应该包含url变量值而不是不传递变量名本身

【问题讨论】:

    标签: php html laravel


    【解决方案1】:

    一个简单的解决方案是用 php 格式化:

    public function build()
    {
        return $this->from('hello@test.it')
         ->view('view')
         ->with([
           'text' => str_replace('{{ $url }}','https://google.com',$this->parameters->text)
          ]);
     }
    

    【讨论】:

    • 这不起作用,因为在 str_replace 中传递的 $url 变量将被解释为变量而不是字符串
    【解决方案2】:

    我没有自己尝试,但您可以尝试使用Blade::compileString(),即:

    public function build()
    {
        return $this->from('hello@test.it')
          ->view('view')
          ->with([
            'url'     => 'https://google.com',
            'text' => \Blade::compileString($this->parameters->text),
        ]);
    }
    

    【讨论】:

    • 这看起来不错,但它删除了变量而不替换任何东西。
    • 请尝试使用普通的 var 扩展 &lt;a href="$url" 并且不使用 Blade::compileString()
    • 它只是将 url 转换为 "$url" 这就是代码现在的样子:lang-php return $this-&gt;from('hello@test.it') -&gt;view('view') -&gt;with([ 'url' =&gt; 'https://google.com', 'text' =&gt; $this-&gt;parameters-&gt;text, ]); 在数据库中:lang-html &lt;p&gt; &lt;span&gt;This is my text for the mail&lt;/span&gt; &lt;a href="$url"&gt;Click here to compile&lt;/a&gt; &lt;/p&gt;
    猜你喜欢
    • 2015-03-13
    • 2018-07-24
    • 2022-11-02
    • 1970-01-01
    • 2018-03-29
    • 2014-03-15
    • 2017-08-24
    • 2017-12-01
    • 2012-08-27
    相关资源
    最近更新 更多