Replies: 2 comments
-
Sorry it has took so long for an answer to this question. It sounds like you might be looking for the functionality of a jinja macro? - I'm not familiar with Hugo to have a mindful alternative, but check out the jinja macro here: https://jinja.palletsprojects.com/en/3.0.x/templates/#macros |
Beta Was this translation helpful? Give feedback.
-
Thanks! We are in that general area, but the mechanism would be different. Suppose that instead of having a block with a plain name, the block was declared with a function, with a first implicit argument (
We would have to define the block function def foo(self) -> str:
"Function that can get info from the block"
s = self.inner # the whole content string
words = s.split()
return f"The words are: {words} and there are ({len(words)} of them)" The applications would be infinite, since what is in the block would no longer need to be the same target language than the rest of the template file (typically HTML). It could be json, csv, XML, mermaid, etc. (anything agreed-upon in advance), and the block function would transform its content into the expected string in the target language. What do you think? Is this idea making sense? |
Beta Was this translation helpful? Give feedback.
-
Jinja2 implements the concept of named block, which may have interesting applications, besides templates inheritance.
If I have the following:
I would like to find a way, through Jinja2's API, to programmatically access the content of that block and , in a manner analogous to Hugo's
.inner
mechanism.Is that possible? Does my question make sense, at all?
Beta Was this translation helpful? Give feedback.
All reactions