Tera: How can you reuse a block value in the template, without repeating the initial value?

The Zola docs has an example like this, which is a very common pattern:

<head>
    <title>{% block title %}{% endblock title %} – {{ config.title }}</title>
</head>

Now, lets say I wanna add a few more call-sites to the value being resolved as title here, such as:

  <meta name="twitter:title" content="..." />

and

  <meta property="og:title" content="..." />

As far as I understand from https://tera.netlify.app/docs/#inheritance, it’s not possible to use blocks in resolution of variables, so there’s no way to use {% set ... %} to have a local title variable to use in all those three cases of the title value needed.

Of course, another option is to copy-paste the same logic into all three places. But that’s harder to maintain in the long run.

Is anyone aware of any solution for this, to be able to prevent duplication of the same composition, while using inheritance?


Fwiw, at the moment, I’m using local variables to calculate the title value, with {% if ... %} conditions for various section and page values. Of course, this is doable, but again non-ideal, since it’s forcing me to put section-specific logic into my base template. Hence, I’m looking for a solution better than this.

I think https://github.com/Keats/tera/issues/265 is the feature you want to have?

1 Like

Thanks, @keats, for the prompt response. Yep, that sounds live what I need, and glad to see there’s already a PR for it!


A side note I like to mention here, though, is that I would love to see the template system supporting a variable-based inheritance/specialization, in addition to the block-based method currently supported.

This is based on the view that: both variables (like of primitive value types) and blocks (written as strings here, but mostly perceived as document pieces) are things worth processing and customizing.

I know most people probably rely on macros to share value-oriented logic. But maybe that doesn’t need to be the case, if there’s support for this in the inheritance system.

Just my pair of pennies.

What do you mean exactly? Do you have an example of some template engine doing that?