Render_view function - useful to anybody else?

I use this function as an alternative to get_template_part(). Its main benefit is that you can inject variables into it. It also uses me a ‘view’ folder in my template which makes it easy to create reusable chunks of code that can even change dynamically based on a variable injected. Another benefit I’ve found is that I don’t need to re-call functions when I need the same info (like post title, or post id) inside multiple views. Anyway, I thought I’d share with the community, let me know what you think!

<?php
namespace Roots\Sage\Render;

function render_view( $default_template_path = false, $variables = array(), $require = 'always' ) {
	$template_path = get_template_directory() . '/views/' . $default_template_path;
	$template_path = apply_filters( 'template_path', $template_path );
	if ( is_file( $template_path ) ) {
		extract( $variables );

		ob_start();
		if ( $require == 'always' ) {
			require( $template_path );
		} else {
			require_once( $template_path );
		}
		$template_content = apply_filters( 'template_content', ob_get_clean(), $default_template_path, $template_path, $variables );
	} else {
		$template_content = '';
	}
	do_action( 'render_template_post', $default_template_path, $variables, $template_path, $template_content );
	return $template_content;
}

Example usage:

<?php echo Roots\Sage\Render\render_view("account/subscription_info.php", array("subscription" => $subscription)); ?>
6 Likes