使用Jinja2检测活动标签?

时间:2017-04-13 11:15:09

标签: flask jinja2 flask-wtforms

我正在制作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可以做到吗?

1 个答案:

答案 0 :(得分:0)

您可以执行类似下面的操作,但它可能无法开箱即用!使用public class ConvertSourceToDestination : ITypeConverter<Source, Destination> { public Destination Convert(Source source, Destination destination, ResolutionContext context) { destination = destination ?? new Destination(); destination.Children = destination.Children ?? new List<Child>(); destination.Children.Add(new Child() { Property1 = source.Child1Property1, Property2 = source.Child1Property2 }); destination.Children.Add(new Child() { Property1 = source.Child2Property1, Property2 = source.Child2Property2 }); destination.Name = source.Name; return destination; } } public static class SourceExtension { public static IEnumerable<Child> Children(this Source source) { yield return new Child() { Property1 = source.Child1Property1, Property2 = source.Child1Property2 }; yield return new Child() { Property1 = source.Child2Property1, Property2 = source.Child2Property2 }; } public static MapperConfiguration CreateMapping() { var config = new MapperConfiguration( cfg => { cfg.CreateMap<Source, Destination>() .ForMember(dest => dest.Children, opt => opt.MapFrom(src => src.Children())); }); return config; } public static MapperConfiguration CreateMapping2() { var config = new MapperConfiguration( cfg => { cfg.CreateMap<Source, Destination>().ConvertUsing(new ConvertSourceToDestination()); }); return config; } } 包装时,您可以将变量传递给包含。您可能需要设置另一个名为hidden的CSS类,如果您还没有,这可以这样做:

with

然后在building_form中:

.hidden {
    display: none;
}

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