jinja_ext.py
Jinja2 extensions built for Copier.
YieldEnvironment
¶
Bases: SandboxedEnvironment
Jinja2 environment with attributes from the YieldExtension.
This is simple environment class that extends the SandboxedEnvironment for use with the YieldExtension, mainly for avoiding type errors.
We use the SandboxedEnvironment because we want to minimize the risk of hidden malware in the templates. Of course we still have the post-copy tasks to worry about, but at least they are more visible to the final user.
Source code in copier/_jinja_ext.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
YieldExtension
¶
Bases: Extension
Jinja2 extension for the yield tag.
If yield tag is used in a template, this extension sets following attribute to the
jinja environment:
yield_name: The name of the variable that will be yielded.yield_iterable: The variable that will be looped over.
Note that this extension just sets the attributes but renders templates as usual.
It is the caller's responsibility to use the yield_context attribute in the template to
generate the desired output.
Example
>>> from copier.jinja_ext import YieldEnvironment, YieldExtension
>>> env = YieldEnvironment(extensions=[YieldExtension])
>>> template = env.from_string("{% yield single_var from looped_var %}{{ single_var }}{% endyield %}")
>>> template.render({"looped_var": [1, 2, 3]})
''
>>> env.yield_name
'single_var'
>>> env.yield_iterable
[1, 2, 3]
Source code in copier/_jinja_ext.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
parse(parser)
¶
Parse the yield tag.
Source code in copier/_jinja_ext.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
preprocess(source, _name, _filename=None)
¶
Preprocess hook to reset attributes before rendering.
Source code in copier/_jinja_ext.py
68 69 70 71 72 73 74 | |