簡體   English   中英

HTML 模板使用 Jinja2 - 丟失

[英]HTML templating using Jinja2 - Lost

我正在嘗試使用 Jinja2 在 python 中創建一個 html 模板。我有一個帶有“template.html”的模板文件夾,但我不知道如何處理環境或 package 加載器。

我使用 easy_python 安裝了 Jinja2 並運行了以下腳本。

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('yourapplication', 'templates'))
template = env.get_template('mytemplate.html')
print template.render()

我收到以下錯誤,因為我不知道如何定義包/模塊。 請幫助我,我只想創建一個簡單的模板。

  File "log_manipulationLL.py", line 291, in <module>
env = Environment(loader=PackageLoader('yourapplication', 'templates'))
 File "/usr/local/lib/python2.7/dist-packages/Jinja2-2.6-py2.7.egg/jinja2/loaders.py",    line 216, in __init__
provider = get_provider(package_name)
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 213, in get_provider
__import__(moduleOrReq)
ImportError: No module named yourapplication

如果您不想要或不需要Python包,您可能應該使用FileSystemLoader ,如下所示:

from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
    loader=FileSystemLoader('file/path/'),
    autoescape=select_autoescape(['html', 'xml']),
)

PackageLoader期望使用常規點語法的實際Python模塊。 例如,如果您的結構如下所示:

myapp/
  __init__.py
  …
  templates/
    mytemplate.html

您應該使用myapp作為模塊名稱。

我使用以下代碼解決了這個問題:

 env = Environment(loader=PackageLoader('scriptname', 
                                        templatesPath))

這段代碼放在scriptname.py文件中。

我不確定我的答案是否相關,但我想知道也許有人可能會覺得這個答案很有用。 如果我錯了,請告訴我。

PackageLoader的定義如下:

class PackageLoader(BaseLoader):
    """Load templates from python eggs or packages.  It is constructed with
    the name of the python package and the path to the templates in that
    package::

        loader = PackageLoader('mypackage', 'views')

    If the package path is not given, ``'templates'`` is assumed.

    Per default the template encoding is ``'utf-8'`` which can be changed
    by setting the `encoding` parameter to something else.  Due to the nature
    of eggs it's only possible to reload templates if the package was loaded
    from the file system and not a zip file.
    """

然后__init__()方法如下:

def __init__(self, package_name, package_path='templates',
             encoding='utf-8'):

這讓我們注意到這樣的結構:

myapp/
  __init__.py
  ...
  templates/
    mytemplate.html

將具有與這兩個聲明相同的PackageLoader實例:

PackageLoader('myapp')
PackageLoader('myapp', 'templates')

因此,如果您從myapp/ path運行,那么您只需要說:

PackageLoader('templates', '')

因此它只需要templates/作為路徑。 如果將第二個參數留空,它將嘗試在templates/templates查找templates/templates

最后,您可以使用list_templates()方法檢查已加載的list_templates()

PackageLoader('templates', '').list_templates()

我有多個模板,jinja2 文檔沒有很好的記錄。 您實際上可以使用 jinja2 中的 FileSystemLoader

from jinja2 import Environment, FileSystemLoader

# Declare the environment
templateLoader = FileSystemLoader(searchpath="./templates")
env = Environment(loader=templateLoader)


# Generate the HTML template base on the template name
template = env.get_template(f'{template}.html')

# And then use it to render your templates
html = template.render( ) # Your args goes here

學分:這個答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM