[Sage 10] Wrong path with dynamic path dirname(__FILE__)

So, here is the thing, when I use dirname(__FILE__) I expect it to find the path on its own, which it works, however, it has the wrong directory.

What do I mean by that?

Take a look at that path, what it should be is this:

/var/www/html/wp-content/themes/TrafikStudioTheme/resources/views/blocks/block-clients.blade.php

Note the difference being at resources/views - I don’t understand they sage? Has the wrong directory when using a dynamic path.

Otherwise, how will I tell the file to search the path on its own? So far this is annying because I need to comment this each time I push the change, and then change it back so it works on localhost, and its in multiple files I have to do this.

__FILE__ returns the current file being executed. The blade files you create are never actually executed: they are used as markup to generate php files, and those are executed. Those generated php files are in the storage folder you see there.

Can you explain why you need the full system path of a blade? There may be another way to solve your problem.

Right.

Because when I try to grab a file from blocks, a directory I created, it seems I need to get the full path, same goes in digital ocean. So what I do is comment in/out the flexible content path each time on multiple templates.

This is my code:

@extends('layouts.app')
@section('content')

{{-- page.blade.php --}}

<?php
//$flexibleContentPath = dirname(__FILE__) . '/blocks/';
$flexibleContentPath = $flexibleContentPath = "/var/www/html/wp-content/themes/TrafikStudioTheme/resources/views/blocks/";
//$flexibleContentPath = "C:\\Users\\44775\\Desktop\\WebDevelopment\\Personal\\TrafikStudio\\wp-content\\themes\\TrafikStudioTheme\\resources\\views\\blocks\\"; 
$count = 0;
?>


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


<x-section container="{{ $page[$count]['container'] }}" paddingTop="{{ $page[$count]['paddingTop']}}" paddingBottom="{{ $page[$count]['paddingBottom']}}" bgColor="{{ $page[$count]['backgroundColor'] }}">
@if ( have_rows( 'row' ) )
@while ( have_rows( 'row' ) ) <?php the_row(); ?>
 

    @if ( have_rows( 'column' ) )
    @while ( have_rows( 'column' ) ) <?php the_row(); ?>
    
        <?php 
            $layout = get_row_layout();
            $layoutConverted = str_replace( '_', '-', $layout);
            $file = ( $flexibleContentPath . str_replace( '_', '-', $layout) . '.blade.php' );
        ?>

        @if( file_exists( $file ))
            <?php include($file) ?>
        @else
            <?php echo "File $file with the name of $layoutConverted doesn't exists" ?>    
        @endif 
 
    
    @endwhile
    @endif
    

@endwhile
@endif
</x-section>


<?php $count++ ?>

@endwhile
@endif


@endsection

It looks like you’re trying to rebuild basic blade functionality. Why can’t you use @include( 'blocks.' . $layoutConverted )? That should have exactly the same result without needing to determine the full path.

1 Like

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