def inject_widget_variables(layout, structure, variables=None):
    """ Takes the layout, structure and a dict of variables meant for the
    template engine and injects the variables required by the widgets, if
    the widgets are indeed in use.

    """
    if not structure:
        return variables
    variables = variables or {}
    for tag, widget in layout.app.config.homepage_widget_registry.items():
        if '<{}'.format(tag) in structure:
            if hasattr(widget, 'get_variables'):
                for key, value in widget.get_variables(layout).items():
                    assert key not in variables, 'use unique variable names'
                    variables[key] = value
    return variables
