Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions moban/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def __init__(self, template_dirs, context_dirs):
def render_to_file(self, template_file, data_file, output_file):
template = self.jj2_environment.get_template(template_file)
data = self.context.get_data(data_file)
template.globals['__target__'] = output_file
template.globals['__template__'] = template.name
reporter.report_templating(template_file, output_file)

rendered_content = template.render(**data)
Expand Down Expand Up @@ -96,6 +98,8 @@ def _file_permissions_copy(self, template_file, output_file):
utils.file_permissions_copy(true_template_file, output_file)

def _apply_template(self, template, data, output):
template.globals['__target__'] = output
template.globals['__template__'] = template.name
temp_file_path = get_template_path(self.template_dirs, template)
rendered_content = template.render(**data)
rendered_content = utils.strip_off_trailing_new_lines(rendered_content)
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/globals/nested.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% include 'variables.template' %}
3 changes: 3 additions & 0 deletions tests/fixtures/globals/variables.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
template: {{ __template__ }}
target: {{ __target__ }}
{{ test }}
1 change: 1 addition & 0 deletions tests/fixtures/globals/variables.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test: here
22 changes: 22 additions & 0 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,25 @@ def test_globals():
content = output_file.read()
eq_(content, "world\n\ntest")
os.unlink(output)


def test_global_template_variables():
output = "test.txt"
path = os.path.join("tests", "fixtures", "globals")
engine = Engine([path], path)
engine.render_to_file("variables.template", "variables.yml", output)
with open(output, "r") as output_file:
content = output_file.read()
eq_(content, "template: variables.template\ntarget: test.txt\nhere")
os.unlink(output)


def test_nested_global_template_variables():
output = "test.txt"
path = os.path.join("tests", "fixtures", "globals")
engine = Engine([path], path)
engine.render_to_file("nested.template", "variables.yml", output)
with open(output, "r") as output_file:
content = output_file.read()
eq_(content, "template: nested.template\ntarget: test.txt\nhere")
os.unlink(output)