Why is it useful to have a templating function in website programming?

We often come across the need to format data into a website that involves expressing it in HTML.

We could just express it directly in code but can get harder to understand if we mix the data together with the markup that we need.

So templates are a way of simplifying this by separating concerns. The template can be expressed in a single place separate from the data used to populate it.

For example see this templating function.