Fixes 4287: Correctly skip template associations when looking for matches

Separate `if` clauses was causing the system to look for incorrect matches (i.e a template which has both an environment and hostgroup association for hostgroup only)