Template Designer Documentation - Jinja2 Documentation (2.10) - 0 views
-
A Jinja template doesn’t need to have a specific extension
- ...106 more annotations...
-
the default behavior is to evaluate to an empty string if printed or iterated over, and to fail for every other operation.
-
if an object has an item and attribute with the same name. Additionally, the attr() filter only looks up attributes.
-
Variables can be modified by filters. Filters are separated from the variable by a pipe symbol (|) and may have optional arguments in parentheses.
-
to find out if a variable is defined, you can do name is defined, which will then return true or false depending on whether name is defined in the current template context.
-
strip whitespace in templates by hand. If you add a minus sign (-) to the start or end of a block (e.g. a For tag), a comment, or a variable expression, the whitespaces before or after that block will be removed
-
Template inheritance allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override.
-
The {% extends %} tag is the key here. It tells the template engine that this template “extends” another template.
-
if the block is replaced by a child template, a variable would appear that was not defined in the block or passed to the context.
-
If you have a variable that may include any of the following chars (>, <, &, or ") you SHOULD escape it unless the variable contains well-formed and trusted HTML.
-
Jinja2 functions (macros, super, self.BLOCKNAME) always return template data that is marked as safe.
-
add the recursive modifier to the loop definition and call the loop variable with the new iterable where you want to recurse.
-
a block tag works in “both” directions. That is, a block tag doesn’t just provide a placeholder to fill - it also defines the content that fills the placeholder in the parent.
-
Assignments at top level (outside of blocks, macros or loops) are exported from the template like top level macros and can be imported by other templates.
-
The include statement is useful to include a template and return the rendered contents of that file into the current namespace
-
imports are cached and imported templates don’t have access to the current template variables, just the globals by default.
-
default(value, default_value=u'', boolean=False)¶ If the value is undefined it will return the passed default value, otherwise the value of the variable
-
dictsort(value, case_sensitive=False, by='key', reverse=False)¶ Sort a dict and yield (key, value) pairs.
-
grouping by is stored in the grouper attribute and the list contains all the objects that have this grouper in common.
-
indent(s, width=4, first=False, blank=False, indentfirst=None)¶ Return a copy of the string with each line indented by 4 spaces. The first line and blank lines are not indented by default.
-
join(value, d=u'', attribute=None)¶ Return a string which is the concatenation of the strings in the sequence.
-
reject()¶ Filters a sequence of objects by applying a test to each object, and rejecting the objects with the test succeeding.
-
replace(s, old, new, count=None)¶ Return a copy of the value with all occurrences of a substring replaced with a new one.
-
select()¶ Filters a sequence of objects by applying a test to each object, and only selecting the objects with the test succeeding.
-
sort(value, reverse=False, case_sensitive=False, attribute=None)¶ Sort an iterable. Per default it sorts ascending, if you pass it true as first argument it will reverse the sorting.
-
tojson(value, indent=None)¶ Dumps a structure to JSON so that it’s safe to use in <script> tags.
-
unique(value, case_sensitive=False, attribute=None)¶ Returns a list of unique items from the the given iterable
-
urlize(value, trim_url_limit=None, nofollow=False, target=None, rel=None)¶ Converts URLs in plain text into clickable links.
-
A joiner is passed a string and will return that string every time it’s called, except the first time (in which case it returns an empty string).
-
The with statement makes it possible to create a new inner scope. Variables set within this scope are not visible outside of the scope.
-
With both trim_blocks and lstrip_blocks enabled, you can put block tags on their own lines, and the entire block line will be removed when rendered, preserving the whitespace of the contents