Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment on Jinja post: pathlib example #152

Open
kolibril13 opened this issue Apr 2, 2022 · 0 comments
Open

Comment on Jinja post: pathlib example #152

kolibril13 opened this issue Apr 2, 2022 · 0 comments

Comments

@kolibril13
Copy link

https://code-maven.com/minimal-example-generating-html-with-python-jinja
Thanks for your amazing jinja example.
One suggestion: The first example could be simlified a bit with pathlib

from jinja2 import Environment, FileSystemLoader
from pathlib import Path
 
root =  Path.cwd()
templates_dir = root /'templates'
env = Environment( loader = FileSystemLoader(templates_dir) )
template = env.get_template('index.html')
filename = root / "rendered_index.html"

with open(filename, 'w') as fh:
    fh.write(template.render(
        h1 = "Hello Jinja2",
        show_one = True,
        show_two = False,
        names    = ["Foo", "Bar", "Qux"],
    ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant