How to add custom CSS class depending on the template

I know that you can achieve the same by using global variables, but this is also one of the ways to add custom class depending on which page template has been loaded (you would add this into functions.php):

While inside page template, you would use something like:

The final result would be:

or something like:

I’ve also posted this as an answer on Stackexchange, so if you find this useful, do not hesitate to vote me up :).