Laravel Blade是否没有多余的空格? [英] Laravel Blade without extra whitespace?
问题描述
如果执行此操作,则会出现错误:
If you do this you get an error:
<p>@if($foo)@if($bar)test@endif@endif</p>
如果执行此操作,则会得到<p> test </p>
,并添加了过多的空白:
And if you do this, you get <p> test </p>
, adding too much whitepace:
<p>@if($foo) @if($bar)test@endif @endif</p>
有办法避免这种情况吗?
Is there a way to avoid this?
请参阅下面的我的答案以获取更新的回复.有一种干净的方法可以做到这一点,而无需黑客或外部库.
see my answer below for an updated response. There's a clean way to do this with no hacks or external libraries.
推荐答案
这似乎吸引了很多搜索流量,所以我认为我要添加一个更新以分享这几天的处理方式.基本上,这是更多代码,但最终却变得愚蠢简单且非常干净:
this appears to be getting a lot of search traffic so I figured I'd add an update to share how I'm handling this these days. Basically, it's a little more code but it ends up being stupid simple and very clean:
@if($foo)
<p>Test</p>
@elseif($bar)
<p>Test2</p>
@else
<p>Test3</p>
@endif
故事的寓意是当您使用刀片时,不要试图在元素中塞满很多条件.而是让条件包含元素的结果.它很干净,易于阅读,并且只花了几个字符.
The moral of the story is when you're working with blade, don't try to cram a lot of conditionals within elements. Rather, have the result of the conditional contain the element. It's clean, easy to read, and with only a few more characters spent.
这篇关于Laravel Blade是否没有多余的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!