Laravel Blade 没有额外的空白?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26146045/
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
Laravel Blade without extra whitespace?
提问by Citizen
If you do this you get an error:
如果你这样做,你会得到一个错误:
<p>@if($foo)@if($bar)test@endif@endif</p>
And if you do this, you get <p> test </p>
, adding too much whitepace:
如果你这样做,你会得到<p> test </p>
,增加了太多的空白:
<p>@if($foo) @if($bar)test@endif @endif</p>
Is there a way to avoid this?
有没有办法避免这种情况?
Edit: see my answer below for an updated response. There's a clean way to do this with no hacks or external libraries.
编辑:有关更新的回复,请参阅下面的答案。有一种干净的方法可以做到这一点,无需黑客或外部库。
采纳答案by Citizen
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.
这个故事的寓意是当你使用 Blade 时,不要试图在元素中塞满很多条件。相反,让条件的结果包含元素。它干净,易于阅读,并且只用了几个字符。
回答by Alex Quintero
Try with a ternary operator, there is no whitespace control in Laravel
尝试使用三元运算符,Laravel 中没有空格控制
<p>{{ $foo ? ($bar ? 'test' : '') : ''}}</p>
回答by Omran Jamal
You could always use the hedronium/spaceless-bladepackage on packagist to add this functionality to Blade.
您始终可以使用packagist 上的 hedronium/spaceless-blade包将此功能添加到 Blade。
回答by Marcin Nabia?ek
As far as I know there is no spaceless
tag in Blade. If you want to use standard Blade tags you will have extra spaces. There is a github discussionwith proposal for new tag
据我所知spaceless
,Blade 中没有标签。如果您想使用标准 Blade 标签,您将有额外的空间。有一个关于新标签提案的github 讨论
回答by Matej Hanzli?
You can add {{""}} in between the code you want to close or connect without space.
您可以在要关闭或连接的代码之间添加 {{""}} 而不留空格。
@if($foo)@if($bar)test@endif{{""}}@endif
@if($foo)@if($bar)test@endif{{""}}@endif