Tribe__Template::template( string|array $name, array $context = array(), boolean $echo = true )

A very simple method to include a Template, allowing filtering and additions using hooks.


Parameters

$name

(string|array) (Required) Which file we are talking about including. If an array, each item will add a directory separator to get to the single template.

$context

(array) (Optional) Any context data you need to expose to this file

Default value: array()

$echo

(boolean) (Optional) If we should also print the Template

Default value: true


Top ↑

Return

(string|false) Either the final content HTML or false if no template could be found.


Top ↑

Source

File: src/Tribe/Template.php

	public function template( $name, $context = array(), $echo = true ) {
		// If name is String make it an Array
		if ( is_string( $name ) ) {
			$name = (array) explode( '/', $name );
		}

		// Clean this Variable
		$name = array_map( 'sanitize_title_with_dashes', $name );

		if ( ! empty( $this->origin->template_namespace ) ) {
			$namespace = array_merge( (array) $this->origin->template_namespace, $name );
		} else {
			$namespace = $name;
		}

		// Setup the Hook name
		$hook_name = implode( '/', $namespace );

		// Check if the file exists
		$file = $this->get_template_file( $name );

		// Check if it's a valid variable
		if ( ! $file ) {
			return false;
		}

		// Before we load the file we check if it exists
		if ( ! file_exists( $file ) ) {
			return false;
		}

		ob_start();

		// Merges the local data passed to template to the global scope
		$this->merge_context( $context, $file, $name );

		/**
		 * Fires an Action before including the template file
		 *
		 * @since  4.6.2
		 * @since  4.7.20   The $name param no longers contains the extension
		 *
		 * @param string $file      Complete path to include the PHP File
		 * @param array  $name      Template name
		 * @param self   $template  Current instance of the Tribe__Template
		 */
		do_action( 'tribe_template_before_include', $file, $name, $this );

		/**
		 * Fires an Action for a given template name before including the template file
		 *
		 * E.g.:
		 *    `tribe_template_before_include:events/blocks/parts/details`
		 *    `tribe_template_before_include:events/embed`
		 *    `tribe_template_before_include:tickets/login-to-purchase`
		 *
		 * @since  4.7.20
		 *
		 * @param string $file      Complete path to include the PHP File
		 * @param array  $name      Template name
		 * @param self   $template  Current instance of the Tribe__Template
		 */
		do_action( "tribe_template_before_include:$hook_name", $file, $name, $this );

		// Only do this if really needed (by default it wont)
		if ( true === $this->template_context_extract && ! empty( $this->context ) ) {
			// We don't allow Extrating of a variable called $name
			if ( isset( $this->context['name'] ) ) {
				unset( $this->context['name'] );
			}

			// We don't allow Extrating of a variable called $file
			if ( isset( $this->context['file'] ) ) {
				unset( $this->context['file'] );
			}

			// Make any provided variables available in the template variable scope
			extract( $this->context ); // @codingStandardsIgnoreLine
		}

		include $file;

		/**
		 * Fires an Action after including the template file
		 *
		 * @since  4.6.2
		 * @since  4.7.20   The $name param no longers contains the extension
		 *
		 * @param string $file      Complete path to include the PHP File
		 * @param array  $name      Template name
		 * @param self   $template  Current instance of the Tribe__Template
		 */
		do_action( 'tribe_template_after_include', $file, $name, $this );

		/**
		 * Fires an Action for a given template name after including the template file
		 *
		 * E.g.:
		 *    `tribe_template_after_include:events/blocks/parts/details`
		 *    `tribe_template_after_include:events/embed`
		 *    `tribe_template_after_include:tickets/login-to-purchase`
		 *
		 * @since  4.7.20
		 *
		 * @param string $file      Complete path to include the PHP File
		 * @param array  $name      Template name
		 * @param self   $template  Current instance of the Tribe__Template
		 */
		do_action( "tribe_template_after_include:$hook_name", $file, $name, $this );

		// Only fetch the contents after the action
		$html = ob_get_clean();

		/**
		 * Allow users to filter the final HTML
		 *
		 * @since  4.6.2
		 * @since  4.7.20   The $name param no longers contains the extension
		 *
		 * @param string $html      The final HTML
		 * @param string $file      Complete path to include the PHP File
		 * @param array  $name      Template name
		 * @param self   $template  Current instance of the Tribe__Template
		 */
		$html = apply_filters( 'tribe_template_html', $html, $file, $name, $this );

		/**
		 * Allow users to filter the final HTML by the name
		 *
		 * E.g.:
		 *    `tribe_template_html:events/blocks/parts/details`
		 *    `tribe_template_html:events/embed`
		 *    `tribe_template_html:tickets/login-to-purchase`
		 *
		 * @since  4.7.20
		 *
		 * @param string $html      The final HTML
		 * @param string $file      Complete path to include the PHP File
		 * @param array  $name      Template name
		 * @param self   $template  Current instance of the Tribe__Template
		 */
		$html = apply_filters( "tribe_template_html:$hook_name", $html, $file, $name, $this );

		if ( $echo ) {
			echo $html;
		}

		return $html;
	}

Top ↑

Changelog

Changelog
Version Description
4.6.2 Introduced.