Writing @scope directive, how can I access $__env->getShared()?

I see some other non Sage directives are able to make use of $__env->getShared() to get all available data, but I can’t seem to figure out how to do this in Sage.

I’m working on a set of Blade directives for wrapping segments in a function scope so I can extract arrays without worrying about leaving a mess behind. Essentially it will work like an include without the file.

The basic usage at the moment is

@scope()
<a href="{{ $url ?? '' }}" target="{{ $taret ?? '' }}">{{ $title ?? '' }}<a>
@endscope($link)
{[-- $url, $target, and $title no longer exist --}}

I’m not thrilled with passing the data via the @endscope directive, but so far that’s the best I’ve come up with. Any suggestions there would be awesome as well.

Preferably I’d like to have it setup to pass the data as the first parameter of @scope, and have a second boolean parameter to determine if all available data for the view should be passed in as well.

You can see my efforts below. If anyone has any suggestions please let me know.

add_action('after_setup_theme', function () {
    sage('blade')->compiler()->directive('scope', function () {
        return <<<PARTIAL
                <?php
                (function (\$scopeData, \$envData){
                    extract(\$envData);
                    extract(\$scopeData);
                ?>
            PARTIAL;
    });

    sage('blade')->compiler()->directive('endscope', function ($expression) {
        return '<?php })(' . $expression . ', $__env->getShared()); ?>';
    });
});

This topic was automatically closed after 42 days. New replies are no longer allowed.