Use snippet with content representation (json.php)

Hello
First of all a disclaimer, I’m probably using kirby in a wrong way, learning everything by doing mistakes so my question may be dumb.
I would like to access the result html from a snippet into my default.json.php template in order to load it with ajax client side.
pseudo code would look something like that, and it’s obviously wrong :

$content = snippet('k7content');
$data = [
   'title' => $page->title()->value(),
   'content'  => $content
];
echo json_encode($data);

Is there a way to do that ?

You can use the snippet function like this to get the content as a return value:

$content = snippet('k7content', [], true);

// Or if you are on PHP 8
$content = snippet('k7content', return: true);

Ohhh thank you so much :slight_smile: