Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
minev-dev opened this issue Mar 5, 2025 · 6 comments

Comments

@minev-dev
Copy link

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

tera/src/renderer/processor.rs

Lines 1058 to 1062 in ae13d7c

pub fn render(&mut self, write: &mut impl Write) -> Result<()> {
for node in &self.template_root.ast {
self.render_node(node, write)
.map_err(|e| Error::chain(self.get_error_location(), e))?;
}

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 opencloudtool/opencloudtool@20ae6de

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"
@Keats
Copy link
Owner

Keats commented Mar 5, 2025

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

@minev-dev
Copy link
Author

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?

@Keats
Copy link
Owner

Keats commented Mar 5, 2025

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=...) }}

@minev-dev
Copy link
Author

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
Copy link
Author

@Keats Added a Proof-of-Concept implementation of what we approximately need 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?

@Keats
Copy link
Owner

Keats commented Mar 7, 2025

No that won't be added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants