Created
March 12, 2017 23:02
-
-
Save develmaycare/15b33411825a0c911cec2b012e221245 to your computer and use it in GitHub Desktop.
An example of how to use Jinja as a standalone parser. This is especially useful for command line packages.
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
# Perhaps there is a better way to do this, but this worked for parsing templates in pyprojectutils. | |
def parse_jinja_template(path, context): | |
# The path is the path to the template. One directory up becomes the search path. | |
search_path = os.path.dirname(path) | |
env = Environment(loader=FileSystemLoader(search_path)) | |
# The template name is at the end of the path in this case. | |
template_name = os.path.basename(path) | |
template = env.get_template(template_name) | |
# Then it's just a normal render. | |
return template.render(**context) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment