[Python] get global variable with dashes in jinja2

Discussão em 'Python' iniciado por Stack, Maio 6, 2025.

  1. Stack

    Stack Membro Participativo

    is there a way to render the variable b-c (456) by changing only template variable in the following code?

    import jinja2

    environment = jinja2.Environment()

    data = {'a':123, 'b-c':456}
    template = "hello {{ b-c }}"

    result = environment.from_string(template, globals = data).render()

    print('result:', result)


    the expected result:


    hello 456
    ​

    current code throws error:


    jinja2.exceptions.UndefinedError: 'b' is undefined
    ​

    i can't change how template variables (data) defined or how template is rendered at the moment. but i can change template.

    Continue reading...

Compartilhe esta Página