我想这可能会告诉您,您的视图需要一个像样的 PHP 模板系统(Smarty?)。
但是,如果您正在寻找快速解决方案,请尝试利用Output Buffering
您的需求:
<?php
// at the top of your parent page
function move_css_to_head($buffer)
{
// do some DOM manipulation here to achieve your goal
$dom = new DOMDocument;
$dom->loadHTML($buffer);
$head = $dom->getElementsByTagName('head')->item(0);
$body = $dom->getElementsByTagName('body')->item(0);
$css_elms = $body->getElementsByTagName('link');
foreach ($css_elms as $elm)
{
$head->appendChild($elm->cloneNode());
$body->removeChild($elm);
}
return $dom->saveHTML();
}
ob_start('move_css_to_head'); // buffer response output, and call the assigned function upon flushing
?>
<!-- Regular Page Content -->
<?php
// at the bottom of your parent page, flush the buffer
ob_end_flush();
?>