Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

develop templates command is broken #172

Open
rsyring opened this issue Aug 16, 2021 · 1 comment
Open

develop templates command is broken #172

rsyring opened this issue Aug 16, 2021 · 1 comment

Comments

@rsyring
Copy link
Member

rsyring commented Aug 16, 2021

We use Jinja private methods and they have changed their signature. My goal with this command is to get a sense of where templates are being loaded from. I was able to hack together this:

@dev_command.command('templates', short_help=_('Show paths searched for a template.'))
@flask.cli.with_appcontext
def templates_command():
    app = flask.current_app
    have_loaders = [app] + [bp for bp in app.iter_blueprints() if bp.jinja_loader]
    result = defaultdict(list)

    for app_or_bp in have_loaders:
        jinja_loader = app_or_bp.jinja_loader
        if hasattr(jinja_loader, 'loaders'):
            # ChoiceLoader has a list of loaders
            loaders = jinja_loader.loaders
        else:
            loaders = [jinja_loader]

        for loader in loaders:
            # PackageLoader
            if hasattr(loader, '_template_root'):
                result[app_or_bp.name].append(loader._template_root)
            else:
                # FileSystemLoader
                result[app_or_bp.name].append(loader.searchpath)

    for source_name in sorted(result.keys()):
        #search_paths = sorted(result[source_name])
        click.echo(source_name)
        for root_path in result[source_name]:
            click.echo('    {}'.format(root_path))

I think the old command would have told you every template and where it was being loaded from. That's helpful, but when you have a blueprint (or something) with a loader mis-configured, it won't actually find the template and so it won't be listed with the old command. The above command actually lists the loaders and where they are sourcing templates from, which helps identify mis-configured loaders, which is what I think I really care about.

@rsyring
Copy link
Member Author

rsyring commented Aug 27, 2021

Maybe we don't need this anymore. Just turn on: EXPLAIN_TEMPLATE_LOADING in Flask?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant