Automation

 View Only

IMPORTANT MODERATION NOTICE

This community is currently under full moderation, meaning  all posts will be reviewed before appearing in the community. Please expect a brief delay—there is no need to post multiple times. If your post is rejected, you'll receive an email outlining the reason(s). We've implemented full moderation to control spam. Thank you for your patience and participation.



Expand all | Collapse all

Question on Loading a Jinja Template

This thread has been viewed 10 times
  • 1.  Question on Loading a Jinja Template

     
    Posted 01-30-2022 07:24

    Hi.

     

    I have the following very basic Python script that uses a Jinja template file "scrap.j2" and instead of using a YAML file, I feed variables in the python script directly using a string.

     

    I am unable to successfully load the Jinja template. I get no error, but the result is an empty string.

     

    Here is my Python script

    #! /usr/bin/python3

    from jinja2 import Environment, FileSystemLoader

     

    #load Jinja Template

    env=Environment(loader=FileSystemLoader("./"))

    my_template = env.get_template("scrap.j2")

     

    #feed variables to the template

    result = my_template.render({"user": "lab"})

     

    print(result)

     

     

    Here is my Jinja file scrap.j2

    hello {{ user }}

     

     

     

    I expect the script to print "hello lab", but instead it's printing an empty string.

     

     

    Kindly let me know what I'm doing wrong here.

     

     

    Many thanks,

    Deepak


    Juniper Business Use Only



  • 2.  RE: Question on Loading a Jinja Template

     
    Posted 03-04-2022 15:05
    Hi Deepak,

    A quick copy/paste of your code with the associated jinja2 template works just fine for me.

    Maybe check versions of libraries, I was using Jinja2 3.0.1 and I also tested with 3.0.3 

    pip3 freeze | grep Jinja2
    Jinja2==3.0.3

    ./foo.py
    hello lab

    Regards

    ------------------------------
    Andy Sharp
    ------------------------------