Actually I think the best way to solve this problem is through template inheritance.
Template inheritance allows you to extend templates and replace parts of the parent's content. You can see a very good example in Twig's documentation.
It is not necessary though to use twig or any template engine, you can implement similar behavior in plain php templates (although not as good looking). For example one could use anonymous functions to achieve similar block based inheritance.
--- Template base.php ----
<?php $main = function($head,$body) { ?>
<html>
<head>
<?php $head() ?>
</head>
<body>
<?php $body() ?>
</body>
</html>
<?php } ?>
<?php $head = function () { ?>
<style> ... </style>
<?php } ?>
<?php $body = function () { ?>
... body ...
<?php } ?>
--- Template derived.php ---
<?php include ('base.php'); ?>
<?php $head = function () use($head) { ?>
<?php $head(); ?>
<script> ... </script>
<?php } ?>
<?php $main($head,$body); ?>