I'm using Kirby CMS and I want to create a reusable snippet that outputs static content before and after dynamic content. I'm familiar with heredoc and output buffers, but I don't think they fit my use case that much.
I have the following.
The snippet file, test.php:
<span>some markup</span>
<?php $content() ?>
<span>end markup</span>
The main file (which includes the snippet):
<?php
$title = 'foobar';
?>
<?php snippet('test', ['content' => function () use ($title) { ?>
<p>some very <?= $title ?> content</p>
<?php }]) ?>
For those who are not familiar with Kirby, the snippet()
function simply includes a file from the designated snippets folder with some data passed to it. In this case, I pass an anonymous function that is available in the snippet as the content
variable.
I get the following output with no errors or warnings:
<span>some markup</span>
<p>some very foobar content</p>
<span>end markup</span>
So my code works as expected and does what I want it to. My question is - is this a good idea for usage in production? Are there some hidden caveats? I've been using PHP for quite a while and I haven't seen something like that before. Is it OK to use?