Render the template with the given context.
(self, **context: Any)
| 120 | self.template = load_content(uri, caller_dir=caller_dir, ftype=ftype) |
| 121 | |
| 122 | def r(self, **context: Any) -> str: |
| 123 | """ |
| 124 | Render the template with the given context. |
| 125 | """ |
| 126 | # loader=FunctionLoader(load_conent) is for supporting grammar like below. |
| 127 | # `{% include "scenarios.data_science.share:component_spec.DataLoadSpec" %}` |
| 128 | rendered = ( |
| 129 | Environment(undefined=StrictUndefined, loader=FunctionLoader(load_content)) |
| 130 | .from_string(self.template) |
| 131 | .render(**context) |
| 132 | .strip("\n") |
| 133 | ) |
| 134 | while "\n\n\n" in rendered: |
| 135 | rendered = rendered.replace("\n\n\n", "\n\n") |
| 136 | logger.log_object( |
| 137 | obj={ |
| 138 | "uri": self.uri, |
| 139 | "template": self.template, |
| 140 | "context": context, |
| 141 | "rendered": rendered, |
| 142 | }, |
| 143 | tag="debug_tpl", |
| 144 | ) |
| 145 | return rendered |
| 146 | |
| 147 | |
| 148 | T = RDAT # shortcuts |