For very large templates it can be useful to not render the whole template at once but evaluate each statement after another and yield piece for piece. This method basically does exactly that and returns a generator that yields one item after another as strings. It
(self, *args: t.Any, **kwargs: t.Any)
| 1324 | return TemplateStream(self.generate(*args, **kwargs)) |
| 1325 | |
| 1326 | def generate(self, *args: t.Any, **kwargs: t.Any) -> t.Iterator[str]: |
| 1327 | """For very large templates it can be useful to not render the whole |
| 1328 | template at once but evaluate each statement after another and yield |
| 1329 | piece for piece. This method basically does exactly that and returns |
| 1330 | a generator that yields one item after another as strings. |
| 1331 | |
| 1332 | It accepts the same arguments as :meth:`render`. |
| 1333 | """ |
| 1334 | if self.environment.is_async: |
| 1335 | import asyncio |
| 1336 | |
| 1337 | async def to_list() -> t.List[str]: |
| 1338 | return [x async for x in self.generate_async(*args, **kwargs)] |
| 1339 | |
| 1340 | yield from asyncio.run(to_list()) |
| 1341 | return |
| 1342 | |
| 1343 | ctx = self.new_context(dict(*args, **kwargs)) |
| 1344 | |
| 1345 | try: |
| 1346 | yield from self.root_render_func(ctx) |
| 1347 | except Exception: |
| 1348 | yield self.environment.handle_exception() |
| 1349 | |
| 1350 | async def generate_async( |
| 1351 | self, *args: t.Any, **kwargs: t.Any |