包含
包含允许您将一个 Pug 文件的内容插入到另一个文件中。
xxxxxxxxxx
//- index.pug doctype html html include includes/head.pug body h1 我的网站 p 欢迎来到我的超级 lame 网站。 include includes/foot.pug
xxxxxxxxxx
//- includes/head.pug head title 我的网站 script(src='/javascripts/jquery.js') script(src='/javascripts/app.js')
xxxxxxxxxx
//- includes/foot.pug footer#footer p 版权 (c) foobar
xxxxxxxxxx
<html> <head> <title>我的网站</title> <script src="/javascripts/jquery.js"></script> <script src="/javascripts/app.js"></script> </head> <body> <h1>我的网站</h1> <p>欢迎来到我的超级 lame 网站。</p> <footer id="footer"> <p>版权 (c) foobar</p> </footer> </body> </html>
如果路径是绝对路径(例如,include /root.pug
),则通过添加前缀options.basedir
来解析。否则,路径相对于正在编译的当前文件解析。
如果没有给出文件扩展名,则.pug
会自动追加到文件名。
包含纯文本
包含非 Pug 文件仅包含其原始文本。
xxxxxxxxxx
//- index.pug doctype html html head style include style.css body h1 我的网站 p 欢迎来到我的超级 lame 网站。 script include script.js
xxxxxxxxxx
/* style.css */ h1 { color: red; }
xxxxxxxxxx
// script.js console.log('您真棒');
xxxxxxxxxx
<html> <head> <style> /* style.css */ h1 { color: red; } </style> </head> <body> <h1>我的网站</h1> <p>欢迎来到我的超级 lame 网站。</p> <script> // script.js console.log('您真棒'); </script> </body> </html>
包括筛选文本
您可以将筛选器与包含项结合使用,以便在包含时对内容进行筛选。
//- index.pug
html
head
title An Article
body
include:markdown-it article.md
# article.md
This is an article written in markdown.
<html>
<head>
<title>An Article</title>
</head>
<body>
<h1>article.md</h1>
<p>This is an article written in markdown.</p>
</body>
</html>