Custom PHP code per page

Hi, I created a little Kirbytext tag that lets you include a html file from your file list. It works like this:

site/tags/include.php

    kirbytext::$tags['include'] = array(
        'attr' => array(
            'include'
        ),
        'html' => function($tag) {
            $filename = $tag->attr('include');
            if($path = $tag->file($filename)){
                return $path->read();
            }
        }
    );

So with the tag (include: filename.html) you can include any html file, that you previously added to your pages filelist.

But now, I also wanted to expand the tag, so that it also could include a php file, and of course also processes the php code. Is that possible? I didn’t find a matching method in the cheatsheet.
Any ideas how to do it?

1 Like

You can’t even upload php files for security reasons.

1 Like

Well, as a admin I can upload it into the content folder. Anyways;
Imagine I had some kind of a portfolio website and on every post I also wanted to run some custom PHP code.
What would be a good approach to solve this?
Thank you very much for your ideas!

You could use a custom tag to include a snippet from the snippets folder: Embedding Snippets

And I would use the same approach for snippets that only contain html. No need to put these folders into the page folders, at least not for this kind of use case.

1 Like