Add support for {% include %} in Jinja2 templates

This allows the use of the {% include %} tag[0] within Jinja2 templates,
with the same search path used for looking up other templates (i.e. the
one which LocalLoader is instantiated with).

[0] http://jinja.pocoo.org/docs/2.9/templates/#include

Change-Id: I0403c385ee317e1460a80a9bf717aa4712f37e2f
This commit is contained in:
Daniel Watkins 2017-08-14 18:18:51 -04:00
parent bc80e5e2f3
commit aa74f8d67c
6 changed files with 82 additions and 3 deletions

View File

@ -324,7 +324,7 @@ class J2String(BaseYAMLObject):
@classmethod
def from_yaml(cls, loader, node):
return Jinja2Loader(node.value)
return Jinja2Loader(node.value, loader.search_path)
class YamlInclude(BaseYAMLObject):
@ -423,7 +423,7 @@ class YamlIncludeJinja2(YamlIncludeRaw):
contents = cls._open_file(loader, node)
if isinstance(contents, LazyLoader):
return contents
return Jinja2Loader(contents)
return Jinja2Loader(contents, loader.search_path)
class DeprecatedTag(BaseYAMLObject):
@ -456,9 +456,12 @@ class CustomLoader(object):
class Jinja2Loader(CustomLoader):
"""A loader for Jinja2-templated files."""
def __init__(self, contents):
def __init__(self, contents, search_path):
self._template = jinja2.Template(contents)
self._template.environment.undefined = jinja2.StrictUndefined
self._template.environment.loader = jinja2.FileSystemLoader(
search_path)
def format(self, **kwargs):
return self._template.render(kwargs)

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</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>

View File

@ -0,0 +1,15 @@
- builder:
name: test-builder
builders:
- shell:
!include-jinja2: jinja-include01.yaml.inc
- job:
name: test-job
builders:
- test-builder:
var: "test variable"
test_list:
- a
- b
- c

View File

@ -0,0 +1 @@
{% include 'jinja01.yaml.inc' %}

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</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>

View File

@ -0,0 +1,14 @@
- builder:
name: test-builder
builders:
- shell: !j2: "{% include 'jinja01.yaml.inc' %}"
- job:
name: test-job
builders:
- test-builder:
var: "test variable"
test_list:
- a
- b
- c