acf_block_render_template()ACF 6.0.4

Locate and include an ACF block's template.

Returns

null. Nothing (null).

Usage

acf_block_render_template( $block, $content, $is_preview, $post_id, $wp_block, $context );
$block(array) (required)
The block props.
$content(required)
.
$is_preview(required)
.
$post_id(required)
.
$wp_block(required)
.
$context(required)
.

Changelog

Since 6.0.4 Introduced.

acf_block_render_template() code ACF 6.8.8

function acf_block_render_template( $block, $content, $is_preview, $post_id, $wp_block, $context ) {
	// Always resolve the block path from the registered block type rather than the
	// passed block, so a path in the saved block attributes can never be included.
	$block_type      = acf_get_block_type( $block['name'] ?? '' );
	$registered_path = $block_type['path'] ?? '';
	$render_template = $block['render_template'] ?? '';

	// Locate template. Templates are always included from the local filesystem, so
	// reject stream wrappers such as `phar://` or `php://` before touching the disk.
	if ( acf_path_has_stream_wrapper( $render_template ) ) {
		$path = '';
	} elseif ( $registered_path && file_exists( $registered_path . '/' . $render_template ) ) {
		$path = $registered_path . '/' . $render_template;
	} elseif ( file_exists( $render_template ) ) {
		$path = $render_template;
	} else {
		$path = locate_template( $render_template );
	}

	do_action( 'acf/blocks/pre_block_template_render', $block, $content, $is_preview, $post_id, $wp_block, $context );

	// Include template.
	if ( file_exists( $path ) ) {
		if ( $is_preview && ! empty( $block['auto_inline_editing'] ) && ! empty( $block['acf_block_version'] ) && $block['acf_block_version'] >= 3 ) {
			$result = apply_inline_editing_attributes_to_render_template( $path, $block, $content, $is_preview, $post_id, $wp_block, $context );

			// In order to allow block render templates to support any html tags,
			// we must assume that escaping has already been properly handled by the block render template here.
			// Typically we'd use something like wp_kses here, but that would limit the HTML tags that can be used.
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			echo $result;
		} else {
			include $path;
		}
	} elseif ( $is_preview ) {
		echo acf_esc_html( apply_filters( 'acf/blocks/template_not_found_message', '<p>' . __( 'The render template for this ACF Block was not found', 'acf' ) . '</p>' ) );
	}

	do_action( 'acf/blocks/post_block_template_render', $block, $content, $is_preview, $post_id, $wp_block, $context );
}