1//NEW CLEAN way (WP 5.5.0 >=) of passing variables to template parts
2
3$data = array('foo'=>'bar');
4
5//add your variables as a third parameter
6get_template_part( 'template-parts/file', 'name', $data );
7
8//In the template part itself: (IMPORTANT NOTE: the name of the variable has to be args)
9echo $args['foo'];