Skip to content

Commit

Permalink
Template Loader: Allow PHP in block templates and parts
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed Sep 14, 2020
1 parent b506d04 commit 465a38c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
12 changes: 8 additions & 4 deletions lib/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ function create_auto_draft_for_template_part_block( $block ) {
}

if ( $template_part_file_path ) {
$file_contents = file_get_contents( $template_part_file_path );
ob_start();
include $template_part_file_path;
$file_contents = ob_get_clean();
if ( $template_part_post && $template_part_post->post_content === $file_contents ) {
$template_part_id = $template_part_post->ID;
} else {
Expand Down Expand Up @@ -214,7 +216,7 @@ function create_auto_draft_for_template_part_block( $block ) {
* @param string[] $template_hierarchy (optional) The current template hierarchy, ordered by priority.
* @return null|array {
* @type WP_Post|null template_post A template post object, or null if none could be found.
* @type int[] A list of template parts IDs for the template.
* @type int[] template_part_ids A list of template parts IDs for the template.
* }
*/
function gutenberg_find_template_post_and_parts( $template_type, $template_hierarchy = array() ) {
Expand Down Expand Up @@ -283,8 +285,10 @@ function gutenberg_find_template_post_and_parts( $template_type, $template_hiera

// If there is, use it instead.
if ( isset( $higher_priority_block_template_path ) ) {
$post_name = basename( $higher_priority_block_template_path, '.html' );
$file_contents = file_get_contents( $higher_priority_block_template_path );
$post_name = basename( $higher_priority_block_template_path, '.html' );
ob_start();
include $higher_priority_block_template_path;
$file_contents = ob_get_clean();
$current_template_post = array(
'post_content' => $file_contents,
'post_title' => $post_name,
Expand Down
4 changes: 3 additions & 1 deletion lib/template-parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ function get_template_part_paths( $base_directory ) {
}
// Build and save each template part.
foreach ( $template_part_files as $template_part_file ) {
$content = file_get_contents( $template_part_file );
ob_start();
include $template_part_file;
$content = ob_get_clean();
// Infer slug from filepath.
$slug = substr(
$template_part_file,
Expand Down
4 changes: 3 additions & 1 deletion packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ function render_block_core_template_part( $attributes ) {
// render the corresponding file content.
$template_part_file_path = get_stylesheet_directory() . '/block-template-parts/' . $attributes['slug'] . '.html';
if ( 0 === validate_file( $template_part_file_path ) && file_exists( $template_part_file_path ) ) {
$content = file_get_contents( $template_part_file_path );
ob_start();
include $template_part_file_path;
$content = ob_get_clean();
}
}
}
Expand Down

0 comments on commit 465a38c

Please sign in to comment.