ism*_*ail 1 django django-templates
我知道django故意不允许模板中有很多逻辑.但是,有时您需要评估某些内容并根据更改您的选项.
如何更改模板中的值或仅在第一条记录中插入内容?但是你仍然想要完成其余的工作.例如,我的模板代码如下:
{% for object in object_list %}
<div id="t{{ object.id }}-header" class="content_headings title_highlight" >{{ object.title }}</div>
<div id="t{{ object.id }}-content">
......
Run Code Online (Sandbox Code Playgroud)
PHP模板中的类似代码:
<div id="t<?php if ($i != 1) { echo $i-1; } ?>-header" class="content_headings<?php if ($i == 1) { ?> title_highlight<?php } ?>" ><?php the_title(); ?></div>
<div id="t<?php if ($i != 1) { echo $i-1; } ?>-content">
Run Code Online (Sandbox Code Playgroud)
forloop.first是要走的路.我认为您需要做的只是略微改变Tiago的答案,并获得以下内容:
{% for object in object_list %}
<div id="t{% if not forloop.first %}{{ object.id }}{% endif %}-header" class="content_headings{% if forloop.first %} title_highlight{% endif %}">
{{ object.title }}
</div>
<div id="t{% if not forloop.first %}{{ object.id }}{% endif %}-content">
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我已经检查了你的PHP代码,它似乎做的几乎完全相同(我没有从1中取出{{ object.id }}因为只要ID是唯一的,它就不会产生影响,对吧?)
| 归档时间: |
|
| 查看次数: |
3751 次 |
| 最近记录: |