【问题标题】:Detect active tab using Jinja2?使用 Jinja2 检测活动标签?
【发布时间】:2017-04-13 23:50:14
【问题描述】:

我正在制作一个 Flask Webapp,并且我有以下标签内容:

<div class="tab-content">
   <div class="tab-pane fade in active" id="gable">  
       {% include 'building_form.html' %}
   </div>
   <div class="tab-pane fade" id="shed">  
       {% include 'building_form.html' %}
   </div>
   <div class="tab-pane fade" id="flat">  
       {% include 'building_form.html' %}
   </div>
</div>

building_form.html 的代码是:

<form method="post" action="">
  {{ form.hidden_tag() }}

    <div class="form-group label-floating">
        {{ wtf.form_field(form.width) }}<br>
    </div>
    <div class="form-group label-floating">
        {{ wtf.form_field(form.length) }}<br>
    </div>
    <div class="form-group label-floating">
        {{ wtf.form_field(form.bottom_height) }}<br>
    </div>
    <div class="form-group label-floating">
        {{ wtf.form_field(form.top_height) }}<br>
    </div>
    {% if ???? %} <!--What put here?-->
    <div class="form-group label-floating">
        {{ wtf.form_field(form.ridge_height) }}<br>
    </div>
    {% endif %}
<p><input type=submit value="Calcular"></p>
</form>

我只想在 id="gable" 处于活动状态时呈现“form.ridge_height”。使用 Jinja2 可以做到吗?

【问题讨论】:

  • 你设法解决了这个问题吗?

标签: flask jinja2 flask-wtforms


【解决方案1】:

您可以执行以下操作,但它可能无法开箱即用!当使用with 包装变量时,您可以将变量传递到包含中。如果您还没有,您可能需要设置另一个名为 hidden 的 CSS 类,可以这样做:

.hidden {
    display: none;
}

{% with gable = True %}
    {% include 'building_form.html' %}
{% endwith %}

然后在 building_form 内:

<div class="form-group label-floating {{ '' if gable == True else 'hidden' }}">
    {{ wtf.form_field(form.ridge_height) }}<br>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多