This question is quite difficult to understand.
Do you mean you want to make generic template pages Programmatically? There are a few ways to do this.
If you have a page built on the backend, you can create a file within your theme folder called page-{page-name}.php. For instance, for a page called about, you should create a file called page-about.php.
If it is a post, the same logic applies, but with single-{post-name}.php.
Alternatively, if you want a template page for several pages under one file, you could use a conditional statement along with the is_page() function inside page.php (or single.php for posts).
You can also use get_post_field with a conditional, as such:
if ('contact-us' == get_post_field( 'post_name', $post->post_parent )) {
get_template_part( 'template-parts/contact', 'page' );
exit;
} else if ('volunteer-with-us' == get_post_field( 'post_name')) {
get_template_part( 'template-parts/contact', 'page' );
exit;
}