Search code examples
phpincludepartial

PHP include partial file name


So I've been looking for an option in PHP I'm not sure exists, but maybe I'm just overlooking the solution as I often do. I'd like to use includes in my PHP to break up my code into smaller bite-sized chunks. I'd like to make the include look for a part of the filename and not the whole name.

So for example. I have 1 file and 3 sub-files.

  • jobs.php
    • jobs_view_simple.php
    • jobs_view_detail.php
    • jobs_view_print.php

In the jobs.php file I have 3 includes for the various needs enclosed in some logic.

include 'partials/jobs_view_simple.php';
include 'partials/jobs_view_detail.php';
include 'partials/jobs_view_print.php';

Is there a way in PHP that I can point to the subfile by referencing the last word/s. Similarly to how wordpress has get_template_part. I think this would be helpful for me in this project, but I'm not sure how this is possible. Maybe this is a function I will need to write that doesn't already exist.

Thoughts?


Solution

  • Should be able to use glob() so you should simply be able to do this:

    foreach (glob("partials/jobs*.php") as $filename) {
        include $filename;
    }
    

    http://php.net/manual/en/function.glob.php