import re


def getTemplateTags(template):
    """Find all substrings containing only letters, digits, underscore
    and dot which are surrounded by '{{ ... }}' or '{% ... %}'.
    Each item must start with a letter or underscore. More than one such
    substring may occur in each block.
    Return a <set>.
    """
    _match = r'([a-zA-Z_][a-zA-Z0-9_.]*)'
    with open(template.filename, 'r', encoding='utf-8') as fh:
        text = fh.read()
    tags = set()
    for item in re.findall(r'\{\{(.*?)\}\}', text):
        tags.update(re.findall(_match, item))
    for item in re.findall(r'\{\%(.*?)\%\}', text):
        tags.update(re.findall(_match, item))
    return tags