如何在Django模板中添加注解?

pftdvrlh  于 2023-02-10  发布在  Go
关注(0)|答案(6)|浏览(133)

我想用一句话来评论这一点:

{% if something.property %}
    <table>
        <tr>...


{% # this is a comment %}
{% if something.property %}
    <table>
        <tr>...
brccelvz

brccelvz1#

正如Miles所回答的,{% comment %}...{% endcomment %}用于多行注解,但您也可以注解掉同一行上的文本,如下所示:

{# some text #}
ztmd8pv5

ztmd8pv52#

注解标记记录在www.example.com上https://docs.djangoproject.com/en/stable/ref/templates/builtins/#std:templatetag-comment

{% comment %} this is a comment {% endcomment %}

单行注解记录在www.example.com上https://docs.djangoproject.com/en/stable/topics/templates/#comments

{# this won't be rendered #}
wbrvyc0a

wbrvyc0a3#

使用{# #}表示法,如下所示:

{# Everything you see here is a comment. It won't show up in the HTML output. #}
h9a6wy2h

h9a6wy2h4#

如果你想注解一些Django模板格式的代码,这种方法会很有帮助。
{#% include 'file.html' %#}(右侧)
如果使用HTML注解进行注解,则以下代码仍将执行。
<!-- {% include 'file.html' %} -->(方向错误)

dkqlctbz

dkqlctbz5#

这是单行注解

{# <p>This is comment</p> #}

这是多行注解

{% comment "This is an optional note for comments" %}
    <p>This is comment</p>
    <p>This is comment</p>
    <p>This is comment</p>
{% endcomment %}
mspsb9vt

mspsb9vt6#

如果你想在{% extends ... %}之前添加注解,这是不起作用的。在这种情况下,最好使用

<!--
# comment 1
# comment 2
# comment 3
-->

相关问题