Skip to content
Myr

[svn] Proofread the rest of the docs.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Georg Brandl · committed by Georg Brandl · parent bb960d4 · Browse files · 7 files+67 added−67 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+67 added−67 removed

  • other:+67 added−67 removed

7 files+67 added−67 removed

modified:docs/src/devintro.txt+43 added-43 removed
1====================2Developer Quickstart3====================45This part of the documentation shows you how to embedd Jinja into your5This part of the documentation shows you how to embed Jinja into your6application.78Starting Up9===========
10 unmodified lines
20This example should output the following string after execution::2122 Hello John Doe!2324If you receive an error check if you have a typo in your code. If not have24If you receive an error, check if you have a typo in your code. If not, have25a look at the `installation`_ page for troubleshooting.2627The Environment28===============2930The core component of Jinja is the `Environment`. It helds important shared30The core component of Jinja is the `Environment`. It contains important shared31variables like configuration, filters, tests, globals and other stuff.3233Here the possible initialisation parameters:33Here the possible initialization parameters:3435=========================== ==================================================36``block_start_string`` * the string marking the begin of a block. this37 defaults to ``'{%'``.38``block_end_string`` * the string marking the end of a block. defaults39 to ``'%}'``.40``variable_start_string`` * the string marking the begin of a print41 statement. defaults to ``'{{'``.42``comment_start_string`` * the string marking the begin of a43 comment. defaults to ``'{#'``.44``comment_end_string`` * the string marking the end of a comment.45 defaults to ``'#}'``.46``trim_blocks`` * If this is set to ``True`` the first newline47 after a block is removed (block, not48 variable tag!). Defaults to ``False``.49``auto_escape`` If this is set to ``True`` Jinja will50 automatically escape all variables using xml51 escaping methods. If you don't want to escape a52 string you have to wrap it in a ``Markup``53 object from the ``jinja.datastructure`` module.54``template_charset`` The charset of the templates. Defaults55 to ``'utf-8'``.56``charset`` Charset of all string input data. Defaults57 to ``'utf-8'``.58``namespace`` Global namespace for all templates.59``loader`` Specify a template loader.60``filters`` dict of filters or the default filters if not61 defined.62``tests`` dict of tests of the default tests if not defined.63=========================== ==================================================35========================= ==================================================36`block_start_string` * the string marking the begin of a block. this37 defaults to ``'{%'``.38`block_end_string` * the string marking the end of a block. defaults39 to ``'%}'``.40`variable_start_string` * the string marking the begin of a print41 statement. defaults to ``'{{'``.42`comment_start_string` * the string marking the begin of a43 comment. defaults to ``'{#'``.44`comment_end_string` * the string marking the end of a comment.45 defaults to ``'#}'``.46`trim_blocks` * If this is set to ``True`` the first newline47 after a block is removed (block, not48 variable tag!). Defaults to ``False``.49`auto_escape` If this is set to ``True`` Jinja will50 automatically escape all variables using xml51 escaping methods. If you don't want to escape a52 string you have to wrap it in a ``Markup``53 object from the ``jinja.datastructure`` module.54`template_charset` The charset of the templates. Defaults55 to ``'utf-8'``.56`charset` Charset of all string input data. Defaults57 to ``'utf-8'``.58`namespace` Global namespace for all templates.59`loader` Specify a template loader.60`filters` dict of filters or the default filters if not61 defined.62`tests` dict of tests of the default tests if not defined.63========================= ==================================================6465All of this variables except those marked with a star(*) are modifyable after66environment initialisation.65All of these variables except those marked with a star (*) are modifiable after66environment initialization.6768The environment provides the following useful functions and properties69additional to the initialisation values:68The environment provides the following useful functions and properties in69addition to the initialization values:7071=========================== ==================================================72``parse(source, filename)`` Parse the sourcecode and return the abstract73 syntax tree. This tree of nodes is used by the74 `translators`_ to convert the template into75 executable source- or bytecode.76``from_string(source)`` Load and parse a template source and translate it77 into evaluable python code. This code is wrapped78 with in a `Template` class that allows you to77 into eval-able Python code. This code is wrapped78 within a `Template` class that allows you to79 render it.80``get_template(name)`` load a template from a loader. If the template81 does not exist you will get a `TemplateNotFound`80``get_template(name)`` Load a template from a loader. If the template81 does not exist, you will get a `TemplateNotFound`82 exception.83=========================== ==================================================8485There are also some internal functions on the environment used by the template
21 unmodified lines
107108Adding Filters109==============110111If you want to add additional filters to the environment the best way is to111If you want to add additional filters to the environment, the best way is to112modify the ``filters`` attribute and not to pass a dict to the environment.113If you pass it a dict it will not include the default filters!114115.. sourcecode:: python
5 unmodified lines
121122Adding Tests123============124125Adding additional tests works analog to filters:125Adding additional tests works analogous to filters:126127.. sourcecode:: python128129 from mylib import my_cool_test
10 unmodified lines
modified:docs/src/filters.txt+5 added-5 removed
10 unmodified lines
1112Writing Filters13===============1415A filter basically is a factory function. Thus a function that returns another16function. We do this because filters can get an unlimited amount of position15A filter basically is a factory function, a function that returns another16function. We do this because filters can get an unlimited amount of positional17arguments and aditionally should gain access to the environment, context and18piped value. A simple filter looks like this:1920.. sourcecode:: python
11 unmodified lines
32.. sourcecode:: python3334 env.filters['join'] = do_join3536In fact this filter is already bundled so you won't see any effect. But its36In fact this filter is already bundled so you won't see any effect. But it37should explain how such a filter looks like. The template designer can just38trigger the outer code (eg: call do_join with or without arguments). The38trigger the outer code (i.e. call `join` with or without arguments). The39returned function is then processed by the jinja template engine once all40filters are created.4142If you want to create filters that just operate on string (in fact unicode42If you want to create filters that just operate on a string (in fact unicode43objects) you can use the `stringfilter` decorator:4445.. sourcecode:: python46
11 unmodified lines
modified:docs/src/frameworks.txt+1 added-1 removed
2 unmodified lines
3=====================45Jinja registers itself in the baker template plugin system. If your framework6supports baker (currently `TurboGears`_ and `pylons`_) you can add it very7easy.7easily.89Pylons10------11
22 unmodified lines
modified:docs/src/fromdjango.txt+10 added-10 removed
1===============================2Differences To Django Templates3===============================45If you have previously worked with Django templates you should feel very6familiar. In fact most of the syntax elements look and work the same.5If you have previously worked with Django templates, you should find Jinja very6familiar. In fact, most of the syntax elements look and work the same.78However Jinja provides some more syntax elements covered in the documentation8However, Jinja provides some more syntax elements covered in the documentation9and some work a bit different.1011Method Calls12============1314In Django method calls work implicit. With Jinja you have to tell it that you15want to call it. Thus this Django code:14In Django method calls work implicitly. With Jinja you have to specify that you15want to call an object. Thus this Django code:1617.. sourcecode:: django1819 {% for page in user.get_created_pages %}
51 unmodified lines
7172Filter Arguments73================7475Jinja provides more than one argument for a filter. Also the syntax for argument75Jinja provides more than one argument for filters. Also the syntax for argument76passing is different. A template that looks like this in Django:7778.. sourcecode:: django79
4 unmodified lines
84.. sourcecode:: jinja8586 {{ items|join(', ') }}8788In fact it's a bit more to write but it allows different type of arguments including89variables and more then one of them.88In fact it's a bit more verbose but it allows different types of arguments - including89variables - and more than one of them.9091Tests92=====9394Additionally to filters there also exists tests you can perform using the `is`95operator. Here some examples:94In addition to filters there also are tests you can perform using the `is` operator.95Here are some examples:9697.. sourcecode:: jinja9899 {% if user.user_id is odd %}
9 unmodified lines
modified:docs/src/loaders.txt+6 added-6 removed
10 unmodified lines
1112Developing Loaders13==================1415Template loaders are just normal python classes that have to provide some16functions used to load and translate templates. Here a simple loader implementation17you can use as base for your own loader:15Template loaders are just normal Python classes that have to provide some16functions used to load and translate templates. Here is a simple loader implementation17you can use as the base for your own loader:1819.. sourcecode:: python2021 from os.path import join
9 unmodified lines
31 self.searchpath = searchpath3233 def get_source(self, environment, name, parent):34 """35 The get_source function is unused at the moment. However future35 The get_source function is unused at the moment. However, future36 versions of jinja will use this function for the debugging37 system. It also works as helper functions for `parse` and38 `load`.39 """
15 unmodified lines
5556 def load(self, environment, name, translator):57 """58 Parse and translate a template. Currently only translation to59 python code is possible, later jinja versions however will59 python code is possible, later Jinja versions however will60 support translating templates to javascript too.61 """62 return translator.process(environment, self.parse(environment, name, None))636465.. admonition:: Note6667 Once a loader is bound to an environment you have to omit the environment67 Once a loader is bound to an environment, you have to omit the environment68 argument for the public functions `get_source`, `parse` and `load`.
modified:docs/src/tests.txt+1 added-1 removed
1==============2Test Functions3==============45Additionally to filters Jinja also supports test functions. Test functions5In addition to filters, Jinja also supports test functions. Test functions6always return either ``True`` or ``False``. Inside the template they are available7using the `is` operator.89Jinja comes with some builtin tests listed in the `designer documentation`_.
21 unmodified lines
modified:docs/src/translators.txt+1 added-1 removed
3 unmodified lines
45Jinja translates the template sourcecode into executable python code behind6the secenes. This is done by the python translator which is currently the7only shipped translator. Because the interface isn't stable it's also not8recommended to write other translators. However for the next Jinja version8recommended yet to write other translators. However for the next Jinja version9a JavaScript translator is planned which allows you to translate Jinja10templates into executable JavaScript code.