tera icon indicating copy to clipboard operation
tera copied to clipboard

Add a way to partially render a template (ignore missing variables in context)

Open minev-dev opened this issue 9 months ago • 6 comments

Problem

There is no way to partially render a template with the only variables available in a context and ignore the template variables that are not presented in the context.

Currently the Processor::render returns an error if at least one variable is failed to render https://github.com/Keats/tera/blob/ae13d7ce39d732aae3f68435ed52c60732fe0ee0/src/renderer/processor.rs#L1058-L1062

Context

We have a multi-step rendering where not all the variables are available at the same time. There is a config file which should support system env variables injection (first step) and then cross-services variables injection in runtime (second step).

Here is a draft PR with the implementation and failing unit test https://github.com/opencloudtool/opencloudtool/pull/283/commits/20ae6de0b2fb1b25c666c28591b44aa113330b4f

Expected behavior

Initial template

[project]
name = "{{ env.PROJECT_NAME }}"
description = "{{ runtime.GOOD_DESCRIPTION }}"

First step

Context

{
  "env": {
    "PROJECT_NAME": "Project Name"
  }
}

Rendered template

[project]
name = "Project Name"
description = "{{ runtime.GOOD_DESCRIPTION }}"

Second step

Context

{
  "runtime": {
    "GOOD_DESCRIPTION": "Good Description"
  }
}

Rendered template

[project]
name = "Project Name"
description = "Good Description"

minev-dev avatar Mar 05 '25 08:03 minev-dev

That's not going to be built in but you can probably write your own filter to handle that

Keats avatar Mar 05 '25 11:03 Keats

That's not going to be built in

Is there a reason for this? Do you need help with the implementation?

you can probably write your own filter to handle that

I suppose it'll require the following syntax {{ runtime.GOOD_DESCRIPTION | some_filter }} which is not a pretty syntax if we want end user to use that. Or is there a way to have a default filter?

minev-dev avatar Mar 05 '25 16:03 minev-dev

Is there a reason for this? Do you need help with the implementation?

It's an important feature of Tera to error if it's missing things rather than silently ignoring it.

I suppose it'll require the following syntax {{ runtime.GOOD_DESCRIPTION | some_filter }} which is not a pretty syntax if we want end user to use that. Or is there a way to have a default filter?

You can also have it as a function eg, {{ get_runtime(name=...) }}

Keats avatar Mar 05 '25 17:03 Keats

It's an important feature of Tera to error if it's missing things rather than silently ignoring it.

I didn't mean that the current behavior should be replaced. There should be a way to optionally ignore or tolerate the errors

You can also have it as a function eg, {{ get_runtime(name=...) }}

Will try it, thanks. But anyway it doesn't look like an easy-to-use format for the end user who just wants to use a variable

minev-dev avatar Mar 05 '25 17:03 minev-dev

@Keats Added a Proof-of-Concept implementation of what we approximately need https://github.com/Keats/tera/compare/master...minev-dev:tera:master It helps to tolerate the template variables that are not presented in a context.

What do you think about investing some time into a well-shaped implementation? Or can you please give me recommendations on how I can properly implement it so I'll submit a PR?

minev-dev avatar Mar 06 '25 07:03 minev-dev

No that won't be added

Keats avatar Mar 07 '25 10:03 Keats