ACF block blade template

Hi. I have my blade template for my ACF block:

app/resources/views/blocks/my-block.blade.php

The acf_register_block_type function has the template path. It works:

'render_template' => 'app/themes/sage/resources/views/blocks/my-block.blade.php',

But none of blade markup inside this template is rendered in the front. I mean, this:

@if (true)
    test
@endif

is rendered literally:

Captura de pantalla 2022-08-06 a las 20.05.47

Normal PHP, like this, works:

<?php echo get_field('my_block_field'); ?>

How can I make the templates transpile the blade markup? it’s possible?

You might look into ACF Composer or Poet for managing the Blade views for ACF blocks.

They take care of the rendering of the blade view instead of just rendering it as php which is I think the default from ACF.

1 Like

I was looking into ACF Composer and Poet but it seems to include a lot more than I was needing. I ended up using render_callback instead and echoed the \Roots\view function to use the blade template in my block. Hopefully this helps future googlers.

class HeroHome
{
    public function __construct()
    {
        add_action('acf/init', [$this, 'init']);
    }

    public function init()
    {
        acf_register_block_type(array(
            'name'              => 'badegg/hero-home',
            'title'             => __('Hero Home'),
            'description'       => __('Hero Banner for the home page'),
            'render_callback'   => [ $this, 'render'],
            'category'          => 'layout',
        ));
    }

    public function render($args = [])
    {
        $fields = [
            'heading',
            'blurb',
            'links',
            'type',
            'colour',
            'opacity',
            'image',
            'video',
        ];

        foreach($fields as $field):
            $args[$field] = get_field($field);
        endforeach;

        echo \Roots\view('blocks.hero-home', $args)->render();
    }
}

Thanks for bumping this with a solution! Our docs should have this covered at this point:

1 Like

Ooo, even better! Thanks Ben.