How can I manipulate yielded content from blade layout file

I am adding a translation service to our site using the DeepL API. Essentially you can pass HTML to the API as a string and it will return translated HTML.

This has been working in my filters.php file where I’ve successfully translated the page content and the page title using their respective filters. Unless I am missing something, doing the translation within filter callback functions requires repeated calls to the translation API (one for the page title, one for the content and so on so forth) which is slowing down page load times.

Ideally, I’d love to have the HTML page fully compiled and run it through the API. The closest I’ve been able to get is add the translation API method to my layout blade file where I’ve attempted to translate the yield content using:

 $__env->getSection('content');

While I can echo this method and have it print out the html for the page, when I run this through the method, I get an area saying the content is null.

Any idea why I can echo the $__env->getSection(‘content’) but why it’s NULL when I run it through the API?

The blade file is a layout.

Here is the relevant code.

@php
    $content= $__env->getSection('content');
    
    $deepl_api_key = $_SERVER['DEEPL_API_KEY'];
    $translator = new \DeepL\Translator($deepl_api_key);
    $content_translated = $translator->translateText($content, 'EN', $target_language, ['tag_handling' => 'html'] );
    echo $content_translated;
    @endphp

OMG. It worked fine. I misread the error. It was because I hadn’t defined the $target_language variable in the blade file.