tutuca
tutuca

Reputation: 3670

Render to a custom output filename with a single template

I need to create a series of reports with the same template with jinja. But I would like to have each report in a different rendered file.

I can't find something relevant in jinja's documentation.

Is there a way to modify the rendered output filename?

Upvotes: 0

Views: 949

Answers (2)

Oleksii Kachaiev
Oleksii Kachaiev

Reputation: 6232

What is the problem with different files?

>>> from jinja2 import Template
>>> template = Template('Hello {{ name }}!')
>>> for n in ["John", "Doe"]:
>>>     with open(n + ".txt", "w") as f:
>>>         print >> f, template.render(name=n)

Upvotes: 0

Jakub M.
Jakub M.

Reputation: 33867

Maybe this would help?

import jinja2

env = jinja2.Environment( loader = jinja2.FileSystemLoader('templates/') )

def render_template( filename_template, filename_output ):
  nice = env.get_template( filename_template ).render()
  with open(filename_output,'w') as fh:
    fh.write(nice)

Upvotes: 2

Related Questions