0

I am building a page with PHP and am making heavy use of templating to split the various parts of each page up into reusable components, each of which has its own stylesheet which is automatically added to the head by script.

However, to save bandwidth and avoid style conflicts in certain situations, I would like to only link those stylesheets which are actually used on a given page. Since the head goes first in the page and the styles go in the head, but there's no way to access something like get_included_files() until the end of the page, I'd (presumably) need a way to reach back into the head and add the link tags later, but I'm not sure how to do that.

The only obvious way I can think of is to necessitate a registry array of all the components which will be used, and then use that to generate the link tags, but that is not really viable because some of the components include other components, so it would be necessary to manually register not just the components included in the top-level file, but all the components included by those components as well.

1 Answers1

0

I figured out a solution. I took each of my components, and basically wrapped them in a class and a public static function of that class, called output(). I can now include() all the components I want to use right at the start, and then just do Component::output() wherever I want it to appear. This way, all the files are included, and visible to get_included_files(), by the time I call output() on the script that generates the head.