Reputation: 4873
I'm starting to using Jinja, so maybe the question is trivial and I didn't get (yet) how jinja and flask are working.
What I want to do is to use a yaml file with some values (no too nested, but still a little bit), load it as a dictionary, pass the dictionary values to the jinja template and use flask to render the final html file/s.
This is the super small script I'm using for flask:
from flask import Flask, render_template
import yaml
import os
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'example.yaml')
app = Flask(__name__)
my_dic = yaml.safe_load(open(file_path))
@app.route("/")
def template_test():
return render_template('base.html', my_dic=my_dic)
if __name__ == '__main__':
app.run(debug=True)
this is the jinja template:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="container">
<p>
{{ my_dic['who']}}
</p>
</div>
</body>
</html>
and this the super silly yaml file content (that should become richer with many other values):
who:
- My string to view
Now, this is the output I see in the html:
as you can see there are []
and '
around the string. I could find a proper way (and I'm sure there is) to render just the string content.
Thanks for any suggestions
SOLVED
Thanks to the suggestions. The problem was that the values were read as array and I had to loop them:
{% for i in my_dic.who %}
<p>
i
</p>
{% endfor %}
Upvotes: 2
Views: 2609
Reputation: 21275
I think the issue is that the variable you're trying to render is an array - and jinja is rendering it as such. But you want it to be rendered as a single value - which it's not.
You should try rendering the variable as an array using a for loop: http://jinja.pocoo.org/docs/2.10/templates/
Upvotes: 3