Advertisement
Guest User

Untitled

a guest
May 24th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import os,jinja2
  4.  
  5. # output_file would be the result file (index.html)
  6. # output_dir would be the location in which to place the result file (/tmp/results)
  7. # template_file would be the file to use as a template (index.html.j2)
  8. # template_dir would be the location of template_file (only directories!)
  9. # context would be all the variables to use in the template rendering process
  10. def template_generator(output_file, output_dir, template_file, template_dir, context):
  11. template_loader = jinja2.FileSystemLoader(searchpath=template_dir)
  12. template_env = jinja2.Environment(loader=template_loader)
  13. template = template_env.get_template(template_file)
  14. output_text = template.render(context)
  15. output = output_dir + "/" + output_file
  16.  
  17. # In case you want to write it to a file, you could do this
  18. with open(output, 'w') as f:
  19. f.write(output_text + "\n")
  20.  
  21. # Alternatively, you can simply return the output...
  22. return output_text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement