Parse a template, using the given parameters, and set it as the feedback message.

        tpl_name must indicate a file. Given that XX_XX is the lang code of the current user ('en_US' or 'fr_FR', for example),
        this function will search template file in different locations, in the following order:
        - [current_dir]/tpl_name.XX_XX.tpl
        - [task_dir]/lang/XX_XX/tpl_name.tpl (this is the preferred way, as it contributes to store all translations in the same folder)
        - [current_dir]/tpl_name.tpl

        Note that you can indicate "../otherdir/mytpl" to force the function to search in the "../otherdir" directory. Simply omit the final ".tpl".

        If no file is found or a parsing exception occured, an error is displayed as feedback message, and False is returned.
        If everything went well, True is returned.

        The parsing uses Jinja2.

        Parameters is a dictionnary that will be given to the Jinja template.

def set_feedback_from_tpl(tpl_name, parameters, problem_id=None, append=False):
    """ Parse a template, using the given parameters, and set it as the feedback message.

        tpl_name must indicate a file. Given that XX_XX is the lang code of the current user ('en_US' or 'fr_FR', for example),
        this function will search template file in different locations, in the following order:
        - [current_dir]/tpl_name.XX_XX.tpl
        - [task_dir]/lang/XX_XX/tpl_name.tpl (this is the preferred way, as it contributes to store all translations in the same folder)
        - [current_dir]/tpl_name.tpl

        Note that you can indicate "../otherdir/mytpl" to force the function to search in the "../otherdir" directory. Simply omit the final ".tpl".

        If no file is found or a parsing exception occured, an error is displayed as feedback message, and False is returned.
        If everything went well, True is returned.

        The parsing uses Jinja2.

        Parameters is a dictionnary that will be given to the Jinja template.
    """
    inginious.lang.init()
    lang = get_lang()

    tpl_location = None
    possible_locations = [".".join([tpl_name, lang, "tpl"]),
                          os.path.join(inginious.lang.get_lang_dir_path(), lang, tpl_name) + ".tpl",
                          ".".join([tpl_name, "tpl"])]
    for path in possible_locations:
        if os.path.exists(path):
            tpl_location = path
            break

    if tpl_location is None:
        output = """
.. error::

    Unable to find template named %s. Please contact your administrator.
    

    """ % tpl_name

        if problem_id is None:
            set_global_feedback(output, append)
        else:
            set_problem_feedback(output, problem_id, append)
        return False

    try:
        template = Template(open(tpl_location, 'r').read())
        parameters.update({"_": _})
        output = template.render(parameters)
        valid = True
    except Exception:
        output = """
.. error::
   
   An error occured while parsing the feedback template. Here is the full error:
   
   ::
   
"""
        output += "\n".join(["\t\t"+line for line in traceback.format_exc().split("\n")])
        output += "\n\tPlease contact your administrator.\n"
        valid = False

    if problem_id is None:
        set_global_feedback(output, append)
    else:
        set_problem_feedback(output, problem_id, append)

    return valid