Skip to content

Instantly share code, notes, and snippets.

@mdutton3
Last active May 28, 2020 16:50
Show Gist options
  • Save mdutton3/e54a44f322f965bccec19dd2df81ed75 to your computer and use it in GitHub Desktop.
Save mdutton3/e54a44f322f965bccec19dd2df81ed75 to your computer and use it in GitHub Desktop.
RobotFramework Jinja2 Example
{
"Arg1": {{ num_value }},
"A_List": [
{% for x in (a_list or []) -%}
"{{ x }}"{% if not loop.last %},{% endif %}
{% endfor %}
],
"SomeStaticData": "alajsdlkajdlkajsdlad"
}
*** Settings ***
Test Setup Store Suite Directory
Library ./RF_Jinja.py
*** Keywords ***
Store Suite Directory
Set Suite Variable ${SUITE_DIR} ${CURDIR}
Log To Console Suite Dir is ${SUITE_DIR} level=INFO
*** Test Cases ***
Log Template Output
${a_list}= Create List Foo Bar Baz
${output}= Template File ${SUITE_DIR}/example.json.j2 num_value=5 a_list=${a_list}
Log To Console ${output}
from __future__ import print_function
from jinja2 import Template
import sys
def template_file(path, **kwargs):
"""
Evaluate the Jinja2 template at path using the given kwargs as the environment.
"""
with open(path, 'r') as file_:
template = Template(file_.read())
return template.render(**kwargs)
__all__ = [ 'template_file' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment