Help defining a chained one-to-many relationship tables
Since Template must be related to a Region, you need a foreign key from Template.region_id to Region.id

Template.region_id will be NOT NULLable.

Template.country_id will be NULLable.

Make a unique constraint on the composite key (Country.id, Country.region_id)

Then you can create a foreign key from (Template.country_id , Template.region_id) to (Country.id, Country.region_id)

This ensures that if the Template is related to a Country, then the Template.region_id is the same as the related Country.region_id

Luckily (for you) when a row's composite foreign key contains one or more null values, then the row is "valid" regardless of the existence of rows in the referenced table.  So having a NULL Template.country_id and a NOT NULL Template.region_id will be fine.

One interesting side effect of this "doubly constrained" system is that it is difficult to update the region_id column of a Country.  Any attempt to update Country.region_id will cause a constraint violation if there are any Templates that contain references to that Country, since the (Template.country_id , Template.region_id) pair will not refer to an existing (Country.id, Country.region_id).

If you must allow this type of update, then you can use an ON UPDATE CASCADE clause when creating the (Template.country_id , Template.region_id) foreign key.