0

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?

dodov
  • 5,206
  • 3
  • 34
  • 65
  • _I've been using PHP for quite a while and I haven't seen something like that before_ .. If you have been using PHP for a while, what does your experience say about "good practice"? – dbf Apr 12 '19 at 06:58
  • The code I posted is clean, does what I need it to, and works. Therefore it must be good practice. However, I always consider the possibility of being wrong, which is why I ask now, instead of regretting later. – dodov Apr 12 '19 at 07:07

0 Answers0