Automattic\WooCommerce\Blocks\BlockTypes

ClassicTemplate::render()protectedWC 1.0

Render method for the Classic Template block. This method will determine which template to render.

Method of the class: ClassicTemplate{}

No Hooks.

Return

String. | void Rendered block type output.

Usage

// protected - for code of main (parent) or child class
$result = $this->render( $attributes, $content, $block );
$attributes(array) (required)
Block attributes.
$content(string) (required)
Block content.
$block(WP_Block) (required)
Block instance.

ClassicTemplate::render() code WC 9.4.2

protected function render( $attributes, $content, $block ) {
	if ( ! isset( $attributes['template'] ) ) {
		return;
	}

	/**
	 * We need to load the scripts here because when using block templates wp_head() gets run after the block
	 * template. As a result we are trying to enqueue required scripts before we have even registered them.
	 *
	 * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/5328#issuecomment-989013447
	 */
	if ( class_exists( 'WC_Frontend_Scripts' ) ) {
		$frontend_scripts = new WC_Frontend_Scripts();
		$frontend_scripts::load_scripts();
	}

	if ( OrderConfirmationTemplate::SLUG === $attributes['template'] ) {
		return $this->render_order_received();
	}

	if ( is_product() ) {
		return $this->render_single_product();
	}

	$valid             = false;
	$archive_templates = array(
		ProductCatalogTemplate::SLUG,
		ProductCategoryTemplate::SLUG,
		ProductTagTemplate::SLUG,
		ProductAttributeTemplate::SLUG,
		ProductSearchResultsTemplate::SLUG,
	);

	// Set selected template when we directly find template base slug.
	if ( in_array( $attributes['template'], $archive_templates, true ) ) {
		$valid = true;
	}

	// Set selected template when we find template base slug as prefix for a specific term.
	foreach ( $archive_templates as $template ) {
		if ( 0 === strpos( $attributes['template'], $template ) ) {
			$valid = true;
		}
	}

	if ( $valid ) {
		// Set this so that our product filters can detect if it's a PHP template.
		$this->asset_data_registry->add( 'isRenderingPhpTemplate', true );

		// Set this so filter blocks being used as widgets know when to render.
		$this->asset_data_registry->add( 'hasFilterableProducts', true );

		$this->asset_data_registry->add(
			'pageUrl',
			html_entity_decode( get_pagenum_link() )
		);

		return $this->render_archive_product();
	}

	ob_start();

	echo "You're using the ClassicTemplate block";

	wp_reset_postdata();

	return ob_get_clean();
}