Laravel:通知中的 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41486212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:04:22  来源:igfitidea点击:

Laravel: HTML in notification

phplaravelnotificationslaravel-5.3

提问by rap-2-h

I'm using the default notification system(Laravel 5.3) to send an email. I want to add HTML tags in message. This does not work (it displays the strong tags in plain text):

我正在使用默认通知系统(Laravel 5.3) 发送电子邮件。我想在消息中添加 HTML 标签。这不起作用(它以纯文本形式显示强标签):

public function toMail($notifiable)
{
    return (new MailMessage)
                ->subject('Info')
                ->line("Hello <strong>World</strong>")
                ->action('Vtheitroad le reporting', config('app.url'));
}

I know it's normal because text is displayed in {{ $text }}in the mail notification template. I tried to use the same system as in csrf_field()helper:

我知道这是正常的,因为文本显示在{{ $text }}邮件通知模板中。我尝试使用与csrf_field()helper相同的系统:

->line( new \Illuminate\Support\HtmlString('Hello <strong>World</strong>') )

But it does not work: it displays strong as plain text.

但它不起作用:它显示为纯文本。

Can I send HTML tags without changing the view?(I don't want to change the view: protecting text is OK for all other cases). Hope it's clear enough, sorry if not.

我可以在不改变视图的情况下发送 HTML 标签吗?(我不想更改视图:在所有其他情况下都可以保护文本)。希望它足够清楚,如果不是很抱歉。

回答by Alexey Mezenin

Run php artisan vendor:publishcommand which will copy email.blade.phpto resources/views/vendor/notificationsfrom vendordirectory.

运行php artisan vendor:publish将复制email.blade.phpresources/views/vendor/notificationsfromvendor目录的命令。

Open this view and change {{ $line }}to {!! $line !!}in two places. In Laravel 5.3 these are 101and 137lines in the view.

打开此视图并在两个位置更改{{ $line }}{!! $line !!}。在Laravel 5.3这些101137在图线。

This will display unescapedlinestrings which will allow you to use HTML tags in notification emails.

这将显示未转义的line字符串,这将允许您在通知电子邮件中使用 HTML 标签。

回答by Eric Lagarda

Well, you can also create a new MailClass extending the MailMessageClass.

好吧,您还可以创建一个扩展MailMessage类的新 MailClass 。

For example you can create this class in app\Notifications

例如,您可以在 app\Notifications

<?php

namespace App\Notifications;

use Illuminate\Notifications\Messages\MailMessage;

class MailExtended extends MailMessage
{
    /**
     * The notification's data.
     *
     * @var string|null
     */
    public $viewData;

    /**
     * Set the content of the notification.
     *
     * @param string $greeting
     *
     * @return $this
     */
    public function content($content)
    {
        $this->viewData['content'] = $content;

        return $this;
    }

    /**
     * Get the data array for the mail message.
     *
     * @return array
     */
    public function data()
    {
        return array_merge($this->toArray(), $this->viewData);
    }
}

And then use in your notification:

然后在您的通知中使用:

Instead:

反而:

return (new MailMessage())

Change it to:

将其更改为:

return (new MailExtended())

And then you can use contentvar in your notification views. For example if you publish the notification views (php artisan vendor:publish), you can edit email.blade.phpin resources/views/vendor/notificationsand append this:

然后您可以content在通知视图中使用var。例如,如果你发布的通知的意见(php artisan vendor:publish),您可以编辑email.blade.phpresources/views/vendor/notifications和追加这一点:

@if (isset($content))
<hr>
    {!! $content !!}
<hr>
@endif

We do it like this and works like a charm :D

我们这样做并且像魅力一样工作:D

回答by thtg88

For anybody following @eric-lagarda's approach as I did, remember not to tab the content in the custom email.blade.phpview as you would normally do in your views, as it will be interpreted by Laravel's markdown parser as code, wrapping the whole contentHTML in <code>HTML tags. This caused me headaches but thanks to thisanswer I managed to figure out what the issue was. Your resulting bit of code to append to the email.blade.phpview, will therefore be this (please note the missing spaces/tabulation before the curly brackets):

对于像我一样遵循 @eric-lagarda 方法的任何人,请记住不要email.blade.php像通常在视图中那样在自定义视图中标记内容,因为它会被 Laravel 的降价解析器解释为代码,将整个contentHTML包装在<code>HTML 标签中. 这让我很头疼,但由于这个答案,我设法弄清楚了问题所在。因此,您生成的附加到email.blade.php视图的代码将是这样的(请注意大括号前缺少的空格/表格):

@if (isset($content))
<hr>
{!! $content !!}
<hr>
@endif

回答by AndyDunn

If you just want to add a bit of basic style to the template, you can use Markdown within the line()method without having to modify any other code.

如果你只是想给模板添加一些基本的样式,你可以在line()方法中使用 Markdown,而无需修改任何其他代码。