Add !j2 directive to allow inline Jinja2 templates
This enables the use of Jinja2 templating within strings in the template YAML files. Change-Id: I2e912d2f874c5f2428e1aed4af94897aee8d4a72
This commit is contained in:
parent
5579395bc3
commit
d6c8111f66
|
@ -141,6 +141,15 @@ Examples:
|
|||
contents of jinja01.yaml.inc:
|
||||
|
||||
.. literalinclude:: /../../tests/yamlparser/fixtures/jinja01.yaml.inc
|
||||
|
||||
|
||||
The tag ``!j2:`` takes a string and treats it as a Jinja2 template. It will be
|
||||
rendered (with the variables in that context) and included in the calling YAML
|
||||
construct.
|
||||
|
||||
Examples:
|
||||
|
||||
.. literalinclude:: /../../tests/yamlparser/fixtures/jinja-string01.yaml
|
||||
"""
|
||||
|
||||
import functools
|
||||
|
@ -310,6 +319,14 @@ class BaseYAMLObject(YAMLObject):
|
|||
yaml_dumper = LocalDumper
|
||||
|
||||
|
||||
class J2String(BaseYAMLObject):
|
||||
yaml_tag = u'!j2:'
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, loader, node):
|
||||
return Jinja2Loader(node.value)
|
||||
|
||||
|
||||
class YamlInclude(BaseYAMLObject):
|
||||
yaml_tag = u'!include:'
|
||||
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<project>
|
||||
<actions/>
|
||||
<description><!-- Managed by Jenkins Job Builder --></description>
|
||||
<keepDependencies>false</keepDependencies>
|
||||
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
|
||||
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
|
||||
<concurrentBuild>false</concurrentBuild>
|
||||
<canRoam>true</canRoam>
|
||||
<properties/>
|
||||
<scm class="hudson.scm.NullSCM"/>
|
||||
<builders>
|
||||
<hudson.tasks.Shell>
|
||||
<command>test variable
|
||||
a
|
||||
b
|
||||
c
|
||||
</command>
|
||||
</hudson.tasks.Shell>
|
||||
</builders>
|
||||
<publishers/>
|
||||
<buildWrappers/>
|
||||
</project>
|
|
@ -0,0 +1,19 @@
|
|||
- builder:
|
||||
name: test-builder
|
||||
builders:
|
||||
- shell:
|
||||
!j2: |
|
||||
{{ var }}
|
||||
{% for item in test_list -%}
|
||||
{{ item }}
|
||||
{% endfor %}
|
||||
|
||||
- job:
|
||||
name: test-job
|
||||
builders:
|
||||
- test-builder:
|
||||
var: "test variable"
|
||||
test_list:
|
||||
- a
|
||||
- b
|
||||
- c
|
Loading…
Reference in New Issue