Skip to content

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

@minev-dev

Description

@minev-dev

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions