How to make a for loop in Jinja?
You can create a loop like this: {% for i in range(11) %} {{ i }} {% endfor %}
You can create a loop like this: {% for i in range(11) %} {{ i }} {% endfor %}
Try also dictionary-based approach. It seems to be less ugly. {% set vars = {‘foo’: False} %} {% for item in items %} {% if vars.update({‘foo’: True}) %} {% endif %} {% if vars.foo %} Ok(1)! {% endif %} {% endfor %} {% if vars.foo %} Ok(2)! {% endif %} This also renders: Ok(1)! Ok(2)!
{% include “file” %} does this. See the jinja2 docs for more information.
I feel like you’re asking two questions here but I’ll take a shot… For the posting url you’d do this: <a href=”https://stackoverflow.com/questions/11124940/{{ url_for(“post_blueprint.get_post’, year=year, month=month, title=title)}}”> {{ title }} </a> To handle static files I’d highly suggest using an asset manager like Flask-Assets, but to do it with vanilla flask you do: {{ url_for(‘static’, filename=”[filenameofstaticfile]”) … Read more
I am sure there is a smarter way for doing what you want but this should work: – name : Test var hosts : all gather_facts : no vars: myvariable : false tasks: – name: param1 set_fact: myvariable: “{{param1}}” when: param1 is defined – name: param2 set_fact: myvariable: “{{ param2 if not myvariable else myvariable … Read more
Solved! service_type: “{{ ‘NodePort’ if expose_service == ‘true’ else ‘ClusterIP’ }}”
Mako actually provides a VERY nice way to track down errors in a template: from mako import exceptions try: template = lookup.get_template(uri) print template.render() except: print exceptions.html_error_template().render()
You are looking for the mapping test: {% if {‘a’: 1, ‘b’: 2} is mapping %} “Oh Yes!” {% else %} “Oh No!” {% endif %} Jinja is not Python though, so you don’t have access to all the builtins (type and print do not exist, for example, unless you add them to the context. … Read more
The other option is to redefine the delimiters used by Vue.js. This is handy if you have a lot of existing template code and you wish to start adding Vue.js functionality to a Flask or Django project. var app = new Vue({ el: ‘#app’, data: { message: ‘Hello Vue!’ }, delimiters: [‘[[‘,’]]’] }) Then in … Read more
When you load your jinja2.Environment, set the ‘undefined’ parameter to ‘jinja2.StrictUndefined’, e.g.: env = jinja2.Environment(loader=<someloader>, undefined=jinja2.StrictUndefined) You can catch and examine the render exception to see what was missing EDIT It would help if I read your full question. 🙂