TerrePorter / StringBladeCompiler

Render Blade templates from string
MIT License
175 stars 76 forks source link

Can I use this in a resource to parse custom directives? #56

Closed Cipa closed 1 year ago

Cipa commented 4 years ago

A solution I found is: $parsedContent = \View::make(['template' => Blade::compileString($this->content)], ['token' => 'I am the child template'])->render();


Hi,

I am trying to use this package in a Resource like below

$this->content has a custom directive: <p>@tv('test') Error voluptate</p>

The blade directive is:

Blade::directive('tv', function ($tv) {
            return "<?php echo $tv . '-works'; ?>";
        });

What would it take to have something like this working: 'parsedContent' => StringBlade::compileString($this->content)

Thank you.

class ResourceResource extends JsonResource
{
    public function toArray($request)
    {
        return array_merge(
            parent::toArray($request),
            [
                'template' => $this->template,
                'type' => $this->type,
                'parsedContent' => StringBlade::compileString($this->content)
            ]
        );
    }
}
TerrePorter commented 3 years ago

not sure, but i added this from someones fork related to custom directives

if(config('blade.autoload_custom_directives')) {
            $blade = app('blade.compiler');
            $string_blade = app('stringblade.compiler');

            collect($blade->getCustomDirectives())
                ->each(function($directive, $name) use ($string_blade) {
                    $string_blade->directive($name, $directive);
                });
        }