Render specific template based on query_var

Hi,

I need to load a specific content and related template based on a custom query_var.
This because at the same URL can exists a page or a custom post type either.

Ex. I’ve this url
http://example.com/first/second/my-slug

If /first/second/my-slug exists and is a page, render the page with page.blade.php template.
Else if /first/second/my-slug exists and is a custom post type car, render the car with single-car.blade.php.

I’ve created a add_rewrite_rule and query_vars to manage the url.
But I’m not able to render the correct content with correct template.

In my tests I end with white pages or with the source code of for example page.blade.php (so I see @extends(‘layouts.app’)…etc), not with the complete and rendered template.

Any hint?

Thank you!

add_action( 'init',  function() {
    if (!is_admin()) {
        function custom_template_redirect() {
            global $wp;
            $content_slug = get_query_var('content_slug');
            $page = get_page_by_path( $wp->request );
            $car = get_page_by_path($content_slug, OBJECT, 'car');
    
            if ($content_slug) {
                if ($page && $page->post_status=== 'publish') {

                    // render $page with 'page.blade.php' template, how?

                } else if($car && $car->post_status === 'publish') {

                    // render $car with 'single-car.blade.php' template, how?

                }
            }
        }
        add_action('template_redirect', 'custom_template_redirect');
    }

    function custom_query_vars_filter($vars) {
        $vars[] = 'content_slug';
        return $vars;
    }
    add_filter('query_vars', 'custom_query_vars_filter');

    add_rewrite_rule(
        '^(first)/(second)/(.*)$',
        'index.php?content_slug=$matches[3]',
        'top'
    );
}, 9999);

You probably want to use template_include instead of template_redirect along with $this->app['sage.view'] to set the Blade view

You could reference a package like GitHub - generoi/sage-woocommerce to see how others are doing this