How to pass a list from Python, by Jinja2 to JavaScript

后端 未结 6 1682
挽巷
挽巷 2020-11-27 10:21

Let\'s say I have a Python variable:

list_of_items = [\'1\',\'2\',\'3\',\'4\',\'5\']

and I pass it to Jinja by rendering HTML, and I also h

6条回答
  •  孤街浪徒
    2020-11-27 10:47

    I can suggest you a javascript oriented approach which makes it easy to work with javascript files in your project.

    Create a javascript section in your jinja template file and place all variables you want to use in your javascript files in a window object:

    Start.html

    ...
    {% block scripts %}
    
    
    {% endblock %}
    

    Jinja will replace values and our appConfig object will be reachable from our other script files:

    App.js

    var AccountKit_OnInteractive = function(){
        AccountKit.init({
            appId: appConfig.facebook_app_id,
            debug: appConfig.debug,
            state: appConfig.csrf_token,
            version: appConfig.accountkit_api_version
        })
    }
    

    I have seperated javascript code from html documents with this way which is easier to manage and seo friendly.

提交回复
热议问题