Last active
December 23, 2015 10:29
-
-
Save franzwong/6622277 to your computer and use it in GitHub Desktop.
jinja2 sample code
This file contains hidden or 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
{ | |
"greeting": "おはようございます", | |
"name": "先生" | |
} |
This file contains hidden or 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 jinja2 import Template | |
import json | |
def loadFile(filePath): | |
with open(filePath, 'r', encoding='utf-8') as f: | |
return f.read() | |
def loadTemplate(filePath): | |
templateString = loadFile(filePath) | |
return Template(templateString) | |
def loadData(filePath): | |
dataString = loadFile(filePath) | |
return json.loads(dataString) | |
template = loadTemplate('template.txt'); | |
data = loadData('data.txt') | |
output = template.render(data) | |
print(output) |
This file contains hidden or 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
{{greeting}}, {{name}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment