[Proposal] Blade – Echo if condition is true
When building views, I often find myself wanting to echo a value based on a condition, something like this:
@if ($somecondition)
{{{ $variable }}}
@endif
This starts looking really messy where the value should be echoed inline, say into an HTML class tag or something.
I really like the syntax for defaults of {{{ $variable or 'Default' }}}, so I propose a similar syntax:
{{{ $variable if $someCondition }}}
This would only echo the variable if the condition evaluates to true. The underlying PHP code would be something like:
<?php echo $someCondition ? $value : ''; ?>
I'll put this together and open a pull request if this proposal is approved. This is my first proposal to this repo, so apologies if I've done it incorrectly.
{{ $condition ? $value : $defaults }} works. You could also do {{ $value ?: $default }} as of PHP 5.3.
http://laravel.com/docs/templates#other-blade-control-structures
@bworwa Yes, but that's not what I'm proposing. I'm proposing a syntax to print a $value if a $condition is true. Not to print a $value or a $default.
@franzliedke I don't understand why you've linked to that article.
Further to @bworwa's comment, the compiled statement might be more like
<?php echo $condition ? $value : ''; ?>
And why not use {{ $condition ? $value : '' }}, besides saving 2 characters.
By that logic why have the existing {{ $value or 'Default }}? Simply a nicer syntax.
Yes, {{ $value or 'default' }} is equivalent to {{ $value ?: 'default' }} and my guess is that it was introduced due to the lack of awareness of the 5.3 ?: introduction. Thing is, where do we draw a line on creating aliases to functionality that already exists.
It's just a nicer syntax. Code should be readable. Laravel is chock full of little syntactic nicities that make development easier, more intuitive and nicer to read.
FWIW, {{ $value ?: 'default' }} does not check whether $value is set. {{ $value or 'default' }} actually compiles to
<?php echo isset($value) ? $value : 'default'; ?>
which is very similar to what this proposal would compile to
<?php echo $condition ? $value : ''; ?>
(which saves 13 characters).
Nope, the or syntax actually works with non-defined variables, too - which is where your suggestion would fail.
@HipsterJazzbo {{ $value ?: 'default' }} does check whether the variable is set.
// unset($variable);
// $variable = null;
// $variable = 'something';
echo $variable ?: 'default';
// 'default'
// 'default'
// 'something'
echo $someRandomNotDefinedVariable ?: 'default';
// 'default'
@bworwa with above example, yes.
✓ php -a
Interactive shell
php > $a['hello'] = 'world';
php > $a['foo'] ?: 'bar';
PHP Notice: Undefined index: foo in php shell code on line 1
PHP Stack trace:
PHP 1. {main}() php shell code:0
@crynobone @HipsterJazzbo Fair enough.
Sorry, I should have made it more clear that I was talking about whether a variable isset(), not whether is has a value.
So, now that we've discussed the subleties of the ?: operator, are we cool with me opening a pull request for this? I can have it done in about half the time it takes to read this thread, and I think it's not only a useful and time-saving feature, but it complements the existing blade functionality nicely.
@taylorotwell Care to comment?
@HipsterJazzbo You are probably better off just submitting a PR and seeing what happens. It is better to see things implemented then talking about how they would be. Any needed discussion can take place in the PR for something this small.
Cool. I only did it this way cause that's what the contrib guidelines said. I'll open a PR then.
On Fri, Jul 18, 2014 at 10:07 PM, Jonathan Garbee [email protected] wrote:
@HipsterJazzbo You are probably better off just submitting a PR and seeing what happens. It is better to see things implemented then talking about how they would be. Any needed discussion can take place in the PR for something this small.
Reply to this email directly or view it on GitHub: https://github.com/laravel/framework/issues/5098#issuecomment-49415264
Yea, if you can crank out a PR for an additional feature in a few minutes just PR it.
Anything with substantial changes or that could incur BC changes should have a proposal first. People shouldn't waste time dealing with big things without asking first, then feelings get hurt when it isn't accepted and they have spent all the time coding it.
I've opened a pull request at #5171.
thanks {{ $boolean? 'checked' : '' }}