Skip to content

Commit

Permalink
FSE: Parse the template before <head> gets rendered (#28319)
Browse files Browse the repository at this point in the history
* Parse the template before head gets rendered

* add phpcs-ignore
  • Loading branch information
aristath authored Jan 20, 2021
1 parent f2d3c09 commit 8ff0c0a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
18 changes: 11 additions & 7 deletions lib/full-site-editing/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,14 @@ function gutenberg_render_title_tag() {
}

/**
* Renders the markup for the current template.
* Returns the markup for the current template.
*/
function gutenberg_render_the_template() {
function gutenberg_get_the_template_html() {
global $_wp_current_template_content;
global $wp_embed;

if ( ! $_wp_current_template_content ) {
echo '<h1>' . esc_html__( 'No matching template found', 'gutenberg' ) . '</h1>';
return;
return '<h1>' . esc_html__( 'No matching template found', 'gutenberg' ) . '</h1>';
}

$content = $wp_embed->run_shortcode( $_wp_current_template_content );
Expand All @@ -178,9 +177,14 @@ function gutenberg_render_the_template() {

// Wrap block template in .wp-site-blocks to allow for specific descendant styles
// (e.g. `.wp-site-blocks > *`).
echo '<div class="wp-site-blocks">';
echo $content; // phpcs:ignore WordPress.Security.EscapeOutput
echo '</div>';
return '<div class="wp-site-blocks">' . $content . '</div>';
}

/**
* Renders the markup for the current template.
*/
function gutenberg_render_the_template() {
echo gutenberg_get_the_template_html(); // phpcs:ignore WordPress.Security.EscapeOutput
}

/**
Expand Down
7 changes: 6 additions & 1 deletion lib/template-canvas.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* @package gutenberg
*/

/**
* Get the template HTML.
* This needs to run before <head> so that blocks can add scripts and styles in wp_head().
*/
$template_html = gutenberg_get_the_template_html();
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
Expand All @@ -16,7 +21,7 @@
<body <?php body_class(); ?>>
<?php wp_body_open(); ?>

<?php gutenberg_render_the_template(); ?>
<?php echo $template_html; // phpcs:ignore WordPress.Security.EscapeOutput ?>

<?php wp_footer(); ?>
</body>
Expand Down

0 comments on commit 8ff0c0a

Please sign in to comment.