Each template has a search rule defining what files it applies to. The program checks each template until it finds one with a rule the matches the file. That template is used to display the file's data.
If you want to figure out what the program is doing (and why it's not doing what you expect), do the same thing with each file. That is, look at it's tags, and check each template until you find the first one with a search rule that matches it.