I imagine you are referencing your thread about [how to keep track of variables](http://www.sitepoint.com/forums/showthread.php?876664-Tools-thoughts-to-help-with-tracking-variables .), replies there dealt with many things, including (from my side) naming conventions for variables, tables and such.
Yesterday you posted some code and I replied with another idea on the ideal proximity of variable declaration and use.
You mention spaghetti code, and yes, although quite well defined on wikipedia in PHP circles it takes on a slightly different guise -- from what I have understood it to mean anyhow and I think we have to be careful here.
Chris Coetzee starts off his blog post How to avoid spaghetti mess code with the following statement before suggesting the tools you should be using to avoid this (frameworks etc).
I like that definition and to me, avoiding spaghetti code does not mean simply "use a template".
However the title of this post is "Separating HTML from PHP", and yes certainly using a template is the most obvious way to achieve that.
I hold that it is possible to have SQL, PHP and HTML all in one file and it be easy to follow and be very obvious in its intent and implementation and it not to be spaghetti code.
It is possible to write spaghetti code as Chris Cotzee defines it even when using templates, and now what is more you will have to deal with the extra complexity of having code over multiple files, probably in different folders.
I am not saying templates are either good or bad but that their use might depend on your situation and does add some complexity. Done wrongly that will create spaghetti code so that is something you have to plan carefully for.
There are other simple disciplines which can make your code easier to follow too and most of what I spout about naming conventions and code proximity etc I learned from Clean Code by (Uncle) Bob Martin (kindly given to me by another SP guru btw).
I nod when I read @oddz; comment because it reminds me of my own reaction when I first took time out to discover MVC, Frameworks and n tier layers etc, that when I got back to my own (spaghetti) code I naturally started separating SQL and HTML from my PHP (spaghetti code in even more files ... )
Its not as easy to say do not mix 3 languages in the same file, but what you can do is start to split the files containing mostly database access code (or xml access code if you will) from what ought to probably be in a template.
Prior to adopting a known framework these two tricks might help you start to start thinking about "separating concerns":
Write SQL statement like this:
$qry = "SELECT this
WHERE my_condition = 1
LIMIT 0, 30";
// rest of your stuff
So that your SQL really jumps out at you (plus the lines of select columns can be copied/pasted, easily added/removed, easily counted etc).
This will make you super-conscious of how much SQL you have dotted around your code, and easier to locate and refactor out later.
Write your html output like this at the end of the file if you can, using the heredoc syntax:
$str = <<<EOD
Example of string with a $widget->name
spanning multiple lines
using heredoc syntax.
Then you can likewise, easily pull that out into a template when all tee'd up.
There does not have to be a "big bang" where spaghetti code is gone, and you have fully separated concerns - but you can gently and resolutely pry the pieces apart over time.