def__init__(self,directory:typing.Union[str,PathLike],context_processors:typing.Optional[typing.List[typing.Callable[[Request],typing.Dict[str,typing.Any]]]]=None,**env_options:typing.Any,)->None:assertjinja2isnotNone,"jinja2 must be installed to use Jinja2Templates"self.env=self._create_env(directory,**env_options)self.context_processors=context_processorsor[]
defTemplateResponse(self,name:str,context:dict,status_code:int=200,headers:typing.Optional[typing.Mapping[str,str]]=None,media_type:typing.Optional[str]=None,background:typing.Optional[BackgroundTask]=None,)->_TemplateResponse:if"request"notincontext:raiseValueError('context must include a "request" key')request=typing.cast(Request,context["request"])forcontext_processorinself.context_processors:context.update(context_processor(request))template=self.get_template(name)return_TemplateResponse(template,context,status_code=status_code,headers=headers,media_type=media_type,background=background,)