frameworks.txt
This file
Path
docs/src/frameworks.txt
130 lines · 5.9 KiB
Last changed by
[svn] added new jinja unittest and added snipped contributed by Bryan McLemore.Armin Ronacherbefore adoption5b4e975
History of this fileTake it
1=====================2Framework Integration3=====================45Starting with Jinja 1.1 it's possible to embed Jinja into some of the existing6frameworks a lot easier. When speaking of frameworks we only refer to `Pylons`_7which has a working implementation of the TurboGears template specification.89Since the whole situation is problematic because of various reasons (kid10specific, uses dotted names for template loading, package name prefix etc.)11we worked around some of the problems by using pylons specific workarounds.1213Jinja also ships an implementation for a hypothetical template abstraction layer14called `General Template Interface`_ which isn't implemented by any existing15framework so far. This specification however tries to solve the problems that16exist in Buffet.1718Buffet19======2021The buffet specification proposes that templates are named in dotted names. That22means `foo.bar` and not `foo/bar.html`. The dotted notation has the disadvantage23that you cannot specify the filename extension. In recent pylons versions it's24however possible to load templates with their native path too if you prefix the25template name with a foreslash (`/foo/bar.html`). If you don't specify the26extension it will assume `.html` for the dotted notation.2728Here the list of configuration values:2930======================= ======================================================31``jinja.extension`` The template extension when templates are loaded using32 the dotted notation. Defaults to ``html``.33``jinja.environment`` If this is provided it must be the only configuration34 value and it's used as jinja environment. In that35 case all other configuration parameters except of36 ``jinja.extension`` are ignored.37``jinja.searchpath`` If provided a new file system loader with this38 search path is instanciated.39``jinja.package`` Name of the python package containing the40 templates. If this and ``package_path`` is41 defined a `PackageLoader` is used.42``jinja.package_path`` Path to the templates inside of a package.43``jinja.loader_func`` Function that takes the name of the template to44 load. If it returns a string or unicode object45 it's used to load a template. If the return46 value is None it's considered missing.47``jinja.getmtime_func`` Function used to check if templates requires48 reloading. Has to return the UNIX timestamp of49 the last template change or 0 if this template50 does not exist or requires updates at any cost.51``jinja.use_memcache`` Set this to ``True`` to enable memory caching.52 This is usually a good idea in production mode,53 but disable it during development since it won't54 reload template changes automatically.55 This only works in persistent environments like56 FastCGI.57``jinja.memcache_size`` Number of template instance you want to cache.58 Defaults to ``40``.59``jinja.cache_folder`` Set this to an existing directory to enable60 caching of templates on the file system. Note61 that this only affects templates transformed62 into python code. Default is ``None`` which means63 that caching is disabled.64``jinja.auto_reload`` Set this to `False` for a slightly better65 performance. In that case of `getmtime_func`66 not being provided this won't have an effect.67======================= ======================================================6869All other options that start with `jinja.` are automatically forwarded to the70environment constructor.7172In pylons for example you can use jinja as buffet plugin like this:7374Edit the `yourproject/config/middleware.py` and add this to `config.init_app`:7576.. sourcecode:: python7778 config.add_template_engine('jinja', '', {79 'jinja.package': 'yourapplication',80 'jinja.package_path': 'res/templates',81 'jinja.use_memcache': True82 })8384Note that it's a good idea to set the second parameter to an empty string.85It's meant to be used as replacement for the turbogears package name but86Jinja assumes that the name of the template does not include the package87path.8889You can then render the template in the view like this:9091.. sourcecode:: python9293 class ExampleController(BaseController):9495 def index(self):96 c.title = "Your Page"97 c.message = 'hi'98 return render_response('jinja', 'test_template')99100 def download(self):101 c.title = "Downloads"102 c.downloads = [1, 2, 3]103 return render_response('jinja', '/downloads.html')104105With the settings from above rendering the `index` action will result in106rendering the template ``res/templates/test_template.html`` where res is107a folder in the ``yourapplication`` python package.108109The `downloads` action uses the pylons specific leading foreslash notation.110111General Template Interface112==========================113114Because nobody implemented this specification so far it's not documented here115but in the sourcecode of the `plugin module`_. The specification itself is116explained on the pocoo trac on the `General Template Interface`_ wiki page.117118119Django120======121122Using Jinja in django is straightforward because django has a pretty low123level response interface. Just have a look at the `developer recipies`_,124there are some examples for django.125126127.. _Pylons: http://www.pylonshq.com/128.. _General Template Interface: http://trac.pocoo.org/wiki/GeneralTemplateInterface129.. _plugin module: http://trac.pocoo.org/browser/jinja/trunk/jinja/plugin.py130.. _developer recipies: devrecipies.txt