Roots Discourse

[Sage 10] Dynamic files that are being "@include" don't compile

I’m creating something like a page builder with ACF. I’m using blade components and they all work all over the website, except in one place, and that is, in the ‘views’ folder, in a custom folder I created named ‘blocks’.

The files get called while looping over with ACF, like so:

    <?php

        // $flexibleContentPath = dirname(__FILE__) . '\\blocks\\';

        $flexibleContentPath = "C:\\Users\\44775\\Desktop\\Web Development\\Clients\\KayLittlehales\\FutureproofED\\wp-content\\themes\\kayTheme\\resources\\views\\blocks\\";

        $count = 0;

    ?>

    @if ( have_rows( 'flexible_content' ) ) 

        @while ( have_rows( 'flexible_content' ) ) <?php the_row(); ?>

     

            @if ( have_rows( 'row' ) )

            @while ( have_rows( 'row' ) ) <?php the_row(); ?>

            <x-section bgColor="{{ $page[$count]['backgroundColor'] }}">

                @if ( have_rows( 'column' ) )

                @while ( have_rows( 'column' ) ) <?php the_row(); ?>

                <div class="{{ $page[$count]['columnWidth'] }}">

                    <?php 

                        $layout = get_row_layout();

                        // print_r($page[$count]);

                        $file = ( $flexibleContentPath . str_replace( '_', '-', $layout) . '.blade.php' );                        

                        if ( file_exists( $file ) ) {

                            @include( $file );

                        }

                    ?>

                                

                </div>

                @endwhile

                @endif

                

            </x-section>

            @endwhile

            @endif

            

            <?php $count++; ?>

        @endwhile

    @endif

    @endsection

The issue I face, is that blade components, that are being @include( $file ); don’t compile, although everything works, and the components, as seen on the page such as x-section works.

I can’t seem a way to figure out a way to compile the files. For example, in blocks/blocks-hero.blade.php I have this code:

<x-button variant="primary" kind="solid">button</x-button>

And it doesn’t work, however, it does work on every other page.

The browser literlly shows this: <x-button variant="primary" kind="solid">button</x-button> in the HTML. While on other pages it shows the code compiled.

I have no idea how to solve this. Tried a hundred things and nothing.

Blade directives don’t work inside of php tags. I’m your example it’s probably using the php include, which won’t compile blades, just import them as text, as you’ve seen.

So you’re saying the include I am using, is basicallly wrong, right?

It works when getting the file, however, when I do try to use blade, it breaks, white screen of death.

 <?php 
                    $layout = get_row_layout();
                    // print_r($page[$count]);
                    $file = ( $flexibleContentPath . str_replace( '_', '-', $layout) . '.blade.php' );                        

                    if ( file_exists( $file ) ) {
                         ?>
                        @include( $file );
                        <?php
                    }
                ?>

Or how would you do it? Because as soon as I add php tags near include, it breaks.