Gutenberg Block render_template and Blade?

Hi!
I’m playing around with sage 9 and so far it’s been great. I was able to create my custom gutenberg blocks but one thing I can’t figure out is how would I go about using blade as render_template on a Gutenberg Block definition? Is there a way to call the compiler to run over that custom template file I created?

thanks

What method are you using to create your blocks?

I’m using acf_register_block_type like so

    acf_register_block_type([
    'name' => 'test-block',
    'title' => 'Test',
    'description' => 'Hey this is a test',
    'render_template' => config('theme.dir') . '/resources/views/modules/blocks/test.blade.php',
    ]);

test.blade.php outputs blade’s directives as string. The html parts work fine, of course.

Ah, so let me make sure I understand the problem. It’s outputting correctly, but it seems like the block template is not passing through the Blade processor. Is that correct?

Exactly! I was wondering if I have to either: 1) call some method to blade to compile it, or 2) change where I store my gutenberg block files?

Try this instead:

acf_register_block_type([
    'name' => 'test-block',
    'title' => 'Test',
    'description' => 'Hey this is a test',
    'render_template' => \Roots\view('modules.blocks.test')->render(),
]);

I’m getting the following error: Fatal error : Uncaught Error: Call to undefined function Roots\view()

Oh, Important question: are you doing this in your theme, or in a plugin?

I’m doing it in my theme.

Oh. Sage 9. You said it in your first post.

… I don’t remember how to do this in Sage 9. Gimme a few minutes to find a project with an example.

Thank you so much! appreciate the time and help

Try this:

acf_register_block_type([
    'name' => 'test-block',
    'title' => 'Test',
    'description' => 'Hey this is a test',
    'render_template' => App\template('modules.blocks.test'),
]);

I think that’s the right way to call that. If not it should get your moving in the right direction.

1 Like

Ok I will test it out! So far it is not outputing anything, but I will investigate that “template” method. Thanks!

this worked!

    acf_register_block_type([
    'name' => 'test-block',
    'title' => 'Test',
    'description' => 'Hey this is a test',
    'render_template' => \App\template_path(locate_template("views/modules/blocks/test.blade.php")),
]);
2 Likes

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