Using blade templates with WP Blocks

I have old WP_Widget classes I am working to update to WP Blocks using ACF Blocks. It works fine except for the rendering. Previously in the WP_Widget class, I just rendered content like this:

/**
         * Outputs the content of the widget
         *
         * @param array $args
         * @param array $instance
         */
        public function widget($args, $instance)
        {
           ...
           ...
           ...
            // outputs the content of the widget
            echo \Roots\view('partials.sidebars.newsletter-signup', $args);
        }

It let me use View composers and all the other blade templating features.

Now I have a block.json file for this widget and I tried to use the renderCallback option mentioned here like this:

// file is located in resources/views/blocks/newsletter.blade.php
function newsletter_block_render_callback($block)
{
    // outputs the content of the widget
    return \Roots\view('blocks.newsletter');
}

I tried multiple different paths, including views & resources/views/. I have also tried echo instead of return. None of it works.

If I use the renderTemplate option like this:

"renderTemplate": "resources/views/blocks/newsletter.blade.php"

It loads the template properly. However, I cannot use any blade functionality like @php @if or {{ $var }}. It requires me to use the <?php tags and echo any PHP variable.

So it does work, but if I can keep consistency across the theme and use the blade rendering templates instead of echo and <?php blocks like in the example here, that would be preferred.

See Rendering Blade Views | Acorn Docs | Roots

1 Like

Perfect, thank you! I was relying on Google too much to find the answer and didn’t consider going through the acorn docs.

1 Like