RobinRadic / blade-extensions

Laravel Blade extensions like $loop->odd/$loop->index in foreach, view blocks and partials, etc
http://robin.radic.nl/blade-extensions
MIT License
267 stars 35 forks source link

BLADE-3 ⁃ @set Directive makes duplicate calls #68

Closed marlongichie closed 7 years ago

marlongichie commented 7 years ago

When I used the @set directive like so :

@set($some_variable, $model->someFunction())

The $model->someFunction() is being called twice.

If I replace the directive with standard PHP tags like so:

<?php $some_variable = $model->someFunction(); ?>

Everything works normal... the $model->someFunction() is called once as expected.

Anyone else come across this issue?

Please advise.

RobinRadic commented 7 years ago

Hey,

It's a silly mistake:

@set($some_variable, $model->someFunction())

transforms into

$some_variable = $model->someFunction(); $__data['some_variable'] = $model->someFunction(); 

should transform into

$some_variable = $model->someFunction(); $__data['some_variable'] = $some_variable; 

cause

in: Radic\BladeExtensions\Directives\SetDirective

class SetDirective extends AbstractDirective
{
    protected $pattern = '/(?<!\w)(\s*)@NAME\s*\(\s*\${0,1}[\'"\s]*(.*?)[\'"\s]*,\s*([\W\w^]*?)\)\s*$/m';

    protected $replace = '$1<?php \$$2 = $3; $__data[\'$2\'] = $3; ?>';
    // which should actually be
    protected $replace = '$1<?php \$$2 = $3; $__data[\'$2\'] = $2; ?>'; //  (last $3 should be $2)
}

fix

I'll release a patch soon. In the mean time you can use this fix. create: App/SetDirective.php

namespace App;
class SetDirective extends \Radic\BladeExtensions\Directives\SetDirective
{
    protected $replace = '$1<?php \$$2 = $3; $__data[\'$2\'] = $2; ?>';
}

modify: config/blade-extensions.php

return [
    'directives'        => [
        //'set'   => 'Radic\\BladeExtensions\\Directives\\SetDirective',
        'set'   => 'App\\SetDirective',
        // omited all the other stuff
     ]
];
marlongichie commented 7 years ago

@RobinRadic was this update included in v7.0 ???

RobinRadic commented 7 years ago

No,

I intend to have the next release include the changes for Laravel 5.5, as well as this fix. But i'm currently hold up by another project, so it might take another week or 2.

Until i release the next version, use the fix i mentioned in the previous reply.

As an alternative, you could define in your composer.json

{
    "require": {
        "radic/blade-extensions": "dev-master#ff39b0d7040dba11d57545452efd68d008779c76"
    }
}

or

{
    "require": {
        "radic/blade-extensions": "dev-master#ff39b0d7040dba11d57545452efd68d008779c76 as 7.1.0"
    }
}