Wordpress $var = get_template_part('content', 'new-post');

Hi Guys,

I want to call dynamic template file in functions.php also pass some variables to that file ,
Then save the resultant output in a variable so that I can return it in json encoded form , Please check my this code -

$my_post_id = wp_insert_post( $my_post );

		set_query_var( 'my_post_id', $my_post_id );
		set_query_var( 'my_post_title', $_POST['post_title']);
		$htmlPart = get_template_part( 'content', 'new-post' );
		 // $htmlPart = file_get_contents(locate_template("content-new-post.php")); 

			echo json_encode(array('result'=>'success','msg'=>'Data saved successfully','my_post_id'=>$my_post_id,'my_post_title'=>$_POST['post_title'],'html'=>$htmlPart));