简体   繁体   English

声明不在jinja工作

[英]Do statement not working in jinja

I'm altering an existing web interface to view ROBOT doc libraries, which uses a mixture of jinja (Python inside HTML) and HTML. 我正在改变现有的Web界面来查看ROBOT doc库,它使用jinja(HTML内部的Python)和HTML的混合。 I have never worked with jinja or HTML before and am having issues getting even a simple test case to work. 我以前从未使用过jinja或HTML,并且在遇到简单的测试用例时遇到了问题。 When the browser loads the docs, I want our project's directory structure for the docs to be preserved to make finding things easier, and so I want to use jinja to create the dir structure. 当浏览器加载文档时,我希望保留文档的项目目录结构,以便更容易查找,因此我想使用jinja创建dir结构。 Here is a snippet of the code I'm working with: 这是我正在使用的代码片段:

{% extends "base.html" %}
{% block body %}
<div class="well" id="left">
  <ul class="list-group list-unstyled">
    {% set collection_list = [] %}
    {% for collection in data.hierarchy %}
      {% if collection.collection_id|string == data.collection_id|string %}
          {% do collection_list.append(collection.path) %}
      {% else %}
        {% for link in collection.path_chain %}
          <li>
          <label class="tree-toggler nav-header"
                 title="file path: {{collection.path}}">{{link}}</label>
          <ul class="list-group  tree collapse"
              id={{link}}>
              </ul>
        {% endfor %}
          </li>
      {% endif %}

...there's more after that, but this is where I hit the error. ......之后还有更多,但这是我遇到错误的地方。 It sets the collection_list var fine, and the if statements work, but when it goes to execute the 'do' statement it fails with: 它将collection_list设置为var,并且if语句可以工作,但是当它执行'do'语句时,它失败了:

TemplateSyntaxError: Encountered unknown tag 'do'. TemplateSyntaxError:遇到未知标签'do'。 Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. Jinja正在寻找以下标签:'elif'或'else'或'endif'。 The innermost block that needs to be closed is 'if'. 需要关闭的最里面的块是“if”。

I don't believe this is an unclosed loop or something because if I replace the do statement with a simple test print statement, it works. 我不相信这是一个未闭合的循环或东西,因为如果我用一个简单的测试打印语句替换do语句,它的工作原理。 Does anyone know what I'm doing wrong? 有谁知道我做错了什么?

From the template documentation : 模板文档

Expression Statement 表达声明

If the expression-statement extension is loaded, a tag called do is available that works exactly like the regular variable expression ( {{ ... }} ); 如果加载了expression-statement扩展,则可以使用名为do的标记,其作用与常规变量表达式( {{ ... }} )完全相同; except it doesn't print anything. 除了它不打印任何东西。 This can be used to modify lists: 这可以用来修改列表:

 {% do navigation.append('a string') %} 

You need to enable the Expression statement extension for this to work. 您需要启用Expression语句扩展才能使其正常工作。

You didn't show how you load the Jinja2 environment, but loading extensions takes place via the extensions argument to the Environment() class : 您没有显示如何加载Jinja2环境,但是通过extensions Environment()extensions参数加载扩展:

jinja_env = Environment(extensions=['jinja2.ext.do'])

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

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