Add parent template name to body class filter when visiting subpage or single post

here:

add_filter('body_class','body_class_slugs');
function body_class_slugs($classes) {
    global $posts,$post;

    if(is_single() || is_page()){ //only on a single post or page
        if (isset($posts)){
            $classes[] = $post[0]->post_name; //posts is an array of posts so we use the first one by calling [0]
        }
        elseif (isset($post)){
            $classes[] = $post->post_name;
        }
    }
    return $classes;
}