我如何在Laravel 5 Blade中使用nl2br() [英] How do I use nl2br() in Laravel 5 Blade

查看:150
本文介绍了我如何在Laravel 5 Blade中使用nl2br()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想在使用Blade Template Engine时保持数据库中的换行符.我想到了使用

So I want to keep linebreaks from the database while using the Blade Template Engine. I came up on the idea using

{!! nl2br(e($task->text)) !!}

有效.但这看起来像是不必要的复杂解决方案.有更好的方法吗?

It works. But it looks like a needlessly complicated solution. Is there a better way?

推荐答案

您可以定义自己的回声格式",将其与常规 content标记 {{ ... }}一起使用.默认格式为e(%s)(sprintf用于应用格式)

You can define your own "echo format" that will be used with the regular content tags {{ ... }}. The default format is e(%s) (sprintf is used to apply the formatting)

要更改格式,请在服务提供商内部致电setEchoFormat():

To change that format call setEchoFormat() inside a service provider:

public function boot(){
    \Blade::setEchoFormat('nl2br(e(%s))');
}

现在,您可以只使用普通的回显标记:

Now you can just use the normal echo tags:

{{ $task->text }}

对于不想应用nl2br()的回声,请使用三方括号{{{ ... }}}

For echos you don't want nl2br() applied, use the triple brackets {{{ ... }}}

要切换括号(三重和双重)的功能,请执行以下操作:

To switch the function of the brackets (triple and double) around, do this:

\Blade::setContentTags('{{{', '}}}');
\Blade::setEscapedContentTags('{{', '}}');

这篇关于我如何在Laravel 5 Blade中使用nl2br()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆