'Ignore unknown functions in Jinja2

When rendering a Jinja2 template, either something like:

  • "hello {{world}}"
  • "hello {{get_world()}}"

I am interested in simply "passing along" any templated variables or functions that are not provided to Jinja. For example, if the variable {{world}} does not exist in the context, I want the output to keep {{world}}. If the the function {{get_world()}} does not exist in the context, I want to keep {{get_world()}}.

The closest I've gotten is with the following:

from jinja2 import Environment, BaseLoader, DebugUndefined

template = Environment(loader=BaseLoader, undefined=DebugUndefined).from_text("my template")

However, this above only works with the example "hello {{world}}", but not the example "hello {{get_world()}}". How can I achieve this functionality for both of these cases?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source