I solved it.
Thanks to Macrura's recommendation, I was able to render the page without losing context.
The code is simplified into this:
// home.php, check if parent has children: if true, render partial, full page otherwise
if(! hasChildren)
// render partially
$template_file = $parent->template->filename;
wireIncludeFile($template_file, array('renderPartial' => true));
...
...
// Using renderPartial, which you can access by itself
// as $renderPartial in the template file associated with the page
// contact.php
// Render partial
if (isset($renderPartial) && $renderPartial) {
wireIncludeFile("partials/_contact_form.php");
} else { // render the whole page
...
...
// Append contact form to the end of content
$content .= wireRenderFile("partials/_contact_form.php");
// Call main template
include("_main.php");
}
// Everything renders fine, can access $config->scripts->foot|head just fine.
For reference:
wireRenderFile
wireIncludeFile