Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML templating using Jinja2 - Lost

I am trying to create a html template in python using Jinja2. I have a templates folder with my 'template.html' but I don't know how to deal with environments or package loaders.

I installed Jinja2 using easy_python and ran the following script.

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

I get the following error because I don't know how to define a package/module. Please help me I just want to create a simple template.

  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
like image 943
luifer Avatar asked Jan 11 '13 11:01

luifer


3 Answers

If you don't want or need a Python package, you probably should use a FileSystemLoader instead, like this:

from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
    loader=FileSystemLoader('file/path/'),
    autoescape=select_autoescape(['html', 'xml']),
)
like image 175
Flimm Avatar answered Nov 15 '22 14:11

Flimm


PackageLoader expects an actual Python module using the regular dot syntax. For example if your structure looks like this:

myapp/
  __init__.py
  …
  templates/
    mytemplate.html

You should use myapp as the module name.

like image 42
patrys Avatar answered Nov 15 '22 15:11

patrys


PackageLoader is defined like this:

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.
    """

And then the __init__() method is as follows:

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

This makes us notice that a structure like this:

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

Will have the same PackageLoader instance with both of these declarations:

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

And so if you are running from the myapp/ path, you then just need to say:

PackageLoader('templates', '')

So that it will just take templates/ as the path. If you leave the 2nd argument empty, it will try to find the templates in templates/templates.

Finally, you can check what has been loaded by using the list_templates() method:

PackageLoader('templates', '').list_templates()
like image 36
fedorqui 'SO stop harming' Avatar answered Nov 15 '22 15:11

fedorqui 'SO stop harming'