Egorrko
Egorrko

Reputation: 63

Jinja nested template inside variable in template

I want to do custom notification system. Right now i have table in database, that have html code, what i put on my page. But if I insert jinja expressions into this code, they are displayed as plain text.

Example:

def render():
    hidden_variable = "world"
    example = "Hello {{hidden_variable}}!"

    return {'example': example, 'hidden_variable': hidden_variable}

template.html

<div>
{{example}}
</div>

What i get:

Hello {{hidden_variable}}!

What i want:

Hello world!

Upvotes: 2

Views: 3025

Answers (2)

Egorrko
Egorrko

Reputation: 63

Maybe you need to use nested jinja templates:

nested_template = 'my_custom_template'
hidden_variable = 'hello world'

base_template.html.j2

{% include 'nested/' + nested_template + '.html.j2' %}

nested/my_custom_template.html.j2

{{ hidden_variable }}

Upvotes: -1

Bijesh Raj Kunwar
Bijesh Raj Kunwar

Reputation: 63

That example is being stored as string because you are using jinja syntax inside a python function.

Jinja is a web template engine for the Python.

Instead:

example = f"Hello {hidden_variable}!"

Upvotes: 3

Related Questions