frameworks.txt
This file
Path
docs/src/frameworks.txt
247 lines · 10.2 KiB
Last changed by
documented pylons' "strict_c".Armin Ronacherbefore adoption1986d2c
History of this fileTake it
The walk stopped after 200 commits: lines older than 859efe0 point at it.
[svn] added ugly, ugly, ugly even more ugly but working hack to jinja to patch the template sources into linecache in order to support broken traceback formatters which go unnamed.Armin Ronacherbefore adoption859efe0
lines 1–4why, one step back1=====================2Framework Integration3=====================4[svn] various updates i haven't checked in so far (see the diff of the changelog) and fixed critical bug reported by Alexey MelchakovArmin Ronacherbefore adoption2158091
lines 5–17why, one step back5Starting 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.17some more doc changes in jinja for the upcoming releaseArmin Ronacherbefore adoption825e522
line 18why, one step back18[svn] various updates i haven't checked in so far (see the diff of the changelog) and fixed critical bug reported by Alexey MelchakovArmin Ronacherbefore adoption2158091
lines 19–89why, one step back19Buffet20======2122The buffet specification proposes that templates are named in dotted names. That23means `foo.bar` and not `foo/bar.html`. The dotted notation has the disadvantage24that you cannot specify the filename extension. In recent pylons versions it's25however possible to load templates with their native path too if you prefix the26template name with a foreslash (`/foo/bar.html`). If you don't specify the27extension it will assume `.html` for the dotted notation.2829Here the list of configuration values:3031======================= ======================================================32``jinja.extension`` The template extension when templates are loaded using33 the dotted notation. Defaults to ``html``.34``jinja.environment`` If this is provided it must be the only configuration35 value and it's used as jinja environment. In that36 case all other configuration parameters except of37 ``jinja.extension`` are ignored.38``jinja.searchpath`` If provided a new file system loader with this39 search path is instanciated.40``jinja.package`` Name of the python package containing the41 templates. If this and ``package_path`` is42 defined a `PackageLoader` is used.43``jinja.package_path`` Path to the templates inside of a package.44``jinja.loader_func`` Function that takes the name of the template to45 load. If it returns a string or unicode object46 it's used to load a template. If the return47 value is None it's considered missing.48``jinja.getmtime_func`` Function used to check if templates requires49 reloading. Has to return the UNIX timestamp of50 the last template change or 0 if this template51 does not exist or requires updates at any cost.52``jinja.use_memcache`` Set this to ``True`` to enable memory caching.53 This is usually a good idea in production mode,54 but disable it during development since it won't55 reload template changes automatically.56 This only works in persistent environments like57 FastCGI.58``jinja.memcache_size`` Number of template instance you want to cache.59 Defaults to ``40``.60``jinja.cache_folder`` Set this to an existing directory to enable61 caching of templates on the file system. Note62 that this only affects templates transformed63 into python code. Default is ``None`` which means64 that caching is disabled.65``jinja.auto_reload`` Set this to `False` for a slightly better66 performance. In that case of `getmtime_func`67 not being provided this won't have an effect.68======================= ======================================================6970All other options that start with `jinja.` are automatically forwarded to the71environment constructor.7273In pylons for example you can use jinja as buffet plugin like this:7475Edit the `yourproject/config/middleware.py` and add this to `config.init_app`:7677.. sourcecode:: python7879 config.add_template_engine('jinja', '', {80 'jinja.package': 'yourapplication',81 'jinja.package_path': 'res/templates',82 'jinja.use_memcache': True83 })8485Note that it's a good idea to set the second parameter to an empty string.86It's meant to be used as replacement for the turbogears package name but87Jinja assumes that the name of the template does not include the package88path.89documented pylons' "strict_c".Armin Ronacherbefore adoption1986d2c
lines 90–99why, one step back90.. admonition:: Note9192 Special note for pylons users. Jinja will be unable to request attributes93 from the special `c` object, unless you set `strict_c` to `True` in the94 `environment.py`:9596 .. sourcecode:: python9798 return pylons.config.Config(tmpl_options, map, paths, strict_c=True)99[svn] various updates i haven't checked in so far (see the diff of the changelog) and fixed critical bug reported by Alexey MelchakovArmin Ronacherbefore adoption2158091
lines 100–121why, one step back100You can then render the template in the view like this:101102.. sourcecode:: python103104 class ExampleController(BaseController):105106 def index(self):107 c.title = "Your Page"108 c.message = 'hi'109 return render_response('jinja', 'test_template')110111 def download(self):112 c.title = "Downloads"113 c.downloads = [1, 2, 3]114 return render_response('jinja', '/downloads.html')115116With the settings from above rendering the `index` action will result in117rendering the template ``res/templates/test_template.html`` where res is118a folder in the ``yourapplication`` python package.119120The `downloads` action uses the pylons specific leading foreslash notation.121some more doc changes in jinja for the upcoming releaseArmin Ronacherbefore adoption825e522
line 122why, one step back122[svn] various updates i haven't checked in so far (see the diff of the changelog) and fixed critical bug reported by Alexey MelchakovArmin Ronacherbefore adoption2158091
lines 123–129why, one step back123General Template Interface124==========================125126Because nobody implemented this specification so far it's not documented here127but in the sourcecode of the `plugin module`_. The specification itself is128explained on the pocoo trac on the `General Template Interface`_ wiki page.129[svn] added new jinja unittest and added snipped contributed by Bryan McLemore.Armin Ronacherbefore adoption5b4e975
lines 130–135why, one step back130131Django132======133134Using Jinja in django is straightforward because django has a pretty low135level response interface. Just have a look at the `developer recipies`_,some more doc changes in jinja for the upcoming releaseArmin Ronacherbefore adoption825e522
lines 136–235why, one step back136there are some examples for django. Starting with Jinja 1.2 there is also137a contrib module that simplifies using Jinja in an unicode enabled django.138139Quickstart140----------141142To get started execute the following code at the bottom of your settings.py143or in some general application file such as urls.py or a central module. The144only thing that matters is that it's executed right *after* the settings145were set up and *before* `django.contrib.jinja` is imported::146147 from jinja.contrib import djangosupport148 djangosupport.configure()149150What this does is setting up a Jinja environment for this django instance151with loaders for `TEMPLATE_DIRS` etc. It also converts a couple of default152django filters such as `date` and `timesince` which are not available in153Jinja per default. If you want to change the list you can provide others154by passing a list with filter import names as `convert_filters` keyword155argument.156157All other keyword arguments are forwarded to the environment. If you want158to provide a loader yourself pass it a loader keyword argument.159160Rendering Templates161-------------------162163To render a template you can use the functions `render_to_string` or164`render_to_response` from the `django.contrib.jinja` module::165166 from django.contrib.jinja import render_to_response167 resp = render_to_response('Hello {{ username }}!', {168 'username': req.session['username']169 }, req)170171`render_to_string` and `render_to_response` take at least the name of172the template as argument, then the optional dict which will become the173context. If you also provide a request object as third argument the174context processors will be applied.175176`render_to_response` also takes a forth parameter which can be the177content type which defaults to `DEFAULT_CONTENT_TYPE`.178179Converting Filters180------------------181182One of the useful objects provided by `django.contrib.jinja` is the183`register` object which can be used to register filters, tests and184global objects. You can also convert any filter django provides in185a Jinja filter using `convert_django_filter`::186187 from django.contrib.jinja import register, convert_django_filter188 from django.template.defaultfilters import floatformat189190 register.filter(convert_django_filter(floatformat), 'floatformat')191192Available methods on the `register` object:193194``object (obj[, name])``195 Register a new global as name or with the object's name.196 Returns the function object unchanged so that you can use197 it as decorator if no name is provided.198199``filter (func[, name])``200 Register a function as filter with the name provided or201 the object's name as filtername.202 Returns the function object unchanged so that you can use203 it as decorator if no name is provided.204205``test (func[, name])``206 Register a function as test with the name provided or the207 object's name as testname.208 Returns the function object unchanged so that you can use209 it as decorator if no name is provided.210211``context_inclusion (func, template[, name])``212 Register a function with a name provided or the func object's213 name in the global namespace that acts as subrender function.214215 func is called with the callers context as dict and the216 arguments and keywords argument of the inclusion function.217 The function should then process the context and return a218 new context or the same context object. Afterwards the219 template is rendered with this context.220221 Example::222223 def add_author(context, author=None):224 if author is not None:225 author = Author.objects.get(name=author)226 context['author'] = author227 return context228229 register.context_inclusion(add_author, 'author_details.html',230 'render_author_details')231232 You can use it in the template like this then::233234 {{ render_author_details('John Doe') }}235some more small release changesArmin Ronacherbefore adoption99e5baa
line 236why, one step back236``clean_inclusion (func, template[, name[, run_processors]])``some more doc changes in jinja for the upcoming releaseArmin Ronacherbefore adoption825e522
lines 237–241why, one step back237 Works like `context_inclusion` but doesn't use the calles238 context but an empty context. If `run_processors` is `True`239 it will lookup the context for a `request` object and pass240 it to the render function to apply context processors.241[svn] added new jinja unittest and added snipped contributed by Bryan McLemore.Armin Ronacherbefore adoption5b4e975
lines 242–243why, one step back242243[svn] various updates i haven't checked in so far (see the diff of the changelog) and fixed critical bug reported by Alexey MelchakovArmin Ronacherbefore adoption2158091
lines 244–246why, one step back244.. _Pylons: http://www.pylonshq.com/245.. _General Template Interface: http://trac.pocoo.org/wiki/GeneralTemplateInterface246.. _plugin module: http://trac.pocoo.org/browser/jinja/trunk/jinja/plugin.py[svn] added new jinja unittest and added snipped contributed by Bryan McLemore.Armin Ronacherbefore adoption5b4e975
line 247why, one step back247.. _developer recipies: devrecipies.txt