Last active
May 28, 2020 16:50
-
-
Save mdutton3/e54a44f322f965bccec19dd2df81ed75 to your computer and use it in GitHub Desktop.
RobotFramework Jinja2 Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Arg1": {{ num_value }}, | |
"A_List": [ | |
{% for x in (a_list or []) -%} | |
"{{ x }}"{% if not loop.last %},{% endif %} | |
{% endfor %} | |
], | |
"SomeStaticData": "alajsdlkajdlkajsdlad" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*** 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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