need to package jinja2 template for python

后端 未结 3 1053
北海茫月
北海茫月 2021-01-21 23:37

(UPDATE: I\'ve made a better question with a better answer here. I was going to delete this question, but some of the answers might prove useful to future searchers.)

M

3条回答
  •  日久生厌
    2021-01-22 00:02

    I've discovered a WORKAROUND, here. In this usage, the template is not part of a module or package; it is loaded directly from the file system. File system:

    ./index.py
    ./template.html
    

    index.py:

    #!/usr/bin/python
    import jinja2
    
    templateLoader = jinja2.FileSystemLoader( searchpath="." )
    templateEnv = jinja2.Environment( loader=templateLoader )
    TEMPLATE_FILE = "template.html"
    template = templateEnv.get_template( TEMPLATE_FILE )
    outputText = template.render( ) # this is where to put args to the template renderer
    
    print ("Content-type: text/html\r\n\r\n")
    print(outputText)
    

提交回复
热议问题