How to get list of all variables in jinja 2 templates

后端 未结 4 709
南笙
南笙 2020-12-02 16:58

I am trying to get list of all variables and blocks in a template. I don\'t want to create my own parser to find variables. I tried using following snippet.

         


        
4条回答
  •  囚心锁ツ
    2020-12-02 17:51

    For my pelican theme, i have created a tools for analyse all jinja variables in my templates files.

    I share my code

    This script generate a sample configuration from all variables exists in template files and get a variables from my official pelicanconf.py

    The function that extract all variables from template file

    def get_variables(filename):
        env = Environment(loader=FileSystemLoader('templates'))
        template_source = env.loader.get_source(env, filename)[0]
        parsed_content = env.parse(template_source)
    

    The complete script

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #
    # use:
    # generate_pelicanconf-sample.py my_official_blog/pelicanconf.py
    
    import sys
    import imp
    import os
    
    from jinja2 import Environment, FileSystemLoader, meta
    
    
    # Search all template files
    def list_html_templates():
        dirList = os.listdir('templates')
    
        return dirList
    
    
    # get all variable in template file
    def get_variables(filename):
        env = Environment(loader=FileSystemLoader('templates'))
        template_source = env.loader.get_source(env, filename)[0]
        parsed_content = env.parse(template_source)
    
        return meta.find_undeclared_variables(parsed_content)
    
    
    # Check if the pelicanconf.py is in param
    if len(sys.argv) != 2:
        print("Please indicate the pelicanconf.py file")
        sys.exit()
    
    # Get all vars from templates files
    all_vars = set()
    files = list_html_templates()
    for fname in files:
        variables = get_variables(fname)
        for var in variables:
            if var.isupper():
                all_vars.add(var)
    
    m = imp.load_source('pelicanconf', sys.argv[1])
    
    # Show pelicanconf.py vars content
    for var in all_vars:
        varname = 'm.%s' % var
        if var in m.__dict__:
            print ("%s = %s" % (var, repr(m.__dict__[var])))
    
    
        return meta.find_undeclared_variables(parsed_content)
    

    The sample result of this program

    LINKS = ((u'Home', u'/'), (u'archives', u'/archives.html'), (u'tags', u'/tags.html'), (u'A propos', u'http://bruno.adele.im'))
    SITESUBTITLE = u'Une famille compl\xe8tement 633<'
    DEFAULT_LANG = u'fr'
    SITEURL = u'http://blog.jesuislibre.org'
    AUTHOR = u'Bruno Adel\xe9'
    SITENAME = u'Famille de geeks'
    SOCIAL = ((u'adele', u'http://adele.im'), (u'feed', u'http://feeds.feedburner.com/FamilleDeGeek'), (u'twitter', u'http://twitter.com/jesuislibre.org'), (u'google+', u'https://plus.google.com/100723270029692582967'), (u'blog', u'http://blog.jesuislibre.org'), (u'facebook', u'http://www.facebook.com/bruno.adele'), (u'flickr', u'http://www.flickr.com/photos/b_adele'), (u'linkedin', u'http://fr.linkedin.com/in/brunoadele'))
    FEED_DOMAIN = u'http://blog.jesuislibre.org'
    FEED_ALL_ATOM = u'feed.atom'
    DISQUS_SITENAME = u'blogdejesuislibreorg'
    DEFAULT_PAGINATION = 10
    GITHUB_BLOG_SITE = u'https://github.com/badele/blog.jesuislibre.org'
    

    For more détail of this script see https://github.com/badele/pelican-theme-jesuislibre

提交回复
热议问题