Framework: Blade "or" syntax not working with associative arrays

Created on 29 Nov 2016  路  5Comments  路  Source: laravel/framework

  • Laravel Version: 5.3.25
  • PHP Version: 5.6.4
  • Database Driver & Version:

Description:

The blade "or" construct has been broken by https://github.com/laravel/framework/commit/e8a9ed155fd10ac7b3595421055d1577fee246a0, the regex does not match when using associative arrays.

Steps To Reproduce:

Use {{ $array['foo'] or 'bar' }} in any view and get "undefined index 'foo'".

Failing test:

$compiler = new Illuminate\View\Compilers\BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php echo isset($name[\'bar\']) ? $name[\'bar\'] : \'foo\'; ?>', $compiler->compileString('{!! $name[\'bar\'] or \'foo\' !!}'));

Most helpful comment

I have an experimental idea for how to fix this for really reals. It will require significant work though. I'm going to attempt this when I have some free time summer 2017.

All 5 comments

You can just use:

{{ $array['foo'] ?? 'bar' }}

@GrahamCampbell He can't since he's on PHP Version: 5.6.4... But yeah this is useless for people on PHP7, guess it'll be removed soon...

Should we revert that fix and apply it in a future release?
It fixes an edge case

{{$var['a or b']}}

This seems more important...

the mentioned PR was reverted.

I have an experimental idea for how to fix this for really reals. It will require significant work though. I'm going to attempt this when I have some free time summer 2017.

I've been trying to figure out the perfect regex. This is still buggy and needs to be fixed.

Lesson learnt : add as many test cases as possible.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabriellimo picture gabriellimo  路  3Comments

kerbylav picture kerbylav  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments