Jinja模板与if语句循环

时间:2019-07-15 19:28:27

标签: python jinja2

我认为可以执行以下操作(通过解压缩的字典var storageMappingItemCollection = CreateStorageMappingItemCollection( (EdmItemCollection)itemCollection, storeItemCollection, inputFile, textTransform.Host, textTransform.Errors); var functionImports = storageMappingItemCollection .GetItems<EntityContainerMapping>() .SelectMany(m => m.FunctionImportMappings) .ToList(); foreach (var item in functionImports) { #> // <#=item.FunctionImport.Name#> => [<#=item.TargetFunction.Schema ?? ""#>].[<#=item.TargetFunction.Name#>] <# } ,其中**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 %}

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 '{'

set

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

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

但是它们会导致类似的{% set is_true = dict_key2{{ n }} %} {% if is_true %}

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:2)

问题在于<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea id="textareaid" name="name" rows="8" cols="80"> TERMO CONTRATUAL CONTRATADO: AAAAA, razão social: AAAAA, pessoa jurídica de direito privado, inscrita no CNPJ/MF sob o nº AAAAAA, estabelecida à AAAAAA; CONTRATANTE e/ou ALUNO: </textarea> <br> - <a href="#" title="Rua" class="button" onclick="insertAtCaret('textareaid', '$sAlunoRua');return false;">$sAlunoRua</a> - <a href="#" title="Numero" class="button" onclick="insertAtCaret('textareaid', '$sAlunoNumero');return false;">$sAlunoNumero</a> - <a href="#" title="Bairro" class="button" onclick="insertAtCaret('textareaid', '$sAlunoBairro');return false;">$sAlunoBairro</a> - <a href="#" title="Cidade" class="button" onclick="insertAtCaret('textareaid', '$sAlunoCidade');return false;"> $sAlunoCidade</a> <script> function insertAtCaret(areaId, text) { var txtarea = document.getElementById(areaId); if (!txtarea) { return; } var scrollPos = txtarea.scrollTop; var strPos = 0; var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false)); if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart('character', -txtarea.value.length); strPos = range.text.length; } else if (br == "ff") { strPos = txtarea.selectionStart; } var front = (txtarea.value).substring(0, strPos); var back = (txtarea.value).substring(strPos, txtarea.value.length); txtarea.value = front + text + back; strPos = strPos + text.length; if (br == "ie") { txtarea.focus(); var ieRange = document.selection.createRange(); ieRange.moveStart('character', -txtarea.value.length); ieRange.moveStart('character', strPos); ieRange.moveEnd('character', 0); ieRange.select(); } else if (br == "ff") { txtarea.selectionStart = strPos; txtarea.selectionEnd = strPos; txtarea.focus(); } txtarea.scrollTop = scrollPos; } </script>是无效的Jinja。通常,access a key in a dictionary in jinja会执行类似${dict_key1{{ n }}}{{ dictionary.field }}的操作。

基于以下示例:

{{ dictionary[field] }}

可能的解决方案是:

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

在上面的示例中,我们使用string concat operator {% 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 %} 使用special loop index variable将密钥与所需的数字连接起来。选择~是因为它从索引1开始。类似loop.index的东西也应该起作用。