如何测试使用django模板块? [英] How to test for use of a django template block?

查看:120
本文介绍了如何测试使用django模板块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行以下操作:

  {%if appnav%} 
< hr /> ;
< div id =appnav>
< ul class =tabs>
{%block appnav%} {%endblock%}
< / ul>
< / div>
{%endif%}

...然而,测试目前使用的阻止按模板进一步向下继承链似乎不起作用。



有没有其他条件可能会这样做?

解决方案

模板语言不能准确地提供您要查找的内容。子模板可以使用 {{block.super}} 调用父区块,但父模板不能引用子模板。


$ b $你最好的打赌可能是写一个自定义的模板标签。



首先,解析直到另一个块标签。这将为您提供如何解析的基础知识。



其次,解析直到另一个块标记并保存内容。通过在自定义标签中放置块标签,您可以检测内容并适当地包装。这应该是有效的,因为我相信内部块标签将被解析。如果这样不起作用,则将django提供的现有块模板标签子类化以实现您的特殊魔法。


I would like to do the following:

{% if appnav %}
<hr />
<div id="appnav">
    <ul class="tabs">
        {% block appnav %}{% endblock %}
    </ul>
</div>
{% endif %}

...however, testing for the present use of a block by templates further down the inheritance chain does not seem to work.

Is there some other conditional that might do this?

解决方案

The template language doesn't provide exactly what you're looking for. Child templates can call the parent block with {{ block.super }}, but parent templates can't reference child templates.

Your best bet will probably be to write a custom template tag. There are two sections in the template manual to review.

First, Parsing until another block tag. This will give you the basics of how to parse.

Second, Parsing until another block tag and saving contents. By placing a block tag inside the custom tag, you could detect content and wrap it as appropriate. This should work, because I believe the inner block tag will be parsed first. If that doesn't work, subclass the existing block template tag provided by django to implement your special magic.

这篇关于如何测试使用django模板块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆