Skip to content

Comment on Jinja post: pathlib example #152

@kolibril13

Description

@kolibril13

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"],
    ))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions