IntroductionA very general thought, will add more specifics when I have done a bit more investigation.
Considering the template end of themeing (so ignoring theme functions for this thought experiment) and restricting to a node template or field templates. Assuming you have have a set of various different fields (some with multiple values perhaps) and you wish to customise the display of the content somewhat perhaps wrapping some of the fields in html to make a carousel or for something else. Is there ever a case where you need write complex PHP in the template/s?
Templates can get nastyThemeing Drupal is quite a big learning curve, in some organisations there is a clear division between developer and themer, between front-end and back-end development. Americans seem better at maintaining a clear division (although I don't have first-hand experience of this) whilst us Brits don't like to admit we can't do something so seem more likely to be blitting from server configuration, through back-end development, and onto front-end, admitting that you can't at least sort of do something is not easy.
Add tight deadlines to the mix, freelance Front End developers who don't know Drupal but who are used as a resource and before you know it your template files can be full of funky PHP pulling things out of $node to for display (even though they are available in $content) or even getting a reference to the node via an object reference in a field .....
Online I see questions about how to check if a field is empty in the node temple, with a variety of answers provided but never (or not in my experience) a question about why someone needs to do this.
Is it this simple?Maybe not, in other frameworks typically by the time you get to the template level any variables you are interested will be available directly or via some sort of context. In the template a simple conditional check will allow you to determine what you need to display.
The solution for Drupal surely (please correct me if wrong) is to call
render()on the field. You can check what is returned, if the the field is empty or doesn't exist the output of render will be empty. If not you can print it (wrapping it with any html etc.)
Yet again for a key field you can provide a field template. Putting the extras in that template and in the node template you can simply render the field knowing that if it empty nothing will be rendered.
There are of course theme functions and various places prior to the template where you can reach in and adjust the output, these are not immediately accessible to a front end developer without much Drupal experience and the back-end developer may well be too busy or they could be working remotely to each other.
So templates should be simple, free of complex PHP perhaps there are situations where this is not possible, I am investigating.