<?php
/**
 * Template loader helper trait.
 *
 * @since   1.0.0
 * @package DonationRouter
 */

declare( strict_types=1 );
namespace DonationRouter;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Provides a single render_template() helper that loads a PHP template from
 * the plugin's templates/ directory with all supplied variables in scope.
 *
 * Usage:
 *   use Template_Loader;
 *   $this->render_template( 'admin/account-form.php', compact( 'heading', 'account' ) );
 *
 * @since 1.0.0
 */
trait Template_Loader {

	/**
	 * Include a template file with the given variables extracted into scope.
	 *
	 * Variables are extracted with EXTR_SKIP so they cannot shadow reserved
	 * names.  The template path is relative to the plugin templates/ root.
	 *
	 * @since 1.0.0
	 *
	 * @param string               $path  Template path relative to templates/.
	 * @param array<string, mixed> $vars  Variables to expose in the template.
	 *
	 * @return void
	 */
	private function render_template( string $path, array $vars = array() ): void {
		// phpcs:ignore WordPress.PHP.DontExtract.extract_extract -- controlled internal use; keys come from plugin code, not user input
		extract( $vars, EXTR_SKIP );
		include ADOR_PATH . 'templates/' . $path;
	}
}
