Node.js 如何在jade中包含css文件(无链接)
我有这个翡翠档案:Node.js 如何在jade中包含css文件(无链接),node.js,pug,Node.js,Pug,我有这个翡翠档案: !!! 5 html head title test include style(type='text/css') //- DOES NOT WORK! include test.css body //- works include test.css div //- works include test.css 输出: $ jade -P test.jade rendered
!!! 5
html
head
title test include
style(type='text/css')
//- DOES NOT WORK!
include test.css
body
//- works
include test.css
div
//- works
include test.css
输出:
$ jade -P test.jade
rendered test.html
$ cat test.html
<!DOCTYPE html>
<html>
<head>
<title>test include</title>
<style type="text/css">
//- DOES NOT WORK!
include test.css
</style>
</head>
<body>body { color: peachpuff; }
<div> body { color: peachpuff; }
</div>
</body>
</html>
$jade-P test.jade
呈现的test.html
$cat test.html
测试包括
//-不行!
包含test.css
身体{颜色:桃红色;}
身体{颜色:桃红色;}
当然,我可以简单地链接css文件,但我不想这样做。一个可能的解决方案是:
style(type="text/css")
#{css}
然后在res.render(…)
调用中传递css文本。我还没有测试它(不是在我的开发计算机ATM上),但这样做可能会奏效:
!!!
head
title test include
| <style type='text/css'>
include test.css
| </style>
!!!
头
标题测试包括
|
包含test.css
|
顺便说一下,我找到了HTML2Jade在线转换器,但没有找到Jade2HTML。知道在哪里可以找到它吗?将
fs
作为数据传入,您可以
style !{fs.readFileSync("index.css").toString()}
在当前版本的Jade(0.35.0)中,只需编写
include style.css就足够了
完整示例(考虑到您正在编写index.jade,它位于视图
文件夹中,而您的样式位于资产
文件夹中):
请注意,模板中没有样式
标记,它将由jade自动插入(这是一个多么好的功能!)。Arnaud的回答对我很有效,但我发现这有点干净:
doctype
head
title test include
style(type="text/css"): include test.css
(type=“text/css”)
也是可选的,具体取决于您的情况。来自:
它工作得非常完美。我认为这是不可能的,但为什么不是简单的解决方案呢?我想您希望在服务器文件中将CSS与HTML分开,但在HTML中提供样式,以避免额外的请求来获取CSS。这是错误的:您为一个不会每次都被请求的文件添加了额外的计算(文件包含),因为它会被缓存。。这只是他问题的解决办法,我根本不明白看我对他的目标的评论;)谢谢你,工作!我不知道任何在线转换器。但我首先找到了失败的原因:“只接受脚本和样式等文本的标记不需要前导字符”()不完全是跨实现安全的,但是一个好的hack-nonetheless 1.0.0(至少),似乎不再是这样:如果不指定更多细节,css只会逐字包含,不在样式标记中您必须包含样式标记,它不会自动添加。虽然这样做有效,但我想补充一点,它与原始问题中的代码相同,不同之处在于版本(3年后)。
doctype
head
title test include
style(type="text/css"): include test.css
doctype html
html
head
style
include style.css
body
h1 My Site
p Welcome to my super lame site.