Laravel 4.2刀片模板三元快捷方式

The short cut for

{{{ isset($name) ? $name : 'Default' }}}

Is:

{{{ $name or 'Default' }}}

Is there a short cut for:

{{ property_exists( Auth::user(), 'firstname' ) ? Auth::user()->firstname : 'Your Name' }}

Or is there a better (shorter) way to write this? Suggestions are welcome.

Consider that sometimes shorter != better, especially if you'll be returning back to your code in the future.

The shortest I can think of is using the object_get() helper:

{{ object_get(Auth::user(), 'firstname', 'Your name') }}

Basically is the same as array_get(), but with objects: if the property doesn't exist, and you provide a default value as 3rd argument, that value is returned, otherwise you get the object->property value.

I think the usage of a default value is not well documented, but if you look under vendor/Laravel/Illuminate/Support/helpers.php, at lines 771-787, you get the function definition:

function object_get($object, $key, $default = null)
    {
        if (is_null($key) || trim($key) == '') return $object;

        foreach (explode('.', $key) as $segment)
        {
            if ( ! is_object($object) || ! isset($object->{$segment}))
            {
                return value($default);
            }

            $object = $object->{$segment};
        }

        return $object;
    }