html - Pug 中的条件语句
问题描述
我有以下用例,在我的项目中,我有两个不同的页脚。感谢@sean 的建议,我尝试了一个案例陈述。这是我的模板:
//- layout.pug
include head
body
block nav
include nav
block content
block footer
case chooseFooter
when 1
include footer1
when 2
include footer2
block scripts
script(src='js/index.js')
这是第一个页脚:
li
.footer_contact1
li
.footer_contact1
这是第二个页脚:
ul.boxes
li
.footer_contact2
li
.footer_contact2
这是调用模板的索引页面:
extend includes/layout
append content
.promo
ul.boxes
li
.boxes__text-wrapper
li
.boxes__text-wrapper
li
.boxes__text-wrapper
li
.boxes__text-wrapper
prepend footer
- let chooseFooter = 1
如果我选择footer1,问题就一致了,我得到以下输出:
<div class="promo">
<p>some text</p>
</div>
<ul class="boxes">
<li>
<div class="boxes__text-wrapper"></div>
</li>
<li>
<div class="boxes__text-wrapper"></div>
</li>
<li>
<div class="boxes__text-wrapper"></div>
</li>
<li>
<div class="boxes__text-wrapper"></div>
</li>
</ul>
<li>
<div class=".footer_contact1"></div>
</li>
<li>
<div class=".footer_contact1"></div>
</li>
<script src="js/index.js"></script>
所以第一个页脚中的 li:s 在 ul 之外,它们应该在结束 ul 之内。
如果我不使用案例代码而只是做一个正常的包含并将其插入到索引文件中的最后一个 li 项之后并使用以下内容:
li
.boxes__text-wrapper
include includes/footer
然后在结束 ul 标记之前嵌入 footer1 li 项目。但是我自然不能只使用一个页脚。
我希望这次我更有意义。
谢谢 :)
解决方案
试试这个方法。在模板块中设置一个条件语句或case语句来检查变量的值。
布局.pug
html
head
block head
body
nav
block content
block footer
footer
case whichFooter
when 1
p Some footer content
when 2
p Some other footer content
default
p Some other default footer content
block scripts
script(src='/my.js')
script(src='/pets.js')
然后在您的页面中定义该变量,并将其添加到模板中的相应块中。
page.pug
extends layout
append head
title Page A
append content
p My page content...
prepend footer
- let whichFooter = 1
这样,您就可以在 pug 需要它来检查条件或 case 语句之前有效地定义变量。
(也不要忘记将它嵌套在nav
里面body
而不是旁边。)
推荐阅读
- android - Android Studio 错误:无法连接到守护进程
- python - pygame sprite 的一个实例正在更改整个组 pos
- android - 在 Realm 的 recyclerview 中删除对象的问题
- java - 如何解决 draw() 它不绘制我的屏幕
- scala - 如何有条件地从列中删除前两个字符
- angular-material - 如何为 Angular Material Table SelectionModel 添加初始选择?
- python - discord.py - 如何向特定成员添加角色?
- javascript - Vue.js:如何使用 id 选择另一个数组中的项目
- django - Django 国际化和本地化而不是 /en 为每种语言使用特定域
- c# - 强制 asp:LoginView 显示带有代码的匿名模板