Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import os,jinja2
- # output_file would be the result file (index.html)
- # output_dir would be the location in which to place the result file (/tmp/results)
- # template_file would be the file to use as a template (index.html.j2)
- # template_dir would be the location of template_file (only directories!)
- # context would be all the variables to use in the template rendering process
- def template_generator(output_file, output_dir, template_file, template_dir, context):
- template_loader = jinja2.FileSystemLoader(searchpath=template_dir)
- template_env = jinja2.Environment(loader=template_loader)
- template = template_env.get_template(template_file)
- output_text = template.render(context)
- output = output_dir + "/" + output_file
- # In case you want to write it to a file, you could do this
- with open(output, 'w') as f:
- f.write(output_text + "\n")
- # Alternatively, you can simply return the output...
- return output_text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement