framework icon indicating copy to clipboard operation
framework copied to clipboard

[Proposal] Blade – Echo if condition is true

Open hipsterjazzbo opened this issue 12 years ago • 18 comments

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.

hipsterjazzbo avatar Jul 16 '14 04:07 hipsterjazzbo

{{ $condition ? $value : $defaults }} works. You could also do {{ $value ?: $default }} as of PHP 5.3.

bworwa avatar Jul 16 '14 13:07 bworwa

http://laravel.com/docs/templates#other-blade-control-structures

franzliedke avatar Jul 16 '14 13:07 franzliedke

@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.

hipsterjazzbo avatar Jul 16 '14 22:07 hipsterjazzbo

Further to @bworwa's comment, the compiled statement might be more like

<?php echo $condition ? $value : ''; ?>

hipsterjazzbo avatar Jul 16 '14 23:07 hipsterjazzbo

And why not use {{ $condition ? $value : '' }}, besides saving 2 characters.

bworwa avatar Jul 17 '14 00:07 bworwa

By that logic why have the existing {{ $value or 'Default }}? Simply a nicer syntax.

hipsterjazzbo avatar Jul 17 '14 01:07 hipsterjazzbo

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.

bworwa avatar Jul 17 '14 01:07 bworwa

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).

hipsterjazzbo avatar Jul 17 '14 01:07 hipsterjazzbo

Nope, the or syntax actually works with non-defined variables, too - which is where your suggestion would fail.

franzliedke avatar Jul 17 '14 07:07 franzliedke

@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 avatar Jul 17 '14 12:07 bworwa

@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 avatar Jul 17 '14 13:07 crynobone

@crynobone @HipsterJazzbo Fair enough.

bworwa avatar Jul 17 '14 13:07 bworwa

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 avatar Jul 18 '14 00:07 hipsterjazzbo

@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.

Garbee avatar Jul 18 '14 10:07 Garbee

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

hipsterjazzbo avatar Jul 18 '14 10:07 hipsterjazzbo

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.

Garbee avatar Jul 18 '14 11:07 Garbee

I've opened a pull request at #5171.

hipsterjazzbo avatar Jul 20 '14 23:07 hipsterjazzbo

thanks {{ $boolean? 'checked' : '' }}

bandagriss avatar Dec 31 '17 04:12 bandagriss