简体   繁体   English

Jinja模板与if语句循环

[英]Jinja template for loop with if statement

I thought it would be possible to do the following (passing an unpacked dictionary **my_dict , where {dict_key21: True} ): 我认为可以执行以下操作(通过解压后的字典**my_dict ,其中{dict_key21: True} ):

{% for n in range(my_dict_len): %}
    <td>${dict_key1{{ n }}}</td>
    {% if dict_key2{{ n }} %}
        <td>New cell</td>
    {% else %}
        # Do nothing
    {% endif %}
{% endfor %}

But I get a jinja2.exceptions.TemplateSyntaxError : 但是我得到了jinja2.exceptions.TemplateSyntaxError

{% if a_dict{{ n }} %}
  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 497, in _parse
    return Parser(self, source, name, encode_filename(filename)).parse()
  File "/usr/local/lib/python3.6/site-packages/jinja2/parser.py", line 901, in parse
    result = nodes.Template(self.subparse(), lineno=1)
  File "/usr/local/lib/python3.6/site-packages/jinja2/parser.py", line 883, in subparse
    rv = self.parse_statement()
  File "/usr/local/lib/python3.6/site-packages/jinja2/parser.py", line 130, in parse_statement
    return getattr(self, 'parse_' + self.stream.current.value)()
  File "/usr/local/lib/python3.6/site-packages/jinja2/parser.py", line 199, in parse_for
    body = self.parse_statements(('name:endfor', 'name:else'))
  File "/usr/local/lib/python3.6/site-packages/jinja2/parser.py", line 165, in parse_statements
    result = self.subparse(end_tokens)
  File "/usr/local/lib/python3.6/site-packages/jinja2/parser.py", line 883, in subparse
    rv = self.parse_statement()
  File "/usr/local/lib/python3.6/site-packages/jinja2/parser.py", line 130, in parse_statement
    return getattr(self, 'parse_' + self.stream.current.value)()
  File "/usr/local/lib/python3.6/site-packages/jinja2/parser.py", line 213, in parse_if
    'name:endif'))
  File "/usr/local/lib/python3.6/site-packages/jinja2/parser.py", line 164, in parse_statements
    self.stream.expect('block_end')
  File "/usr/local/lib/python3.6/site-packages/jinja2/lexer.py", line 384, in expect
    self.name, self.filename)
jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got '{'

I've also tried different formats and using set : 我也尝试了不同的格式并使用set

{% if ${dict_key2{{ n }}} %}

{% if dict_key2{{ n }} == True %}

{% set is_true = dict_key2{{ n }} %}
{% if is_true %}

But they result in a similar jinja2.exceptions.TemplateSyntaxError 但是它们会导致类似的jinja2.exceptions.TemplateSyntaxError

Is there a way to achieve that? 有办法实现吗?

The issue is that ${dict_key1{{ n }}} is invalid Jinja. 问题是${dict_key1{{ n }}}是无效的Jinja。 Typically to access a key in a dictionary in jinja you would do something like {{ dictionary.field }} or {{ dictionary[field] }} . 通常,要访问jinja中的字典中的键,您将执行类似{{ dictionary.field }}{{ dictionary[field] }}

Based off the example of: 基于以下示例:

my_dict = {'dict_key11': 'Title', 'dict_key21': True}

A possible solution is: 可能的解决方案是:

{% set dict_length = my_dict|count-1 %}
{% for _ in range(dict_length) %}
  <td>{{ my_dict['dict_key1'~loop.index] }}</td>
  {% if my_dict['dict_key2'~loop.index] %}
      <td>New cell</td>
  {% else %}
      {# Do nothing #}
  {% endif %}
{% endfor %}

In the above example we're using the string concat operator ~ to join the key with the required number using a special loop index variable . 在上面的示例中,我们使用字符串concat运算符 ~使用特殊的循环索引变量将键与所需的数字连接起来。 loop.index was chosen because it starts at index 1. Something like range(1, my_dict|count) should also work. 选择loop.index是因为它从索引1开始。诸如range(1, my_dict|count)类的东西也应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM