How does get_template_part() work inside a loop?

Posted: - Source : stackoverflow

Could someone please explain to me how this function works inside a loop?

I've used this functions many times inside a wordpress loop like this:

loop start

  get_template_part("template/part", "example");

loop end

And the magic about this is that I can call functions like the_title() without any problem in template part-example.php.

But if I try to pass custom variables to part-example.php like this, it will fail:

$var = "variable";
Start loop

  get_template_part("template/part", "example");

End loop

And inside part-exmaple.php file

echo $var; // this will fail

I do find a workaround to solve this problem by reading great this article, it's using include( locate_template() ).

But I just wonder, how the post data get passed through by get_template_part()?

Thanks in advance.